protected void visitAnnotations(AnnotatedNode node, int target) {
    if (node.getAnnotations().isEmpty()) {
      return;
    }
    this.currentClass.setAnnotated(true);
    if (!isAnnotationCompatible()) {
      addError("Annotations are not supported in the current runtime. " + JVM_ERROR_MESSAGE, node);
      return;
    }
    for (AnnotationNode unvisited : node.getAnnotations()) {
      AnnotationNode visited = visitAnnotation(unvisited);
      boolean isTargetAnnotation =
          visited.getClassNode().isResolved()
              && visited.getClassNode().getName().equals("java.lang.annotation.Target");

      // Check if the annotation target is correct, unless it's the target annotating an annotation
      // definition
      // defining on which target elements the annotation applies
      if (!isTargetAnnotation && !visited.isTargetAllowed(target)) {
        addError(
            "Annotation @"
                + visited.getClassNode().getName()
                + " is not allowed on element "
                + AnnotationNode.targetToName(target),
            visited);
      }
      visitDeprecation(node, visited);
    }
  }
 public static boolean isStaticallyCompiled(AnnotatedNode node) {
   if (node.getNodeMetaData(STATIC_COMPILE_NODE) != null)
     return (Boolean) node.getNodeMetaData(STATIC_COMPILE_NODE);
   if (node instanceof MethodNode) {
     return isStaticallyCompiled(node.getDeclaringClass());
   }
   if (node instanceof InnerClassNode) {
     return isStaticallyCompiled(((InnerClassNode) node).getOuterClass());
   }
   return false;
 }
 /**
  * Adds the annotation to the internal target list if a match is found.
  *
  * @param node the AST node we are processing
  */
 public void visitAnnotations(AnnotatedNode node) {
   super.visitAnnotations(node);
   for (AnnotationNode an : node.getAnnotations()) {
     String name = an.getClassNode().getName();
     if ((GRAB_CLASS_NAME.equals(name))
         || (allowShortGrab && GRAB_SHORT_NAME.equals(name))
         || (grabAliases.contains(name))) {
       grabAnnotations.add(an);
     }
     if ((GRABEXCLUDE_CLASS_NAME.equals(name))
         || (allowShortGrabExcludes && GRABEXCLUDE_SHORT_NAME.equals(name))
         || (grabExcludeAliases.contains(name))) {
       grabExcludeAnnotations.add(an);
     }
     if ((GRABCONFIG_CLASS_NAME.equals(name))
         || (allowShortGrabConfig && GRABCONFIG_SHORT_NAME.equals(name))
         || (grabConfigAliases.contains(name))) {
       grabConfigAnnotations.add(an);
     }
     if ((GRAPES_CLASS_NAME.equals(name))
         || (allowShortGrapes && GRAPES_SHORT_NAME.equals(name))
         || (grapesAliases.contains(name))) {
       grapesAnnotations.add(an);
     }
     if ((GRABRESOLVER_CLASS_NAME.equals(name))
         || (allowShortGrabResolver && GRABRESOLVER_SHORT_NAME.equals(name))
         || (grabResolverAliases.contains(name))) {
       grabResolverAnnotations.add(an);
     }
   }
 }
