private List<ImportDeclaration> getImports(Object parent) throws JavaModelException { List<ImportDeclaration> result = new ArrayList<>(); Set<Object> objects = childrens.get(parent); if (objects == null) { return result; } for (Object object : objects) { if (object instanceof IImportContainer) { Set<Object> imports = childrens.get(object); if (imports == null) { continue; } addImport(result, imports); } } return result; }
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { switch (element.getElementType()) { case IJavaElement.FIELD: return new ASTNode[] {getFieldOrEnumConstantDeclaration((IField) element, cuNode)}; case IJavaElement.IMPORT_CONTAINER: return getImportNodes((IImportContainer) element, cuNode); case IJavaElement.IMPORT_DECLARATION: return new ASTNode[] {getImportDeclarationNode((IImportDeclaration) element, cuNode)}; case IJavaElement.INITIALIZER: return new ASTNode[] {getInitializerNode((IInitializer) element, cuNode)}; case IJavaElement.METHOD: return new ASTNode[] { getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode) }; case IJavaElement.PACKAGE_DECLARATION: return new ASTNode[] {getPackageDeclarationNode((IPackageDeclaration) element, cuNode)}; case IJavaElement.TYPE: return new ASTNode[] {getAbstractTypeDeclarationNode((IType) element, cuNode)}; default: Assert.isTrue(false, String.valueOf(element.getElementType())); return null; } }
private void copyImportsToDestination( IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException { ListRewrite listRewrite = rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY); IJavaElement[] importDeclarations = container.getChildren(); for (int i = 0; i < importDeclarations.length; i++) { IImportDeclaration declaration = (IImportDeclaration) importDeclarations[i]; ImportDeclaration sourceNode = ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode); ImportDeclaration copiedNode = (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode); if (getLocation() == IReorgDestination.LOCATION_BEFORE) { listRewrite.insertFirst(copiedNode, null); } else { listRewrite.insertLast(copiedNode, null); } } }
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException { switch(element.getElementType()){ case IJavaElement.FIELD: return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)}; case IJavaElement.IMPORT_CONTAINER: return getImportNodes((IImportContainer)element, cuNode); case IJavaElement.IMPORT_DECLARATION: return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)}; case IJavaElement.INITIALIZER: return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)}; case IJavaElement.METHOD: return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)}; case IJavaElement.PACKAGE_DECLARATION: return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)}; case IJavaElement.TYPE: return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)}; default: Assert.isTrue(false, String.valueOf(element.getElementType())); return null; } }
private void copyImportsToDestination(IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException { ListRewrite listRewrite= rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY); IJavaElement[] importDeclarations= container.getChildren(); for (int i= 0; i < importDeclarations.length; i++) { IImportDeclaration declaration= (IImportDeclaration) importDeclarations[i]; ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode); ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode); if (getLocation() == IReorgDestination.LOCATION_BEFORE) { listRewrite.insertFirst(copiedNode, null); } else { listRewrite.insertLast(copiedNode, null); } } }
/** * Returns the input for the given element. The Java breadcrumb does not show some elements of * the model: * <ul> * <li><code>ITypeRoots</li> * <li><code>IPackageDeclaration</li> * <li><code>IImportContainer</li> * <li><code>IImportDeclaration</li> * </ul> * * @param element the potential input element * @return the element to use as input */ private IJavaElement getInput(IJavaElement element) { try { if (element instanceof IImportDeclaration) element= element.getParent(); if (element instanceof IImportContainer) element= element.getParent(); if (element instanceof IPackageDeclaration) element= element.getParent(); if (element instanceof ICompilationUnit) { IType[] types= ((ICompilationUnit) element).getTypes(); if (types.length > 0) element= types[0]; } if (element instanceof IClassFile) element= ((IClassFile) element).getType(); return element; } catch (JavaModelException e) { return null; } }
private Object[] getChildren(IType type) throws JavaModelException{ IParent parent; if (type.isBinary()) parent= type.getClassFile(); else { parent= type.getCompilationUnit(); } if (type.getDeclaringType() != null) return type.getChildren(); // Add import declarations IJavaElement[] members= parent.getChildren(); ArrayList<IJavaElement> tempResult= new ArrayList<IJavaElement>(members.length); for (int i= 0; i < members.length; i++) if ((members[i] instanceof IImportContainer)) tempResult.add(members[i]); tempResult.addAll(Arrays.asList(type.getChildren())); return tempResult.toArray(); }
/** * Answers if the given <code>element</code> is a valid * element for this part. * * @param element the object to test * @return <true> if the given element is a valid element */ @Override protected boolean isValidElement(Object element) { if (element instanceof IMember) return super.isValidElement(((IMember)element).getDeclaringType()); else if (element instanceof IImportDeclaration) return isValidElement(((IJavaElement)element).getParent()); else if (element instanceof IImportContainer) { Object input= getViewer().getInput(); if (input instanceof IJavaElement) { ICompilationUnit cu= (ICompilationUnit)((IJavaElement)input).getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { ICompilationUnit importContainerCu= (ICompilationUnit)((IJavaElement)element).getAncestor(IJavaElement.COMPILATION_UNIT); return cu.equals(importContainerCu); } else { IClassFile cf= (IClassFile)((IJavaElement)input).getAncestor(IJavaElement.CLASS_FILE); IClassFile importContainerCf= (IClassFile)((IJavaElement)element).getAncestor(IJavaElement.CLASS_FILE); return cf != null && cf.equals(importContainerCf); } } } return false; }
public static ASTNode[] getImportNodes(IImportContainer reference, CompilationUnit cuNode) throws JavaModelException { IJavaElement[] imps = reference.getChildren(); ASTNode[] result = new ASTNode[imps.length]; for (int i = 0; i < imps.length; i++) { result[i] = getImportDeclarationNode((IImportDeclaration) imps[i], cuNode); } return result; }
public static ASTNode[] getImportNodes(IImportContainer reference, CompilationUnit cuNode) throws JavaModelException { IJavaElement[] imps= reference.getChildren(); ASTNode[] result= new ASTNode[imps.length]; for (int i= 0; i < imps.length; i++) { result[i]= getImportDeclarationNode((IImportDeclaration)imps[i], cuNode); } return result; }
protected void copyToDestination(IJavaElement element, CompilationUnitRewrite targetRewriter, CompilationUnit sourceCuNode, CompilationUnit targetCuNode) throws CoreException { final ASTRewrite rewrite= targetRewriter.getASTRewrite(); switch (element.getElementType()) { case IJavaElement.FIELD: copyMemberToDestination((IMember) element, targetRewriter, sourceCuNode, targetCuNode, createNewFieldDeclarationNode(((IField) element), rewrite, sourceCuNode)); break; case IJavaElement.IMPORT_CONTAINER: copyImportsToDestination((IImportContainer) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.IMPORT_DECLARATION: copyImportToDestination((IImportDeclaration) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.INITIALIZER: copyInitializerToDestination((IInitializer) element, targetRewriter, sourceCuNode, targetCuNode); break; case IJavaElement.METHOD: copyMethodToDestination((IMethod) element, targetRewriter, sourceCuNode, targetCuNode); break; case IJavaElement.PACKAGE_DECLARATION: copyPackageDeclarationToDestination((IPackageDeclaration) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.TYPE: copyTypeToDestination((IType) element, targetRewriter, sourceCuNode, targetCuNode); break; default: Assert.isTrue(false); } }
private static TypedSource[] createTypedSources(IJavaElement elem, SourceTuple tuple) throws CoreException { if (! ReorgUtils.isInsideCompilationUnit(elem)) return null; if (elem.getElementType() == IJavaElement.IMPORT_CONTAINER) return createTypedSourcesForImportContainer(tuple, (IImportContainer)elem); else if (elem.getElementType() == IJavaElement.FIELD) return new TypedSource[] {create(getFieldSource((IField)elem, tuple), elem.getElementType())}; return new TypedSource[] {create(getSourceOfDeclararationNode(elem, tuple.unit), elem.getElementType())}; }
private static TypedSource[] createTypedSourcesForImportContainer(SourceTuple tuple, IImportContainer container) throws JavaModelException, CoreException { IJavaElement[] imports= container.getChildren(); List<TypedSource> result= new ArrayList<TypedSource>(imports.length); for (int i= 0; i < imports.length; i++) { result.addAll(Arrays.asList(createTypedSources(imports[i], tuple))); } return result.toArray(new TypedSource[result.size()]); }
private Object[] removeImportAndPackageDeclarations(Object[] members) { ArrayList<Object> tempResult= new ArrayList<Object>(members.length); for (int i= 0; i < members.length; i++) if (!(members[i] instanceof IImportContainer) && !(members[i] instanceof IPackageDeclaration)) tempResult.add(members[i]); return tempResult.toArray(); }
/** * Finds the element which has to be selected in this part. * * @param je the Java element which has the focus * @return the element to select */ @Override protected IJavaElement findElementToSelect(IJavaElement je) { if (je == null) return null; switch (je.getElementType()) { case IJavaElement.TYPE: if (((IType)je).getDeclaringType() == null) return null; return je; case IJavaElement.METHOD: case IJavaElement.INITIALIZER: case IJavaElement.FIELD: case IJavaElement.PACKAGE_DECLARATION: case IJavaElement.IMPORT_CONTAINER: return je; case IJavaElement.IMPORT_DECLARATION: ICompilationUnit cu= (ICompilationUnit)je.getParent().getParent(); try { if (cu.getImports()[0].equals(je)) { Object selectedElement= getSingleElementFromSelection(getViewer().getSelection()); if (selectedElement instanceof IImportContainer) return (IImportContainer)selectedElement; } } catch (JavaModelException ex) { // return je; } return je; } return null; }
@Override public void visit(IJavaElement javaElement) { if (javaElement != null && !visited.contains(javaElement)) { if (javaElement instanceof IJavaProject) { visit((IJavaProject)javaElement); } else if (javaElement instanceof IJavaModel) { visit((IJavaModel)javaElement); } else if (javaElement instanceof IPackageFragmentRoot) { visit((IPackageFragmentRoot)javaElement); } else if (javaElement instanceof IPackageFragment) { visit((IPackageFragment)javaElement); } else if (javaElement instanceof IClassFile) { visit((IClassFile)javaElement); } else if (javaElement instanceof ICompilationUnit) { visit((ICompilationUnit)javaElement); } else if (javaElement instanceof IMember) { visit((IMember)javaElement); } else if (javaElement instanceof IAnnotation) { visit((IAnnotation)javaElement); } else if (javaElement instanceof IImportContainer) { visit((IImportContainer)javaElement); } else if (javaElement instanceof IImportDeclaration) { visit((IImportDeclaration)javaElement); } else if (javaElement instanceof IPackageDeclaration) { visit((IPackageDeclaration)javaElement); } else if (javaElement instanceof ITypeParameter) { visit((ITypeParameter)javaElement); } else if (javaElement instanceof ILocalVariable) { visit((ILocalVariable)javaElement); } else { CoreActivator.log(IStatus.ERROR, "Not handled !" + javaElement); } } }
public final IJavaElement transplantHandle(IJavaElement element) { IJavaElement parent = element.getParent(); if (parent != null) parent = transplantHandle(parent); // recursive switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: return transplantHandle((IJavaModel) element); case IJavaElement.JAVA_PROJECT: return transplantHandle((IJavaProject) element); case IJavaElement.PACKAGE_FRAGMENT_ROOT: return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element); case IJavaElement.PACKAGE_FRAGMENT: return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element); case IJavaElement.COMPILATION_UNIT: return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element); case IJavaElement.CLASS_FILE: return transplantHandle((IPackageFragment) parent, (IClassFile) element); case IJavaElement.TYPE: return transplantHandle(parent, (IType) element); case IJavaElement.FIELD: return transplantHandle((IType) parent, (IField) element); case IJavaElement.METHOD: return transplantHandle((IType) parent, (IMethod) element); case IJavaElement.INITIALIZER: return transplantHandle((IType) parent, (IInitializer) element); case IJavaElement.PACKAGE_DECLARATION: return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element); case IJavaElement.IMPORT_CONTAINER: return transplantHandle((ICompilationUnit) parent, (IImportContainer) element); case IJavaElement.IMPORT_DECLARATION: return transplantHandle((IImportContainer) parent, (IImportDeclaration) element); case IJavaElement.LOCAL_VARIABLE: return transplantHandle((ILocalVariable) element); case IJavaElement.TYPE_PARAMETER: return transplantHandle((IMember) parent, (ITypeParameter) element); case IJavaElement.ANNOTATION: return transplantHandle((IAnnotatable) parent, (IAnnotation) element); default: throw new IllegalArgumentException(element.toString()); } }
protected IImportContainer transplantHandle(ICompilationUnit parent, IImportContainer element) { return parent.getImportContainer(); }
protected IImportDeclaration transplantHandle( IImportContainer parent, IImportDeclaration element) { return parent.getImport(element.getElementName()); }
protected void copyToDestination( IJavaElement element, CompilationUnitRewrite targetRewriter, CompilationUnit sourceCuNode, CompilationUnit targetCuNode) throws CoreException { final ASTRewrite rewrite = targetRewriter.getASTRewrite(); switch (element.getElementType()) { case IJavaElement.FIELD: copyMemberToDestination( (IMember) element, targetRewriter, sourceCuNode, targetCuNode, createNewFieldDeclarationNode(((IField) element), rewrite, sourceCuNode)); break; case IJavaElement.IMPORT_CONTAINER: copyImportsToDestination((IImportContainer) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.IMPORT_DECLARATION: copyImportToDestination( (IImportDeclaration) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.INITIALIZER: copyInitializerToDestination( (IInitializer) element, targetRewriter, sourceCuNode, targetCuNode); break; case IJavaElement.METHOD: copyMethodToDestination((IMethod) element, targetRewriter, sourceCuNode, targetCuNode); break; case IJavaElement.PACKAGE_DECLARATION: copyPackageDeclarationToDestination( (IPackageDeclaration) element, rewrite, sourceCuNode, targetCuNode); break; case IJavaElement.TYPE: copyTypeToDestination((IType) element, targetRewriter, sourceCuNode, targetCuNode); break; default: Assert.isTrue(false); } }
/** * {@inheritDoc} */ @Override public IImportContainer getImportContainer() { return compilationUnit.getImportContainer(); }
public final IJavaElement transplantHandle(IJavaElement element) { IJavaElement parent= element.getParent(); if (parent != null) parent= transplantHandle(parent); // recursive switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: return transplantHandle((IJavaModel) element); case IJavaElement.JAVA_PROJECT: return transplantHandle((IJavaProject) element); case IJavaElement.PACKAGE_FRAGMENT_ROOT: return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element); case IJavaElement.PACKAGE_FRAGMENT: return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element); case IJavaElement.COMPILATION_UNIT: return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element); case IJavaElement.CLASS_FILE: return transplantHandle((IPackageFragment) parent, (IClassFile) element); case IJavaElement.TYPE: return transplantHandle(parent, (IType) element); case IJavaElement.FIELD: return transplantHandle((IType) parent, (IField) element); case IJavaElement.METHOD: return transplantHandle((IType) parent, (IMethod) element); case IJavaElement.INITIALIZER: return transplantHandle((IType) parent, (IInitializer) element); case IJavaElement.PACKAGE_DECLARATION: return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element); case IJavaElement.IMPORT_CONTAINER: return transplantHandle((ICompilationUnit) parent, (IImportContainer) element); case IJavaElement.IMPORT_DECLARATION: return transplantHandle((IImportContainer) parent, (IImportDeclaration) element); case IJavaElement.LOCAL_VARIABLE: return transplantHandle((ILocalVariable) element); case IJavaElement.TYPE_PARAMETER: return transplantHandle((IMember) parent, (ITypeParameter) element); case IJavaElement.ANNOTATION: return transplantHandle((IAnnotatable) parent, (IAnnotation) element); default: throw new IllegalArgumentException(element.toString()); } }
protected IImportDeclaration transplantHandle(IImportContainer parent, IImportDeclaration element) { return parent.getImport(element.getElementName()); }
/** * Computes and returns the source reference that includes the caret and * serves as provider for the outline page selection and the editor range * indication. * * @return the computed source reference * @since 3.0 */ protected ISourceReference computeHighlightRangeSourceReference() { ISourceViewer sourceViewer= getSourceViewer(); if (sourceViewer == null) return null; StyledText styledText= sourceViewer.getTextWidget(); if (styledText == null) return null; int caret= 0; if (sourceViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension= (ITextViewerExtension5)sourceViewer; caret= extension.widgetOffset2ModelOffset(styledText.getCaretOffset()); } else { int offset= sourceViewer.getVisibleRegion().getOffset(); caret= offset + styledText.getCaretOffset(); } IJavaElement element= getElementAt(caret, false); if ( !(element instanceof ISourceReference)) return null; if (element.getElementType() == IJavaElement.IMPORT_DECLARATION) { IImportDeclaration declaration= (IImportDeclaration) element; IImportContainer container= (IImportContainer) declaration.getParent(); ISourceRange srcRange= null; try { srcRange= container.getSourceRange(); } catch (JavaModelException e) { } if (srcRange != null && srcRange.getOffset() == caret) return container; } return (ISourceReference) element; }
@Override public boolean select(Viewer viewer, Object parent, Object element) { return !((element instanceof IImportContainer) || (element instanceof IImportDeclaration)); }
public IImportContainer getImportContainer() { return new AssistImportContainer(this, this.infoCache); }