private MethodDeclaration[] getConstructorDeclarationNodes(final AbstractTypeDeclaration declaration) { if (declaration instanceof TypeDeclaration) { final MethodDeclaration[] declarations= ((TypeDeclaration) declaration).getMethods(); final List<MethodDeclaration> result= new ArrayList<>(2); for (int index= 0; index < declarations.length; index++) { if (declarations[index].isConstructor()) result.add(declarations[index]); } return result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private MethodDeclaration[] getConstructorDeclarationNodes(final AbstractTypeDeclaration declaration) { if (declaration instanceof TypeDeclaration) { final MethodDeclaration[] declarations= ((TypeDeclaration) declaration).getMethods(); final List<MethodDeclaration> result= new ArrayList<>(2); for (int index= 0; index < declarations.length; index++) { if (declarations[index].isConstructor()) result.add(declarations[index]); } return result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private MethodDeclaration[] getConstructorDeclarationNodes(final AbstractTypeDeclaration declaration) { if (declaration instanceof TypeDeclaration) { final MethodDeclaration[] declarations= ((TypeDeclaration) declaration).getMethods(); final List result= new ArrayList(2); for (int index= 0; index < declarations.length; index++) { if (declarations[index].isConstructor()) result.add(declarations[index]); } return (MethodDeclaration[]) result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) { if (typeDeclaration instanceof TypeDeclaration) { MethodDeclaration[] allMethods= ((TypeDeclaration) typeDeclaration).getMethods(); List result= new ArrayList(Math.min(allMethods.length, 1)); for (int i= 0; i < allMethods.length; i++) { MethodDeclaration declaration= allMethods[i]; if (declaration.isConstructor()) result.add(declaration); } return (MethodDeclaration[]) result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) { if (typeDeclaration instanceof TypeDeclaration) { MethodDeclaration[] allMethods= ((TypeDeclaration) typeDeclaration).getMethods(); List<MethodDeclaration> result= new ArrayList<>(Math.min(allMethods.length, 1)); for (int i= 0; i < allMethods.length; i++) { MethodDeclaration declaration= allMethods[i]; if (declaration.isConstructor()) result.add(declaration); } return result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) { if (typeDeclaration instanceof TypeDeclaration) { MethodDeclaration[] allMethods= ((TypeDeclaration) typeDeclaration).getMethods(); List<MethodDeclaration> result= new ArrayList<>(Math.min(allMethods.length, 1)); for (int i= 0; i < allMethods.length; i++) { MethodDeclaration declaration= allMethods[i]; if (declaration.isConstructor()) result.add(declaration); } return result.toArray(new MethodDeclaration[result.size()]); } return new MethodDeclaration[] {}; }
private static MethodDeclaration[] getMethods(AbstractTypeDeclaration node) { if (node instanceof TypeDeclaration) { return ((TypeDeclaration)node).getMethods(); } else if (node instanceof EnumDeclaration) { List<MethodDeclaration> methodsList = new ArrayList<MethodDeclaration>(); for (Object declaration : node.bodyDeclarations()) { if (declaration instanceof MethodDeclaration) { methodsList.add((MethodDeclaration)declaration); } } return methodsList.toArray(new MethodDeclaration[methodsList.size()]); } else { throw new UnsupportedOperationException("not implemented"); } }
private MethodDeclaration findMethod(TypeDeclaration typeDeclaration, String methodToFind) { if (typeDeclaration != null) { for (MethodDeclaration method : typeDeclaration.getMethods()) { IMethodBinding methodBinding = method.resolveBinding(); if (methodBinding != null && methodToFind.equals(methodBinding.getName()) && method.parameters().isEmpty()) { return method; } } } return null; }
@Override public MethodDeclaration bodyDeclaration(CompilationUnit astRoot) { String methodName = this.getName(); for (MethodDeclaration methodDeclaration : this.declaringTypeDeclaration(astRoot).getMethods()) { if (methodDeclaration.getName().getFullyQualifiedName().equals(methodName) && (methodDeclaration.parameters().size() == 0)) { return methodDeclaration; } } return null; }
public static TypeParameter newTypeParameter(AST ast, String content) { StringBuffer buffer= new StringBuffer(TYPEPARAM_HEADER); buffer.append(content); buffer.append(TYPEPARAM_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0); ASTNode result= ASTNode.copySubtree(ast, tp); result.accept(new PositionClearer()); return (TypeParameter) result; }
public static TypeParameter newTypeParameter(AST ast, String content) { StringBuilder buffer= new StringBuilder(TYPEPARAM_HEADER); buffer.append(content); buffer.append(TYPEPARAM_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List<AbstractTypeDeclaration> list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0); ASTNode result= ASTNode.copySubtree(ast, tp); result.accept(new PositionClearer()); return (TypeParameter) result; }
public static TypeParameter newTypeParameter(AST ast, String content) { StringBuffer buffer= new StringBuffer(TYPEPARAM_HEADER); buffer.append(content); buffer.append(TYPEPARAM_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List<AbstractTypeDeclaration> list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0); ASTNode result= ASTNode.copySubtree(ast, tp); result.accept(new PositionClearer()); return (TypeParameter) result; }
public static Type newType(AST ast, String content) { StringBuffer buffer= new StringBuffer(TYPE_HEADER); buffer.append(content); buffer.append(TYPE_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; ASTNode type= methodDecl.getReturnType2(); ASTNode result= ASTNode.copySubtree(ast, type); result.accept(new PositionClearer()); return (Type)result; }
public static Type newType(AST ast, String content) { StringBuilder buffer= new StringBuilder(TYPE_HEADER); buffer.append(content); buffer.append(TYPE_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List<AbstractTypeDeclaration> list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; ASTNode type= methodDecl.getReturnType2(); ASTNode result= ASTNode.copySubtree(ast, type); result.accept(new PositionClearer()); return (Type)result; }
public static Type newType(AST ast, String content) { StringBuffer buffer= new StringBuffer(TYPE_HEADER); buffer.append(content); buffer.append(TYPE_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List<AbstractTypeDeclaration> list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; ASTNode type= methodDecl.getReturnType2(); ASTNode result= ASTNode.copySubtree(ast, type); result.accept(new PositionClearer()); return (Type)result; }
private ASTNode getPreviousSibling(ASTNode node) { boolean isPrevious = true; if (node != null && node.getParent() instanceof TypeDeclaration) { final TypeDeclaration typeDecl = (TypeDeclaration) node.getParent(); final TreeMap<Integer, ASTNode> nodes = new TreeMap<Integer, ASTNode>(); addAll(nodes, typeDecl.getFields()); addAll(nodes, typeDecl.getMethods()); addAll(nodes, typeDecl.getTypes()); if (isPrevious) { SortedMap<Integer, ASTNode> entries = nodes.headMap(node.getStartPosition()); if (!entries.isEmpty()) { return entries.get(entries.lastKey()); } } else { SortedMap<Integer, ASTNode> entries = nodes.tailMap(node.getStartPosition()); if (!entries.isEmpty()) { return entries.get(entries.firstKey()); } } } return null; }
private int getInterfaceMethodModifiers(ASTNode targetTypeDecl, boolean createAbstractMethod) { // for interface and annotation members copy the modifiers from an existing member if (targetTypeDecl instanceof TypeDeclaration) { TypeDeclaration type= (TypeDeclaration) targetTypeDecl; MethodDeclaration[] methodDecls= type.getMethods(); if (methodDecls.length > 0) { if (createAbstractMethod) { for (MethodDeclaration methodDeclaration : methodDecls) { IMethodBinding methodBinding= methodDeclaration.resolveBinding(); if (methodBinding != null && JdtFlags.isAbstract(methodBinding)) { return methodDeclaration.getModifiers(); } } } return methodDecls[0].getModifiers() & Modifier.PUBLIC; } List<BodyDeclaration> bodyDecls= type.bodyDeclarations(); if (bodyDecls.size() > 0) { return bodyDecls.get(0).getModifiers() & Modifier.PUBLIC; } } return 0; }
public static void addSiblingMethodNames( final MethodDeclaration declaration, final Set<String> features) { if (!(declaration.getParent() instanceof TypeDeclaration)) { return; } final TypeDeclaration td = (TypeDeclaration) declaration.getParent(); for (final MethodDeclaration md : td.getMethods()) { if (md.getName().getIdentifier() .equals(declaration.getName().getIdentifier())) { continue; } final List<String> nameparts = getNameParts(md.getName() .getIdentifier()); nameparts.forEach(p -> features.add("sibling:" + p)); } }
/** * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.TypeDeclaration) */ public boolean visit(TypeDeclaration node) { if (containsPosition(node)) { // visit the methode declarations MethodDeclaration[] methods = node.getMethods(); for (int i = 0, length = methods.length; i < length && !fFound; i++) { methods[i].accept(this); } if (!fFound) { // visit inner types TypeDeclaration[] types = node.getTypes(); for (int i = 0, length = types.length; i < length && !fFound; i++) { types[i].accept(this); } } } return false; }
/** * Matches if methodName and parameters are equal * * @param keySig search key * @param scopeCu search scope * @return method sig, null if not found */ public static MethodSig findRelevantMethodSig(MethodSig keySig, CompilationUnit scopeCu) { TypeDeclaration type = getType(scopeCu); for (MethodDeclaration method : type.getMethods()) { if (method.getName().getIdentifier().equals(keySig.getMethodName())) { MethodSig methodSig = toMethodSig(method); if (methodSig.parameters().equals(keySig.parameters())) { return methodSig; } } } return null; }