Exemplo n.º 1
0
 public static AnnotationNode findAnnotation(ClassNode classNode, Class type) {
   List<AnnotationNode> annotations = classNode.getAnnotations();
   if (annotations != null) {
     return findAnnotation(new ClassNode(type), annotations);
   }
   return null;
 }
 private boolean isKnownImmutableClass(ClassNode fieldType, List<String> knownImmutableClasses) {
   if (!fieldType.isResolved()) return false;
   return fieldType.isEnum()
       || ClassHelper.isPrimitiveType(fieldType)
       || fieldType.getAnnotations(MY_TYPE).size() != 0
       || inImmutableList(fieldType.getName())
       || knownImmutableClasses.contains(fieldType.getName());
 }
Exemplo n.º 3
0
 public void configureAnnotation(AnnotationNode node) {
   ClassNode type = node.getClassNode();
   List<AnnotationNode> annotations = type.getAnnotations();
   for (AnnotationNode an : annotations) {
     configureAnnotationFromDefinition(an, node);
   }
   configureAnnotationFromDefinition(node, node);
 }
  private Class[] getTransformClasses(ClassNode classNode) {
    if (!classNode.hasClass()) {
      List<AnnotationNode> annotations = classNode.getAnnotations();
      AnnotationNode transformAnnotation = null;
      for (AnnotationNode anno : annotations) {
        if (anno.getClassNode().getName().equals(GroovyASTTransformationClass.class.getName())) {
          transformAnnotation = anno;
          break;
        }
      }
      if (transformAnnotation != null) {
        Expression expr = (Expression) transformAnnotation.getMember("classes");
        if (expr == null) {
          return NO_CLASSES;
        }
        Class<?>[] values = NO_CLASSES;
        // Will need to extract the classnames
        if (expr instanceof ListExpression) {
          List<Class<?>> loadedClasses = new ArrayList<Class<?>>();
          ListExpression expression = (ListExpression) expr;
          List<Expression> expressions = expression.getExpressions();
          for (Expression oneExpr : expressions) {
            String classname = ((ClassExpression) oneExpr).getType().getName();
            try {
              Class<?> clazz = Class.forName(classname, false, transformLoader);
              loadedClasses.add(clazz);
            } catch (ClassNotFoundException cnfe) {
              source
                  .getErrorCollector()
                  .addError(
                      new SimpleMessage(
                          "Ast transform processing, cannot find " + classname, source));
            }
          }
          if (loadedClasses.size() != 0) {
            values = loadedClasses.toArray(new Class<?>[loadedClasses.size()]);
          }
          return values;
        } else {

        }

        throw new RuntimeException(
            "nyi implemented in eclipse: need to support: "
                + expr
                + " (class="
                + expr.getClass()
                + ")");
      }
      return null;
    } else {
      Annotation transformClassAnnotation = getTransformClassAnnotation(classNode);
      if (transformClassAnnotation == null) {
        return null;
      }
      return getTransformClasses(transformClassAnnotation);
    }
  }
 AnnotationNode findAnnotation(ClassNode clazz) {
   if (clazz != null) {
     for (AnnotationNode node : clazz.getAnnotations()) {
       if (node.getClassNode().getName().equals(LineNumber.class.getName())) {
         // LOG.debug "Transforming in ${clazz.name}"
         return node;
       }
     }
   }
   return null;
 }
