/** * Check if the list of annotation contains the annotation of given name. * * @param annos, the annotation list * @param annotationName, the annotation name * @return <code>true</code> if the annotation list contains the given annotation. */ private boolean containsAnnotation(List<AnnotationExpr> annos, String annotationName) { for (AnnotationExpr anno : annos) { if (anno.getName().getName().equals(annotationName)) { return true; } } return false; }
/** * @param declaration * @return true if given declaration already has an @Override annotation, false * otherwise */ private boolean isOverrideAnnotationExisting(MethodDeclaration declaration) { List<AnnotationExpr> annotations = declaration.getAnnotations(); for (AnnotationExpr annotation : annotations) { if (annotation.getNameAsString().equals(OVERRIDE_ANNOTATION_NAME)) { return true; } } return false; }
public static Map<String, MemberValuePair> mapAnnotationFields(AnnotationExpr annotationExpr) { Map<String, MemberValuePair> nameToFieldMap = new HashMap<>(); for (Node node : annotationExpr.getChildrenNodes()) { if (node instanceof MemberValuePair) { MemberValuePair memberValuePair = (MemberValuePair) node; nameToFieldMap.put(memberValuePair.getName(), memberValuePair); } } return nameToFieldMap; }
private Optional<AnnotationImp> getAnnotationMethod(final AnnotationExpr an) { Optional<AnnotationImp> optional = Optional.empty(); final List<String> classes = this.findImportForClasses(an.getNameAsString()); for (final Class<?> clazz : this.loadClasses(classes)) { try { final AnnotationImp annotationImp = new AnnotationImp(clazz.asSubclass(Annotation.class)); annotationImp.location(LocationEnum.METHOD); an.ifNormalAnnotationExpr(normal -> { normal.getPairs().forEach(pair -> { annotationImp.property( pair.getNameAsString(), pair.getValue().toString().replaceAll("\"", "")); }); }); optional = Optional.ofNullable(annotationImp); break; } catch (final Exception e) { // Probably wrong import. } } return optional; }
public void calculate(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, CompilationUnit compilationUnit) { List<AnnotationExpr> annotations = classOrInterfaceDeclaration.getAnnotations(); for (AnnotationExpr annotation : annotations) { String annotationName = annotation.getNameAsString(); String annotationPackageName = annotation .findCompilationUnit() .flatMap(CompilationUnit::getPackageDeclaration) .flatMap(pkg -> Optional.of(pkg.getNameAsString())).orElse("???"); if (typeDao.exist(annotationName, annotationPackageName)) { // JDK annotations are not indexed int annotationId = typeDao.getId(annotationName, annotationPackageName); int typeId = typeDao.getId(classOrInterfaceDeclaration.getNameAsString(), compilationUnit.getPackageDeclaration().get().getNameAsString()); annotatedWithDao.save(new AnnotatedWith(typeId, annotationId)); } } } }
@Override public void visit(CatchClause n, A arg) { MultiTypeParameter multiTypeParameter = n.getExcept(); HashSet<String> blockVariables = newHashSet(); this.localVariables.addLast(blockVariables); try { blockVariables.add(multiTypeParameter.getId().getName()); for (AnnotationExpr annotationExpr : emptyIfNull(multiTypeParameter.getAnnotations())) { annotationExpr.accept(this, arg); } BlockStmt body = n.getCatchBlock(); if (body != null) { visit(body, arg); } } finally { this.localVariables.removeLast(); } }
private Optional<AnnotationImp> getAnnotationField(final AnnotationExpr an) { Optional<AnnotationImp> optional = Optional.empty(); final List<String> classes = this.findImportForClasses(an.getNameAsString()); for (final Class<?> clazz : this.loadClasses(classes)) { try { final AnnotationImp annotationImp = new AnnotationImp(clazz.asSubclass(Annotation.class)); annotationImp.location(LocationEnum.FIELD); an.ifNormalAnnotationExpr(normal -> { normal.getPairs().forEach(pair -> { annotationImp.property( pair.getNameAsString(), pair.getValue().toString().replaceAll("\"", "")); }); }); optional = Optional.ofNullable(annotationImp); break; } catch (final Exception e) { // Probably wrong import. } } return optional; }
@Override public void visit(ConstructorDeclaration n, A arg) { HashSet<String> blockVariables = newHashSet(); this.localVariables.addLast(blockVariables); try { for (Parameter parameter : emptyIfNull(n.getParameters())) { blockVariables.add(parameter.getId().getName()); } for (AnnotationExpr annotationExpr : emptyIfNull(n.getAnnotations())) { annotationExpr.accept(this, arg); } BlockStmt body = n.getBlock(); if (body != null) { visit(body, arg); } } finally { this.localVariables.removeLast(); } }
@Override public void visit(final MethodDeclaration m, final Context ctx) { if (!script) { boolean mvc = m.getAnnotations().stream() .map(it -> it.getName().getName()) .filter(Route.METHODS::contains) .findFirst() .isPresent(); if (mvc) { nodes.add(Maps.immutableEntry(m, m.getBody())); } } }
@Override protected String getTypeName(CompilationUnit compilationUnit, int index) { List<AnnotationExpr> annotations = compilationUnit.getTypes().get(0).getAnnotations(); return annotations.get(index).getNameAsString(); }
public static Map<String, MemberValuePair> mapAnnotationFields(AnnotationExpr annotationExpr) { Map<String, MemberValuePair> nameToFieldMap = new HashMap<>(); for (Node node : annotationExpr.getChildrenNodes()) { if (node instanceof MemberValuePair) { MemberValuePair memberValuePair = (MemberValuePair) node; nameToFieldMap.put(memberValuePair.getName(), memberValuePair); } } return nameToFieldMap; }
@Override public void visit(MethodDeclaration n, A arg) { HashSet<String> blockVariables = newHashSet(); this.localVariables.addLast(blockVariables); try { for (Parameter parameter : emptyIfNull(n.getParameters())) { blockVariables.add(parameter.getId().getName()); } for (AnnotationExpr annotationExpr : emptyIfNull(n.getAnnotations())) { annotationExpr.accept(this, arg); } BlockStmt body = n.getBody(); if (body != null) { visit(body, arg); } } finally { this.localVariables.removeLast(); } }
private MemberParameterTag createMemberParamTag(JavadocDescription javadocDescription, Stream<AnnotationExpr> annotationStream) { Map<String, String> annotations = annotationStream .filter(Expression::isSingleMemberAnnotationExpr) .collect(Collectors.toMap(a -> a.getName().getIdentifier(), this::createMemberParamValue)); return new MemberParameterTag(javadocDescription.toText(), annotations); }
public SymbolReference<ResolvedAnnotationDeclaration> solve(AnnotationExpr annotationExpr) { Context context = JavaParserFactory.getContext(annotationExpr, typeSolver); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = context.solveType(annotationExpr.getNameAsString(), typeSolver); ResolvedAnnotationDeclaration annotationDeclaration = (ResolvedAnnotationDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration(); if (typeDeclarationSymbolReference.isSolved()) { return SymbolReference.solved(annotationDeclaration); } else { return SymbolReference.unsolved(ResolvedAnnotationDeclaration.class); } }
@Override public void visit(VariableDeclarationExpr n, A arg) { for (AnnotationExpr annotationExpr : emptyIfNull(n.getAnnotations())) { annotationExpr.accept(this, arg); } n.getType().accept(this, arg); Set<String> blockVariables = this.localVariables.getLast(); for (VariableDeclarator variableDeclarator : n.getVars()) { Expression expr = variableDeclarator.getInit(); if (expr != null) { expr.accept(this, arg); } blockVariables.add(variableDeclarator.getId().getName()); } }
private boolean isGenerated(BodyDeclaration<?> node) { for (AnnotationExpr annotation : node.getAnnotations()) { if (annotation.getName().toString().equals("Generated")) { return true; } } return false; }
public SymbolReference<ResolvedAnnotationDeclaration> solve(AnnotationExpr annotationExpr) { Context context = JavaParserFactory.getContext(annotationExpr, typeSolver); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = context.solveType(annotationExpr.getNameAsString(), typeSolver); ResolvedAnnotationDeclaration annotationDeclaration = (ResolvedAnnotationDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration(); if (typeDeclarationSymbolReference.isSolved()) { return SymbolReference.solved(annotationDeclaration); } else { return SymbolReference.unsolved(ResolvedAnnotationDeclaration.class); } }
private MemberParameterTag createMemberParamTag(JavadocDescription javadocDescription, Stream<AnnotationExpr> annotationStream) { Map<String, String> annotations = annotationStream .filter(Expression::isSingleMemberAnnotationExpr) .collect(Collectors.toMap(a -> a.getName().getIdentifier(), this::createMemberParamValue)); return new MemberParameterTag(javadocDescription.toText(), annotations); }
public SymbolReference<ResolvedAnnotationDeclaration> solve(AnnotationExpr annotationExpr) { Context context = JavaParserFactory.getContext(annotationExpr, typeSolver); SymbolReference<ResolvedTypeDeclaration> typeDeclarationSymbolReference = context.solveType(annotationExpr.getNameAsString()); if (typeDeclarationSymbolReference.isSolved()) { ResolvedAnnotationDeclaration annotationDeclaration = (ResolvedAnnotationDeclaration) typeDeclarationSymbolReference.getCorrespondingDeclaration(); return SymbolReference.solved(annotationDeclaration); } else { return SymbolReference.unsolved(ResolvedAnnotationDeclaration.class); } }
@Override public void visit(MethodDeclaration methodDeclaration, Object arg) { super.visit(methodDeclaration, arg); boolean isATestMethod = false; for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { if (expr.getName().getName().equals(Test.class.getSimpleName()) || expr.getName().getName().equals(Test.class.getName())) { isATestMethod = true; break; } } if (!isATestMethod) { return; } methodAnnotationMap.put(methodDeclaration.getName(), MutablePair.of(methodDeclaration, new HashMap<String, AnnotationExpr>())); for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { String[] split = expr.getName().getName().split("\\."); methodAnnotationMap.get(methodDeclaration.getName()).getRight().put(split[split.length - 1], expr); } }