private AnnotationExpr findAnnotation(final BodyDeclaration n, String fullyQualifiedName, boolean foundAnnImport) { final String simpleName = ClassUtils.getShortClassName(fullyQualifiedName); final List<AnnotationExpr> annotations = n.getAnnotations() != null ? n.getAnnotations() : new ArrayList<AnnotationExpr>(); for (AnnotationExpr ae : annotations) { final String name = ae.getName().toString(); if ((simpleName.equals(name) && foundAnnImport)) { LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + "."); return ae; } if (fullyQualifiedName.equals(name)) { LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + "."); return ae; } } return null; }
@SuppressWarnings("unchecked") public XAnnotation<?> parse(final AnnotationExpr annotationElement) throws AnnotationExpressionParseException { Validate.notNull(annotationElement, "Annotation expression must not be null."); final String className = annotationElement.getName().toString(); try { final Class<?> draftClass = ClassUtils.getClass(classLoader, className); if (!Annotation.class.isAssignableFrom(draftClass)) throw new AnnotationExpressionParseException(annotationElement, new IllegalArgumentException(MessageFormat.format( "The class [{0}] is not an annotation class.", draftClass.getName()))); final Class<? extends Annotation> annotationClass = (Class<? extends Annotation>) draftClass; final XAnnotationField<?>[] fields = parseFields(annotationElement, annotationClass); @SuppressWarnings("rawtypes") final XAnnotation<?> xannotation = new XAnnotation(annotationClass, fields); return xannotation; } catch (ClassNotFoundException cnfex) { throw new AnnotationExpressionParseException(annotationElement, new AnnotationClassNotFoundException(className, cnfex)); } }
@Override public void visit(MethodDeclaration n, Object arg) { // here you can access the attributes of the method. // this method will be called for all methods in this // CompilationUnit, including inner class methods currentMethod = new CGEntry(TYPE.METHOD, currentClass, currentMethod, n.getName()); if (isDebug()) System.out.println("M:" + currentMethod); list.add(currentMethod); List<AnnotationExpr> annos = n.getAnnotations(); if (annos != null) { for (AnnotationExpr anno : annos) { if (isDebug()) System.out.println("A:" + anno.getName()); currentMethod.addAnnotation(new CGEntry(TYPE.ANNOTATION, currentClass, currentMethod, anno.getName().getName())); } } super.visit(n, arg); if (currentMethod != null) currentMethod = currentMethod.getMethod(); }
@Override public void visit(FieldDeclaration n, Object arg) { for (VariableDeclarator var : n.getVariables()) { CGEntry currentVariable = new CGEntry(TYPE.VARIABLE, currentClass, currentMethod, var.getId().getName()); if (isDebug()) System.out.println("V:" + currentVariable); list.add(currentVariable); List<AnnotationExpr> annos = n.getAnnotations(); if (annos != null) { for (AnnotationExpr anno : annos) { if (isDebug()) System.out.println("A:" + anno.getName()); currentVariable.addAnnotation(new CGEntry(TYPE.ANNOTATION, currentClass, currentMethod, anno.getName().getName())); } } } super.visit(n, arg); }
@Override public void visit(ClassOrInterfaceDeclaration n, Object arg) { currentClass = new CGEntry(CGEntry.TYPE.CLASS, currentClass, null, n.getName()); if (mainClass == null) { mainClass = currentClass; mainClass.setMainClass(true); mainClass.setPackage(pack); } if (isDebug()) System.out.println("C:" + currentClass); list.add(currentClass); List<AnnotationExpr> annos = n.getAnnotations(); if (annos != null) { for (AnnotationExpr anno : annos) { if (isDebug()) System.out.println("A:" + anno.getName()); currentClass.addAnnotation(new CGEntry(TYPE.ANNOTATION, currentClass, currentMethod, anno.getName().getName())); } } super.visit(n, arg); if (currentClass != null ) currentClass = currentClass.getClazz(); }