Exemplo n.º 6
0
 public static void addAnnotationIfNecessary(
     ClassNode classNode, @SuppressWarnings("unused") Class<Entity> entityClass) {
   List<AnnotationNode> annotations = classNode.getAnnotations();
   ClassNode annotationClassNode = new ClassNode(Entity.class);
   AnnotationNode annotationToAdd = new AnnotationNode(annotationClassNode);
   if (annotations.isEmpty()) {
     classNode.addAnnotation(annotationToAdd);
   } else {
     boolean foundAnn = findAnnotation(annotationClassNode, annotations) != null;
     if (!foundAnn) classNode.addAnnotation(annotationToAdd);
   }
 }
 /**
  * For the supplied classnode, this method will check if there is an annotation on it of kind
  * 'GroovyASTTransformationClass'. If there is then the 'value' member of that annotation will be
  * retrieved and the value considered to be the class name of a transformation.
  *
  * @return null if no annotation found, otherwise a String[] of classnames - this will be size 0
  *     if no value was specified
  */
 private String[] getTransformClassNames(ClassNode cn) {
   if (!cn.hasClass()) {
     List<AnnotationNode> annotations = cn.getAnnotations();
     AnnotationNode transformAnnotation = null;
     for (AnnotationNode anno : annotations) {
       if (anno.getClassNode().getName().equals(GroovyASTTransformationClass.class.getName())) {
         transformAnnotation = anno;
         break;
       }
     }
     if (transformAnnotation != null) {
       // will work so long as signature for the member 'value' is String[]
       Expression expr2 = transformAnnotation.getMember("value");
       String[] values = null;
       if (expr2 == null) {
         return NONE;
       }
       if (expr2 instanceof ListExpression) {
         ListExpression expression = (ListExpression) expr2;
         List<Expression> expressions = expression.getExpressions();
         values = new String[expressions.size()];
         int e = 0;
         for (Expression expr : expressions) {
           values[e++] = ((ConstantExpression) expr).getText();
         }
       } else if (expr2 instanceof ConstantExpression) {
         values = new String[1];
         values[0] = ((ConstantExpression) expr2).getText();
       } else {
         throw new IllegalStateException(
             "NYI: eclipse doesn't understand this kind of expression in an Ast transform definition: "
                 + expr2
                 + " (class="
                 + expr2.getClass().getName()
                 + ")");
       }
       return values;
     }
     return null;
   } else {
     // FIXASC check haven't broken transforms for 'vanilla' (outside of eclipse) execution of
     // groovyc
     Annotation transformClassAnnotation = getTransformClassAnnotation(cn);
     if (transformClassAnnotation == null) {
       return null;
     }
     return getTransformClassNames(transformClassAnnotation);
   }
 }
  private boolean addCollectedAnnotations(
      List<AnnotationNode> collected, AnnotationNode aliasNode, AnnotatedNode origin) {
    ClassNode classNode = aliasNode.getClassNode();
    boolean ret = false;
    for (AnnotationNode annotation : classNode.getAnnotations()) {
      if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
        Expression processorExp = annotation.getMember("processor");
        AnnotationCollectorTransform act = null;
        assertStringConstant(processorExp);
        if (processorExp != null) {
          String className = (String) ((ConstantExpression) processorExp).getValue();
          Class klass = loadTransformClass(className, aliasNode);
          if (klass != null) {
            try {
              act = (AnnotationCollectorTransform) klass.newInstance();
            } catch (InstantiationException e) {
              source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
            } catch (IllegalAccessException e) {
              source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
            }
          }
        } else {
          act = new AnnotationCollectorTransform();
        }
        if (act != null) {
          // GRECLIPSE edit
          // collected.addAll(act.visit(annotation, aliasNode, origin, source));
          // original annotation added to metadata to prevent import organizer from deleting its
          // import
          List<AnnotationNode> visitResult = act.visit(annotation, aliasNode, origin, source);
          for (AnnotationNode annotationNode : visitResult) {
            Set<AnnotationNode> aliases = annotationNode.getNodeMetaData("AnnotationCollector");
            if (aliases == null)
              annotationNode.setNodeMetaData("AnnotationCollector", (aliases = new HashSet(1)));

            aliases.add(aliasNode);
          }
          collected.addAll(visitResult);
          // GRECLIPSE end
        }
        ret = true;
      }
    }
    return ret;
  }
Exemplo n.º 9
0
  @Override
  public void visit(ASTNode[] astNodes, SourceUnit source) {
    if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
      throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }

    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode node = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
      return;
    }

    ClassNode classNode = (ClassNode) parent;
    if (classNode.isInterface() || Modifier.isAbstract(classNode.getModifiers())) {
      return;
    }

    boolean junit3Test = isJunit3Test(classNode);
    boolean spockTest = isSpockTest(classNode);
    boolean isJunit = classNode.getName().endsWith("Tests");

    if (!junit3Test && !spockTest && !isJunit) return;

    Expression value = node.getMember("value");
    ClassExpression ce;
    if (value instanceof ClassExpression) {
      ce = (ClassExpression) value;
      testFor(classNode, ce);
    } else {
      if (!junit3Test) {
        List<AnnotationNode> annotations = classNode.getAnnotations(MY_TYPE);
        if (annotations.size() > 0)
          return; // bail out, in this case it was already applied as a local transform
        // no explicit class specified try by convention
        String fileName = source.getName();
        String className = GrailsResourceUtils.getClassName(new FileSystemResource(fileName));
        if (className != null) {
          boolean isSpock = className.endsWith("Spec");
          String targetClassName = null;

          if (isJunit) {
            targetClassName = className.substring(0, className.indexOf("Tests"));
          } else if (isSpock) {
            targetClassName = className.substring(0, className.indexOf("Spec"));
          }

          if (targetClassName != null) {
            Resource targetResource =
                getResourceLocator().findResourceForClassName(targetClassName);
            if (targetResource != null) {
              try {
                if (GrailsResourceUtils.isDomainClass(targetResource.getURL())) {
                  testFor(
                      classNode,
                      new ClassExpression(
                          new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
                } else {
                  for (String artefactType : artefactTypeToTestMap.keySet()) {
                    if (targetClassName.endsWith(artefactType)) {
                      testFor(
                          classNode,
                          new ClassExpression(
                              new ClassNode(targetClassName, 0, ClassHelper.OBJECT_TYPE)));
                      break;
                    }
                  }
                }
              } catch (IOException e) {
                // ignore
              }
            }
          }
        }
      }
    }
  }
Exemplo n.º 10
0
 /**
  * Returns true if classNode is marked with annotationClass
  *
  * @param classNode A ClassNode to inspect
  * @param annotationClass an annotation to look for
  * @return true if classNode is marked with annotationClass, otherwise false
  */
 public static boolean hasAnnotation(
     final ClassNode classNode, final Class<? extends Annotation> annotationClass) {
   List<AnnotationNode> annotations = classNode.getAnnotations(new ClassNode(annotationClass));
   return annotations.size() > 0;
 }
Exemplo n.º 11
0
 public List<AnnotationNode> getAnnotations(ClassNode type) {
   if (redirect != null) return redirect.getAnnotations(type);
   lazyClassInit();
   return super.getAnnotations(type);
 }