Example #4
0
 private void setAnnotationMetaData(Annotation[] annotations, AnnotatedNode an) {
   for (Annotation annotation : annotations) {
     AnnotationNode node = new AnnotationNode(ClassHelper.make(annotation.annotationType()));
     configureAnnotation(node, annotation);
     an.addAnnotation(node);
   }
 }
 /**
  * Adds the annotation to the internal target list if a match is found.
  *
  * @param node the node to be processed
  */
 public void visitAnnotations(AnnotatedNode node) {
   super.visitAnnotations(node);
   for (AnnotationNode annotation : node.getAnnotations()) {
     if (transforms.containsKey(annotation)) {
       targetNodes.add(new ASTNode[] {annotation, node});
     }
   }
 }
 /**
  * Convenience method to see if an annotated node is {@code @MybatisAware}.
  *
  * @param node the node to check
  * @return true if the node is an event publisher
  */
 public static boolean hasMybatisAwareAnnotation(AnnotatedNode node) {
   for (AnnotationNode annotation : node.getAnnotations()) {
     if (MYBATIS_AWARE_CNODE.equals(annotation.getClassNode())) {
       return true;
     }
   }
   return false;
 }
 /**
  * Convenience method to see if an annotated node is {@code @MessageSourceAware}.
  *
  * @param node the node to check
  * @return true if the node is an event publisher
  */
 public static boolean hasMessageSourceAwareAnnotation(AnnotatedNode node) {
   for (AnnotationNode annotation : node.getAnnotations()) {
     if (MESSAGE_SOURCE_AWARE_CNODE.equals(annotation.getClassNode())) {
       return true;
     }
   }
   return false;
 }
 /**
  * Convenience method to see if an annotated node is {@code @EventPublisher}.
  *
  * @param node the node to check
  * @return true if the node is an event publisher
  */
 public static boolean hasEventPublisherAnnotation(AnnotatedNode node) {
   for (AnnotationNode annotation : node.getAnnotations()) {
     if (EVENT_PUBLISHER_ANODE.equals(annotation.getClassNode())) {
       return true;
     }
   }
   return false;
 }
  /**
   * If the annotation is annotated with {@link GroovyASTTransformation} the annotation is added to
   * <code>stageVisitors</code> at the appropriate processor visitor.
   *
   * @param node the node to process
   */
  public void visitAnnotations(AnnotatedNode node) {
    super.visitAnnotations(node);
    List<AnnotationNode> collected = new ArrayList<AnnotationNode>();
    for (Iterator<AnnotationNode> it = node.getAnnotations().iterator(); it.hasNext(); ) {
      AnnotationNode annotation = it.next();
      if (addCollectedAnnotations(collected, annotation, node)) it.remove();
    }
    node.getAnnotations().addAll(collected);

    for (AnnotationNode annotation : node.getAnnotations()) {

      // GRECLIPSE: start: under eclipse we may be asking a node that has no backing class
      /*{
      Annotation transformClassAnnotation = getTransformClassAnnotation(annotation.getClassNode());
      if (transformClassAnnotation == null) {
          // skip if there is no such annotation
          continue;
      }
      addTransformsToClassNode(annotation, transformClassAnnotation);
      }*/
      // newcode:
      if (!this.allowTransforms) {
        if (!isAllowed(annotation.getClassNode().getName())) {
          continue;
        }
      }
      // GRECLIPSE end
      String[] transformClassNames = getTransformClassNames(annotation.getClassNode());
      Class[] transformClasses = getTransformClasses(annotation.getClassNode());
      if (transformClassNames == null && transformClasses == null) {
        continue;
      }
      if (transformClassNames == null) {
        transformClassNames = NONE;
      }
      if (transformClasses == null) {
        transformClasses = NO_CLASSES;
      }
      addTransformsToClassNode(annotation, transformClassNames, transformClasses);
      // end
    }
  }
 public void visitAnnotations(AnnotatedNode node) {
   List<AnnotationNode> annotations = node.getAnnotations();
   if (annotations.isEmpty()) return;
   for (AnnotationNode an : annotations) {
     // skip built-in properties
     if (an.isBuiltIn()) continue;
     for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
       Expression annMemberValue = member.getValue();
       annMemberValue.visit(this);
     }
   }
 }
  public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2
        || !(nodes[0] instanceof AnnotationNode)
        || !(nodes[1] instanceof AnnotatedNode)) {
      throw new RuntimeException(
          "Internal error: expecting [AnnotationNode, AnnotatedNode] but got: "
              + Arrays.asList(nodes));
    }

    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
    if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode) return;

    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
      List<groovy.transform.PackageScopeTarget> targets;
      if (value == null)
        targets = Arrays.asList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
      else targets = determineTargets(value);
      visitClassNode((ClassNode) parent, targets);
      parent.getAnnotations();
    } else {
      if (value != null)
        throw new RuntimeException(
            "Error during "
                + MY_TYPE_NAME
                + " processing: "
                + TARGET_CLASS_NAME
                + " only allowed at class level.");
      if (parent instanceof MethodNode) {
        visitMethodNode((MethodNode) parent);
      } else if (parent instanceof FieldNode) {
        visitFieldNode((FieldNode) parent);
      }
    }
  }
  /**
   * Copies all <tt>candidateAnnotations</tt> with retention policy {@link
   * java.lang.annotation.RetentionPolicy#RUNTIME} and {@link
   * java.lang.annotation.RetentionPolicy#CLASS}.
   *
   * <p>Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not
   * supported at present.
   */
  public static void copyAnnotatedNodeAnnotations(
      final AnnotatedNode annotatedNode,
      final List<AnnotationNode> copied,
      List<AnnotationNode> notCopied) {
    List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
    for (AnnotationNode annotation : annotationList) {

      List<AnnotationNode> annotations =
          annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
      if (annotations.isEmpty()) continue;

      if (hasClosureMember(annotation)) {
        notCopied.add(annotation);
        continue;
      }

      AnnotationNode retentionPolicyAnnotation = annotations.get(0);
      Expression valueExpression = retentionPolicyAnnotation.getMember("value");
      if (!(valueExpression instanceof PropertyExpression)) continue;

      PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
      boolean processAnnotation =
          propertyExpression.getProperty() instanceof ConstantExpression
              && ("RUNTIME"
                      .equals(((ConstantExpression) (propertyExpression.getProperty())).getValue())
                  || "CLASS"
                      .equals(
                          ((ConstantExpression) (propertyExpression.getProperty())).getValue()));

      if (processAnnotation) {
        AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
        for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) {
          newAnnotation.addMember(member.getKey(), member.getValue());
        }
        newAnnotation.setSourcePosition(annotatedNode);

        copied.add(newAnnotation);
      }
    }
  }
 private void printAnnotations(PrintWriter out, AnnotatedNode annotated) {
   if (!java5) return;
   for (AnnotationNode annotation : annotated.getAnnotations()) {
     printAnnotation(out, annotation);
   }
 }