private List<Initializer> getInitializes(Object parent) throws JavaModelException { List<Initializer> result = new ArrayList<>(); Set<Object> objects = childrens.get(parent); if (objects == null) { return result; } for (Object object : objects) { if (object instanceof IInitializer) { IInitializer initializer = (IInitializer) object; Initializer init = DtoFactory.newDto(Initializer.class); init.setElementName(initializer.getElementName()); init.setHandleIdentifier(initializer.getHandleIdentifier()); init.setFlags(initializer.getFlags()); init.setLabel( JavaElementLabels.getElementLabel(initializer, JavaElementLabels.ALL_DEFAULT)); result.add(init); } } return result; }
/** * Creates a new java element visibility adjustor. * * @param referencing the referencing element used to compute the visibility * @param referenced the referenced member which causes the visibility changes */ public MemberVisibilityAdjustor(final IJavaElement referencing, final IMember referenced) { Assert.isTrue(!(referenced instanceof IInitializer)); Assert.isTrue( referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment); fScope = RefactoringScopeFactory.createReferencedScope( new IJavaElement[] {referenced}, IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES); fReferencing = referencing; fReferenced = referenced; }
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; } }
protected IType transplantHandle(IJavaElement parent, IType element) { switch (parent.getElementType()) { case IJavaElement.COMPILATION_UNIT: return ((ICompilationUnit) parent).getType(element.getElementName()); case IJavaElement.CLASS_FILE: return ((IClassFile) parent).getType(); case IJavaElement.METHOD: return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.FIELD: return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.INITIALIZER: return ((IInitializer) parent) .getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.TYPE: return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount()); default: throw new IllegalStateException(element.toString()); } }
/** * Appends the label for a initializer. Considers the I_* flags. * * @param initializer the element to render * @param flags the rendering flags. Flags with names starting with 'I_' are considered. */ public void appendInitializerLabel(IInitializer initializer, long flags) { // qualification if (getFlag(flags, JavaElementLabels.I_FULLY_QUALIFIED)) { appendTypeLabel( initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuffer.append('.'); } fBuffer.append(JavaUIMessages.JavaElementLabels_initializer); // post qualification if (getFlag(flags, JavaElementLabels.I_POST_QUALIFIED)) { int offset = fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); appendTypeLabel( initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } } }
/** Returns the IInitializer that contains the given local variable in the given type */ public static JavaElement getUnresolvedJavaElement( int localSourceStart, int localSourceEnd, JavaElement type) { try { if (!(type instanceof IType)) return null; IInitializer[] initializers = ((IType) type).getInitializers(); for (int i = 0; i < initializers.length; i++) { IInitializer initializer = initializers[i]; ISourceRange sourceRange = initializer.getSourceRange(); if (sourceRange != null) { int initializerStart = sourceRange.getOffset(); int initializerEnd = initializerStart + sourceRange.getLength(); if (initializerStart <= localSourceStart && localSourceEnd <= initializerEnd) { return (JavaElement) initializer; } } } return null; } catch (JavaModelException e) { return null; } }
/** * Appends the label for a initializer. Considers the I_* flags. * * @param initializer the element to render * @param flags the rendering flags. Flags with names starting with 'I_' are considered. */ public void appendInitializerLabel(IInitializer initializer, long flags) { // qualification if (getFlag(flags, I_FULLY_QUALIFIED)) { appendTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuffer.append('.'); } fBuffer.append("{...}"); // post qualification if (getFlag(flags, I_POST_QUALIFIED)) { int offset= fBuffer.length(); fBuffer.append(CONCAT_STRING); appendTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); } }
private RefactoringStatus checkMovedMemberAvailability(IMember memberToMove, IProgressMonitor pm) throws JavaModelException{ RefactoringStatus result= new RefactoringStatus(); if (memberToMove instanceof IType) { // recursively check accessibility of member type's members IJavaElement[] typeMembers= ((IType) memberToMove).getChildren(); pm.beginTask(RefactoringCoreMessages.MoveMembersRefactoring_checking, typeMembers.length + 1); for (int i= 0; i < typeMembers.length; i++) { if (typeMembers[i] instanceof IInitializer) pm.worked(1); else result.merge(checkMovedMemberAvailability((IMember) typeMembers[i], new SubProgressMonitor(pm, 1))); } } else { pm.beginTask(RefactoringCoreMessages.MoveMembersRefactoring_checking, 1); } IType[] blindAccessorTypes= getTypesNotSeeingMovedMember(memberToMove, new SubProgressMonitor(pm, 1), result); for (int k= 0; k < blindAccessorTypes.length; k++) { String message= createNonAccessibleMemberMessage(memberToMove, blindAccessorTypes[k],/*moved*/true); result.addError(message, JavaStatusContext.create(memberToMove)); } pm.done(); 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; } }
protected IType transplantHandle(IJavaElement parent, IType element) { switch (parent.getElementType()) { case IJavaElement.COMPILATION_UNIT: return ((ICompilationUnit) parent).getType(element.getElementName()); case IJavaElement.CLASS_FILE: return ((IClassFile) parent).getType(); case IJavaElement.METHOD: return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.FIELD: return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.INITIALIZER: return ((IInitializer) parent).getType(element.getElementName(), element.getOccurrenceCount()); case IJavaElement.TYPE: return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount()); default: throw new IllegalStateException(element.toString()); } }
private ASTNode getDestinationNode(IJavaElement destination, CompilationUnit target) throws JavaModelException { switch (destination.getElementType()) { case IJavaElement.INITIALIZER: return ASTNodeSearchUtil.getInitializerNode((IInitializer) destination, target); case IJavaElement.FIELD: return ASTNodeSearchUtil.getFieldOrEnumConstantDeclaration((IField) destination, target); case IJavaElement.METHOD: return ASTNodeSearchUtil.getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) destination, target); case IJavaElement.TYPE: IType typeDestination= (IType) destination; if (typeDestination.isAnonymous()) { return ASTNodeSearchUtil.getClassInstanceCreationNode(typeDestination, target).getAnonymousClassDeclaration(); } else { return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(typeDestination, target); } case IJavaElement.COMPILATION_UNIT: IType mainType= JavaElementUtil.getMainType((ICompilationUnit) destination); if (mainType != null) { return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(mainType, target); } //$FALL-THROUGH$ default: return null; } }
private static boolean isPotentialNLSAccessor(ICompilationUnit unit) throws JavaModelException { IType type= unit.getTypes()[0]; if (!type.exists()) return false; IField bundleNameField= getBundleNameField(type.getFields()); if (bundleNameField == null) return false; if (!importsOSGIUtil(unit)) return false; IInitializer[] initializers= type.getInitializers(); for (int i= 0; i < initializers.length; i++) { if (Modifier.isStatic(initializers[0].getFlags())) return true; } return false; }
private static boolean isPotentialNLSAccessor(ICompilationUnit unit) throws JavaModelException { IType type= unit.getTypes()[0]; if (!type.exists()) return false; IField bundleNameField= getBundleNameField(type.getFields()); if (bundleNameField == null) return false; if (importsOSGIUtil(unit)) { //new school IInitializer[] initializers= type.getInitializers(); for (int i= 0; i < initializers.length; i++) { if (Modifier.isStatic(initializers[0].getFlags())) return true; } } else { //old school IMethod[] methods= type.getMethods(); for (int i= 0; i < methods.length; i++) { IMethod method= methods[i]; if (isValueAccessor(method)) return true; } } return false; }
/** * Appends the label for a initializer. Considers the I_* flags. * * @param initializer the element to render * @param flags the rendering flags. Flags with names starting with 'I_' are considered. */ public void appendInitializerLabel(IInitializer initializer, long flags) { // qualification if (getFlag(flags, JavaElementLabels.I_FULLY_QUALIFIED)) { appendTypeLabel(initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuffer.append('.'); } fBuffer.append(JavaUIMessages.JavaElementLabels_initializer); // post qualification if (getFlag(flags, JavaElementLabels.I_POST_QUALIFIED)) { int offset= fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); appendTypeLabel(initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } } }
public static IMember getMemberFromOffset(ITextEditor javaEditor, int offset) throws JavaModelException { ITypeRoot element= JavaUI.getEditorInputTypeRoot(javaEditor.getEditorInput()); CompilationUnit ast = SharedASTProvider.getAST(element, SharedASTProvider.WAIT_YES, null); NodeFinder finder= new NodeFinder(ast, offset, 0); ASTNode node= finder.getCoveringNode(); while(node != null) { if(node instanceof Initializer) { TypeDeclaration typeDeclaration = (TypeDeclaration) node.getParent(); IType type = (IType) typeDeclaration.resolveBinding().getJavaElement(); for(IInitializer initializer : type.getInitializers()) { if(node.getStartPosition() == initializer.getSourceRange().getOffset() && Flags.isStatic(initializer.getFlags())) { return initializer; } } } if(node instanceof MethodDeclaration) { IMethodBinding binding = ((MethodDeclaration) node).resolveBinding(); return (IMethod) binding.getJavaElement(); } node = node.getParent(); } return null; }
/** * Returns {@code IMethod} if the current selection is a method. When the selection is a static initializer returns {@code IInitializer}. * If it is neither of them, returns null. * @param event an {@code ExecutionEvent} * @return {@code IMethod} or {@code IInitializer} * @throws JavaModelException */ protected IMember getMember(ExecutionEvent event) throws JavaModelException { ISelection selection = getSelection(event); if(selection instanceof ITextSelection) { ITextEditor editor = (ITextEditor) getEditor(event); ITextSelection textSelection = (ITextSelection) editor.getSelectionProvider().getSelection(); return JavaUtils.getMemberFromOffset(editor, textSelection.getOffset()); } else { IStructuredSelection structuredSelection = (IStructuredSelection) selection; IMember member = (IMember) structuredSelection.getFirstElement(); if(member instanceof IInitializer && !Flags.isStatic(((IInitializer) member).getFlags())) { return null; } return member; } }
/** * Appends the label for a initializer. Considers the I_* flags. * * @param initializer the element to render * @param flags the rendering flags. Flags with names starting with 'I_' are considered. */ public void appendInitializerLabel(IInitializer initializer, long flags) { // qualification if (getFlag(flags, JavaElementLabels.I_FULLY_QUALIFIED)) { appendTypeLabel(initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuilder.append('.'); } fBuilder.append("{...}"); // post qualification if (getFlag(flags, JavaElementLabels.I_POST_QUALIFIED)) { fBuilder.append(JavaElementLabels.CONCAT_STRING); appendTypeLabel(initializer.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); } }
private Initializer convertToDTOInitializer(IInitializer iInitializer) throws JavaModelException { Initializer initializer = DtoFactory.newDto(Initializer.class); initializer.setFileRegion(convertToDTORegion(iInitializer.getSourceRange())); initializer.setElementName(iInitializer.getElementName()); initializer.setHandleIdentifier(iInitializer.getHandleIdentifier()); initializer.setFlags(iInitializer.getFlags()); initializer.setLabel( org.eclipse.jdt.ui.JavaElementLabels.getElementLabel( iInitializer, org.eclipse.jdt.ui.JavaElementLabels.ALL_DEFAULT)); return initializer; }
/** * Creates a new incoming member visibility adjustment. * * @param member the member which is adjusted * @param keyword the keyword representing the adjusted visibility * @param status the associated status, or <code>null</code> */ public IncomingMemberVisibilityAdjustment( final IMember member, final ModifierKeyword keyword, final RefactoringStatus status) { Assert.isNotNull(member); Assert.isTrue(!(member instanceof IInitializer)); Assert.isTrue(isVisibilityKeyword(keyword)); fMember = member; fKeyword = keyword; fRefactoringStatus = status; }
/** * Check whether anyone accesses the members of the moved type from the outside. Those may need to * have their visibility adjusted. * * @param member the member * @param monitor the progress monitor to use * @throws JavaModelException if an error occurs */ private void adjustMemberVisibility(final IMember member, final IProgressMonitor monitor) throws JavaModelException { if (member instanceof IType) { // recursively check accessibility of member type's members final IJavaElement[] typeMembers = ((IType) member).getChildren(); for (int i = 0; i < typeMembers.length; i++) { if (!(typeMembers[i] instanceof IInitializer)) adjustMemberVisibility((IMember) typeMembers[i], monitor); } } if (member.equals(fReferenced) || Modifier.isPublic(member.getFlags())) return; final SearchResultGroup[] references = findReferences(member, monitor); for (int i = 0; i < references.length; i++) { final SearchMatch[] searchResults = references[i].getSearchResults(); for (int k = 0; k < searchResults.length; k++) { final IJavaElement referenceToMember = (IJavaElement) searchResults[k].getElement(); if (fAdjustments.get(member) == null && referenceToMember instanceof IMember && !isInsideMovedMember(referenceToMember)) { // check whether the member is still visible from the // destination. As we are moving a type, the destination is // a package or another type. adjustIncomingVisibility(fReferencing, member, new SubProgressMonitor(monitor, 1)); } } } }
public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException { ASTNode node = findNode(initializer.getSourceRange(), cuNode); if (node instanceof Initializer) return (Initializer) node; if (node instanceof Block && node.getParent() instanceof Initializer) return (Initializer) node.getParent(); return null; }
private void copyInitializerToDestination( IInitializer initializer, CompilationUnitRewrite targetRewriter, CompilationUnit sourceCuNode, CompilationUnit targetCuNode) throws JavaModelException { BodyDeclaration newInitializer = (BodyDeclaration) targetRewriter .getASTRewrite() .createStringPlaceholder(getUnindentedSource(initializer), ASTNode.INITIALIZER); copyMemberToDestination( initializer, targetRewriter, sourceCuNode, targetCuNode, newInitializer); }
private ASTNode getDestinationNode(IJavaElement destination, CompilationUnit target) throws JavaModelException { switch (destination.getElementType()) { case IJavaElement.INITIALIZER: return ASTNodeSearchUtil.getInitializerNode((IInitializer) destination, target); case IJavaElement.FIELD: return ASTNodeSearchUtil.getFieldOrEnumConstantDeclaration((IField) destination, target); case IJavaElement.METHOD: return ASTNodeSearchUtil.getMethodOrAnnotationTypeMemberDeclarationNode( (IMethod) destination, target); case IJavaElement.TYPE: IType typeDestination = (IType) destination; if (typeDestination.isAnonymous()) { return ASTNodeSearchUtil.getClassInstanceCreationNode(typeDestination, target) .getAnonymousClassDeclaration(); } else { return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(typeDestination, target); } case IJavaElement.COMPILATION_UNIT: IType mainType = JavaElementUtil.getMainType((ICompilationUnit) destination); if (mainType != null) { return ASTNodeSearchUtil.getAbstractTypeDeclarationNode(mainType, target); } // $FALL-THROUGH$ default: return null; } }
protected static String createLabel(final IMember member) { if (member instanceof IType) return JavaElementLabels.getTextLabel(member, JavaElementLabels.ALL_FULLY_QUALIFIED); else if (member instanceof IMethod) return JavaElementLabels.getTextLabel(member, JavaElementLabels.ALL_FULLY_QUALIFIED); else if (member instanceof IField) return JavaElementLabels.getTextLabel(member, JavaElementLabels.ALL_FULLY_QUALIFIED); else if (member instanceof IInitializer) return RefactoringCoreMessages.HierarchyRefactoring_initializer; Assert.isTrue(false); return null; }
/** * Creates a new incoming member visibility adjustment. * * @param member the member which is adjusted * @param keyword the keyword representing the adjusted visibility * @param status the associated status, or <code>null</code> */ public IncomingMemberVisibilityAdjustment(final IMember member, final ModifierKeyword keyword, final RefactoringStatus status) { Assert.isNotNull(member); Assert.isTrue(!(member instanceof IInitializer)); Assert.isTrue(isVisibilityKeyword(keyword)); fMember= member; fKeyword= keyword; fRefactoringStatus= status; }
/** * Creates a new java element visibility adjustor. * * @param referencing the referencing element used to compute the visibility * @param referenced the referenced member which causes the visibility changes */ public MemberVisibilityAdjustor(final IJavaElement referencing, final IMember referenced) { Assert.isTrue(!(referenced instanceof IInitializer)); Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment); fScope= RefactoringScopeFactory.createReferencedScope(new IJavaElement[] { referenced}, IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES); fReferencing= referencing; fReferenced= referenced; }
/** * Check whether anyone accesses the members of the moved type from the * outside. Those may need to have their visibility adjusted. * @param member the member * @param monitor the progress monitor to use * @throws JavaModelException if an error occurs */ private void adjustMemberVisibility(final IMember member, final IProgressMonitor monitor) throws JavaModelException { if (member instanceof IType) { // recursively check accessibility of member type's members final IJavaElement[] typeMembers= ((IType) member).getChildren(); for (int i= 0; i < typeMembers.length; i++) { if (! (typeMembers[i] instanceof IInitializer)) adjustMemberVisibility((IMember) typeMembers[i], monitor); } } if (member.equals(fReferenced) || Modifier.isPublic(member.getFlags())) return; final SearchResultGroup[] references= findReferences(member, monitor); for (int i= 0; i < references.length; i++) { final SearchMatch[] searchResults= references[i].getSearchResults(); for (int k= 0; k < searchResults.length; k++) { final IJavaElement referenceToMember= (IJavaElement) searchResults[k].getElement(); if (fAdjustments.get(member) == null && referenceToMember instanceof IMember && !isInsideMovedMember(referenceToMember)) { // check whether the member is still visible from the // destination. As we are moving a type, the destination is // a package or another type. adjustIncomingVisibility(fReferencing, member, new SubProgressMonitor(monitor, 1)); } } } }
public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException { ASTNode node= findNode(initializer.getSourceRange(), cuNode); if (node instanceof Initializer) return (Initializer) node; if (node instanceof Block && node.getParent() instanceof Initializer) return (Initializer) node.getParent(); return null; }
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 IJavaElement getEnclosingMethod(ITypeRoot input, ITextSelection selection) { try { IJavaElement enclosingElement= input.getElementAt(selection.getOffset()); if (enclosingElement instanceof IMethod || enclosingElement instanceof IInitializer || enclosingElement instanceof IField) { // opening on the enclosing type would be too confusing (since the type resolves to the constructors) return enclosingElement; } } catch (JavaModelException e) { JavaPlugin.log(e); } return null; }
private static int getOffset(IMember iMember) throws JavaModelException { //special case if (iMember.getElementType() == IJavaElement.INITIALIZER) return firstOpeningBraceOffset((IInitializer)iMember); if (iMember.getNameRange() != null && iMember.getNameRange().getOffset() >= 0) return iMember.getNameRange().getOffset(); return iMember.getSourceRange().getOffset(); }
private static int firstOpeningBraceOffset(IInitializer iInitializer) throws JavaModelException { try { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(iInitializer.getSource().toCharArray()); int token= scanner.getNextToken(); while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLBRACE) token= scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameLBRACE) return iInitializer.getSourceRange().getOffset() + scanner.getCurrentTokenStartPosition() + scanner.getRawTokenSource().length; return iInitializer.getSourceRange().getOffset(); } catch (InvalidInputException e) { return iInitializer.getSourceRange().getOffset(); } }
public ASTNode search() throws JavaModelException { ISourceRange range = null; if (this.element instanceof IMember && !(this.element instanceof IInitializer)) range = ((IMember) this.element).getNameRange(); else range = this.element.getSourceRange(); this.rangeStart = range.getOffset(); this.rangeLength = range.getLength(); this.ast.accept(this); return this.foundNode; }