private static int getNodeStartPosition(final ASTNode node) { if (node instanceof MethodDeclaration) { MethodDeclaration decl = (MethodDeclaration) node; return decl.isConstructor() ? decl.getName().getStartPosition() : decl.getReturnType2().getStartPosition(); } else if (node instanceof FieldDeclaration) { return ((FieldDeclaration) node).getType().getStartPosition(); } else if (node instanceof AbstractTypeDeclaration) { return ((AbstractTypeDeclaration) node).getName().getStartPosition(); } else if (node instanceof AnnotationTypeMemberDeclaration) { return ((AnnotationTypeMemberDeclaration) node).getName().getStartPosition(); } else if (node instanceof EnumConstantDeclaration) { return ((EnumConstantDeclaration) node).getName().getStartPosition(); } else if (node instanceof PackageDeclaration) { return ((PackageDeclaration) node).getName().getStartPosition(); } /* TODO: Initializer */ return node.getStartPosition(); }
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) { switch (declNode.getNodeType()) { case ASTNode.METHOD_DECLARATION: return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY); case ASTNode.FIELD_DECLARATION: return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY); case ASTNode.VARIABLE_DECLARATION_EXPRESSION: return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY); case ASTNode.VARIABLE_DECLARATION_STATEMENT: return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY); case ASTNode.SINGLE_VARIABLE_DECLARATION: return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY); case ASTNode.TYPE_DECLARATION: return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ENUM_DECLARATION: return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ANNOTATION_TYPE_DECLARATION: return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ENUM_CONSTANT_DECLARATION: return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY); default: throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$ } }
@Override public boolean visit(AnnotationTypeMemberDeclaration node) { if (node.getJavadoc() != null) { node.getJavadoc().accept(this); } printModifiers(node.modifiers()); node.getType().accept(this); this.fBuffer.append(" ");//$NON-NLS-1$ node.getName().accept(this); this.fBuffer.append("()");//$NON-NLS-1$ if (node.getDefault() != null) { this.fBuffer.append(" default ");//$NON-NLS-1$ node.getDefault().accept(this); } this.fBuffer.append(";");//$NON-NLS-1$ return false; }
public boolean visit(AnnotationTypeMemberDeclaration node) { if (node.getJavadoc() != null) { node.getJavadoc().accept(this); } printIndent(); printModifiers(node.modifiers()); node.getType().accept(this); this.buffer.append(" ");//$NON-NLS-1$ node.getName().accept(this); this.buffer.append("()");//$NON-NLS-1$ if (node.getDefault() != null) { this.buffer.append(" default ");//$NON-NLS-1$ node.getDefault().accept(this); } this.buffer.append(";\n");//$NON-NLS-1$ return false; }
@Override public void endVisit(AnnotationTypeMemberDeclaration node) { if (skipNode(node)) { return; } GenericSequentialFlowInfo info = processSequential(node, node.getType(), node.getDefault()); info.setNoReturn(); }
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode); ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding); ASTNode newTypeDecl= null; if (typeDecl != null) { newTypeDecl= typeDecl; } else { astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey()); } createImportRewrite(astRoot); if (newTypeDecl instanceof AnnotationTypeDeclaration) { AnnotationTypeDeclaration newAnnotationTypeDecl= (AnnotationTypeDeclaration) newTypeDecl; ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); AnnotationTypeMemberDeclaration newStub= getStub(rewrite, newAnnotationTypeDecl); List<BodyDeclaration> members= newAnnotationTypeDecl.bodyDeclarations(); int insertIndex= members.size(); ListRewrite listRewriter= rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY); listRewriter.insertAt(newStub, insertIndex, null); return rewrite; } return null; }
private int evaluateModifiers(AnnotationTypeDeclaration targetTypeDecl) { List<BodyDeclaration> methodDecls= targetTypeDecl.bodyDeclarations(); for (int i= 0; i < methodDecls.size(); i++) { Object curr= methodDecls.get(i); if (curr instanceof AnnotationTypeMemberDeclaration) { return ((AnnotationTypeMemberDeclaration) curr).getModifiers(); } } return 0; }
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) { switch (declNode.getNodeType()) { case ASTNode.METHOD_DECLARATION: return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY); case ASTNode.FIELD_DECLARATION: return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY); case ASTNode.VARIABLE_DECLARATION_EXPRESSION: return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY); case ASTNode.VARIABLE_DECLARATION_STATEMENT: return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY); case ASTNode.SINGLE_VARIABLE_DECLARATION: return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY); case ASTNode.TYPE_DECLARATION: return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ENUM_DECLARATION: return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ANNOTATION_TYPE_DECLARATION: return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ENUM_CONSTANT_DECLARATION: return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY); case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: return rewrite.getListRewrite( declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY); default: throw new IllegalArgumentException( "node has no modifiers: " + declNode.getClass().getName()); // $NON-NLS-1$ } }
public boolean resolveInVariableInitializer() { if (fInVariableInitializerRequested) return fInVariableInitializer; fInVariableInitializerRequested = true; resolveSelectedNodes(); ASTNode node = getStartNode(); ASTNode last = null; while (node != null) { int nodeType = node.getNodeType(); if (node instanceof AbstractTypeDeclaration) { fInVariableInitializer = false; break; } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) { fInVariableInitializer = false; break; } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment) node).getInitializer() == last) { fInVariableInitializer = true; break; } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration) node).getInitializer() == last) { fInVariableInitializer = true; break; } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration) node).getDefault() == last) { fInVariableInitializer = true; break; } last = node; node = node.getParent(); } return fInVariableInitializer; }
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fInvocationNode); ASTNode typeDecl = astRoot.findDeclaringNode(fSenderBinding); ASTNode newTypeDecl = null; if (typeDecl != null) { newTypeDecl = typeDecl; } else { astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey()); } createImportRewrite(astRoot); if (newTypeDecl instanceof AnnotationTypeDeclaration) { AnnotationTypeDeclaration newAnnotationTypeDecl = (AnnotationTypeDeclaration) newTypeDecl; ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AnnotationTypeMemberDeclaration newStub = getStub(rewrite, newAnnotationTypeDecl); List<BodyDeclaration> members = newAnnotationTypeDecl.bodyDeclarations(); int insertIndex = members.size(); ListRewrite listRewriter = rewrite.getListRewrite( newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY); listRewriter.insertAt(newStub, insertIndex, null); return rewrite; } return null; }
private int evaluateModifiers(AnnotationTypeDeclaration targetTypeDecl) { List<BodyDeclaration> methodDecls = targetTypeDecl.bodyDeclarations(); for (int i = 0; i < methodDecls.size(); i++) { Object curr = methodDecls.get(i); if (curr instanceof AnnotationTypeMemberDeclaration) { return ((AnnotationTypeMemberDeclaration) curr).getModifiers(); } } return 0; }
@Override public boolean visit(AnnotationTypeMemberDeclaration node) { AnnotationTypeAttribute attribute = importer.ensureAnnotationTypeAttributeFromDeclaration(node); attribute.setIsStub(false); importer.ensureCommentFromBodyDeclaration(attribute, node); return super.visit(node); }
public AnnotationTypeAttribute ensureAnnotationTypeAttributeFromDeclaration( AnnotationTypeMemberDeclaration node) { IMethodBinding binding = node.resolveBinding(); if (binding != null) return ensureAnnotationTypeAttributeFromBinding(binding); return new AnnotationTypeAttribute(); }
@Override public void endVisit(AnnotationTypeMemberDeclaration node) { if (skipNode(node)) return; GenericSequentialFlowInfo info= processSequential(node, node.getType(), node.getDefault()); info.setNoReturn(); }
public boolean resolveInVariableInitializer() { if (fInVariableInitializerRequested) return fInVariableInitializer; fInVariableInitializerRequested= true; resolveSelectedNodes(); ASTNode node= getStartNode(); ASTNode last= null; while (node != null) { int nodeType= node.getNodeType(); if (node instanceof AbstractTypeDeclaration) { fInVariableInitializer= false; break; } else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) { fInVariableInitializer= false; break; } else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT && ((VariableDeclarationFragment)node).getInitializer() == last) { fInVariableInitializer= true; break; } else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION && ((SingleVariableDeclaration)node).getInitializer() == last) { fInVariableInitializer= true; break; } else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION && ((AnnotationTypeMemberDeclaration)node).getDefault() == last) { fInVariableInitializer= true; break; } last= node; node= node.getParent(); } return fInVariableInitializer; }
private String getSignature(AnnotationTypeMemberDeclaration node) { StringBuffer buffer= new StringBuffer(); buffer.append(node.getName().toString()); buffer.append('('); buffer.append(')'); return buffer.toString(); }
@Override protected ASTRewrite getRewrite() throws CoreException { CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode); ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding); ASTNode newTypeDecl= null; if (typeDecl != null) { newTypeDecl= typeDecl; } else { astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey()); } createImportRewrite(astRoot); if (newTypeDecl instanceof AnnotationTypeDeclaration) { ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); AnnotationTypeMemberDeclaration newStub= getStub(rewrite, (AnnotationTypeDeclaration) newTypeDecl); ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl); List<? extends ASTNode> members= (List<? extends ASTNode>) newTypeDecl.getStructuralProperty(property); int insertIndex= members.size(); ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property); listRewriter.insertAt(newStub, insertIndex, null); return rewrite; } return null; }
@Override public boolean visit(AnnotationTypeMemberDeclaration node) { AbstractAnnotationMember annoMember = new AbstractAnnotationMember( node.getType().resolveBinding().getQualifiedName(), node.getName().getIdentifier()); annoMember.setDefaultExpression(node.getDefault() != null ? node.getDefault().toString() : null); annotationMembers.add(annoMember); return true; }
public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode( IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { return (AnnotationTypeMemberDeclaration) ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class); }
private ITypeBinding[] resolveBindings( String[] types, RefactoringStatus[] results, boolean firstPass) throws CoreException { // TODO: split types into parameterTypes and returnType int parameterCount = types.length - 1; ITypeBinding[] typeBindings = new ITypeBinding[types.length]; StringBuffer cuString = new StringBuffer(); cuString.append(fStubTypeContext.getBeforeString()); int offsetBeforeMethodName = appendMethodDeclaration(cuString, types, parameterCount); cuString.append(fStubTypeContext.getAfterString()); // need a working copy to tell the parser where to resolve (package visible) types ICompilationUnit wc = fMethod .getCompilationUnit() .getWorkingCopy( new WorkingCopyOwner() { /*subclass*/ }, new NullProgressMonitor()); try { wc.getBuffer().setContents(cuString.toString()); CompilationUnit compilationUnit = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true); ASTNode method = NodeFinder.perform(compilationUnit, offsetBeforeMethodName, METHOD_NAME.length()) .getParent(); Type[] typeNodes = new Type[types.length]; if (method instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) method; typeNodes[parameterCount] = methodDeclaration.getReturnType2(); List<SingleVariableDeclaration> parameters = methodDeclaration.parameters(); for (int i = 0; i < parameterCount; i++) typeNodes[i] = parameters.get(i).getType(); } else if (method instanceof AnnotationTypeMemberDeclaration) { typeNodes[0] = ((AnnotationTypeMemberDeclaration) method).getType(); } for (int i = 0; i < types.length; i++) { Type type = typeNodes[i]; if (type == null) { String msg = Messages.format( RefactoringCoreMessages.TypeContextChecker_couldNotResolveType, BasicElementLabels.getJavaElementName(types[i])); results[i] = RefactoringStatus.createErrorStatus(msg); continue; } results[i] = new RefactoringStatus(); IProblem[] problems = ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS); if (problems.length > 0) { for (int p = 0; p < problems.length; p++) if (isError(problems[p], type)) results[i].addError(problems[p].getMessage()); } ITypeBinding binding = handleBug84585(type.resolveBinding()); if (firstPass && (binding == null || binding.isRecovered())) { types[i] = qualifyTypes(type, results[i]); } typeBindings[i] = binding; } return typeBindings; } finally { wc.discardWorkingCopy(); } }
private AnnotationTypeMemberDeclaration getStub( ASTRewrite rewrite, AnnotationTypeDeclaration targetTypeDecl) { AST ast = targetTypeDecl.getAST(); AnnotationTypeMemberDeclaration decl = ast.newAnnotationTypeMemberDeclaration(); SimpleName newNameNode = getNewName(rewrite); decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateModifiers(targetTypeDecl))); ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( getLinkedProposalModel(), rewrite, decl.modifiers(), true); decl.setName(newNameNode); Type returnType = getNewType(rewrite); decl.setType(returnType); return decl; }
@Override public boolean consumes(SemanticToken token) { StructuralPropertyDescriptor location = token.getNode().getLocationInParent(); return location == MethodDeclaration.NAME_PROPERTY || location == AnnotationTypeMemberDeclaration.NAME_PROPERTY; }
/** {@inheritDoc} */ @Override public void endVisit(AnnotationTypeMemberDeclaration node) { logger.warn("Method endVisitAnnotationTypeMemberDeclaration for " + node + " for " + node + " not implemented!"); super.endVisit(node); }
/** {@inheritDoc} */ @Override public boolean visit(AnnotationTypeMemberDeclaration node) { logger.warn("Method visitAnnotationTypeMemberDeclaration for " + node + " for " + node + " not implemented!"); return super.visit(node); }
@Override public boolean visit(final AnnotationTypeMemberDeclaration node) { return false; }
@Override public boolean visit(final AnnotationTypeMemberDeclaration node) { // will never be visited assert false; return false; }
@Override public void endVisit(AnnotationTypeMemberDeclaration node) { super.endVisit(node); }
public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException { return (AnnotationTypeMemberDeclaration) ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class); }
@Override public boolean visit(AnnotationTypeMemberDeclaration node) { if (node.subtreeMatch(fMatcher, fNodeToMatch)) return matches(node); return super.visit(node); }
private ITypeBinding[] resolveBindings(String[] types, RefactoringStatus[] results, boolean firstPass) throws CoreException { //TODO: split types into parameterTypes and returnType int parameterCount= types.length - 1; ITypeBinding[] typeBindings= new ITypeBinding[types.length]; StringBuffer cuString= new StringBuffer(); cuString.append(fStubTypeContext.getBeforeString()); int offsetBeforeMethodName= appendMethodDeclaration(cuString, types, parameterCount); cuString.append(fStubTypeContext.getAfterString()); // need a working copy to tell the parser where to resolve (package visible) types ICompilationUnit wc= fMethod.getCompilationUnit().getWorkingCopy(new WorkingCopyOwner() {/*subclass*/}, new NullProgressMonitor()); try { wc.getBuffer().setContents(cuString.toString()); CompilationUnit compilationUnit= new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true); ASTNode method= NodeFinder.perform(compilationUnit, offsetBeforeMethodName, METHOD_NAME.length()).getParent(); Type[] typeNodes= new Type[types.length]; if (method instanceof MethodDeclaration) { MethodDeclaration methodDeclaration= (MethodDeclaration) method; typeNodes[parameterCount]= methodDeclaration.getReturnType2(); List<SingleVariableDeclaration> parameters= methodDeclaration.parameters(); for (int i= 0; i < parameterCount; i++) typeNodes[i]= parameters.get(i).getType(); } else if (method instanceof AnnotationTypeMemberDeclaration) { typeNodes[0]= ((AnnotationTypeMemberDeclaration) method).getType(); } for (int i= 0; i < types.length; i++) { Type type= typeNodes[i]; if (type == null) { String msg= Messages.format(RefactoringCoreMessages.TypeContextChecker_couldNotResolveType, BasicElementLabels.getJavaElementName(types[i])); results[i]= RefactoringStatus.createErrorStatus(msg); continue; } results[i]= new RefactoringStatus(); IProblem[] problems= ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS); if (problems.length > 0) { for (int p= 0; p < problems.length; p++) if (isError(problems[p], type)) results[i].addError(problems[p].getMessage()); } ITypeBinding binding= handleBug84585(type.resolveBinding()); if (firstPass && (binding == null || binding.isRecovered())) { types[i]= qualifyTypes(type, results[i]); } typeBindings[i]= binding; } return typeBindings; } finally { wc.discardWorkingCopy(); } }
@Override public void endVisit(AnnotationTypeMemberDeclaration node) { endVisitNode(node); }
@Override public boolean visit(AnnotationTypeMemberDeclaration node) { return visitNode(node); }
@Override public boolean consumes(SemanticToken token) { StructuralPropertyDescriptor location= token.getNode().getLocationInParent(); return location == MethodDeclaration.NAME_PROPERTY || location == AnnotationTypeMemberDeclaration.NAME_PROPERTY; }