private Statement createConstructorStatement(
     ClassNode cNode,
     PropertyNode pNode,
     List<String> knownImmutableClasses,
     List<String> knownImmutables) {
   FieldNode fNode = pNode.getField();
   final ClassNode fieldType = fNode.getType();
   Statement statement = null;
   if (fieldType.isArray() || isOrImplements(fieldType, CLONEABLE_TYPE)) {
     statement = createConstructorStatementArrayOrCloneable(fNode);
   } else if (isKnownImmutableClass(fieldType, knownImmutableClasses)
       || isKnownImmutable(pNode.getName(), knownImmutables)) {
     statement = createConstructorStatementDefault(fNode);
   } else if (fieldType.isDerivedFrom(DATE_TYPE)) {
     statement = createConstructorStatementDate(fNode);
   } else if (isOrImplements(fieldType, COLLECTION_TYPE)
       || fieldType.isDerivedFrom(COLLECTION_TYPE)
       || isOrImplements(fieldType, MAP_TYPE)
       || fieldType.isDerivedFrom(MAP_TYPE)) {
     statement = createConstructorStatementCollection(fNode);
   } else if (fieldType.isResolved()) {
     addError(
         createErrorMessage(cNode.getName(), fNode.getName(), fieldType.getName(), "compiling"),
         fNode);
     statement = EmptyStatement.INSTANCE;
   } else {
     statement = createConstructorStatementGuarded(cNode, fNode);
   }
   return statement;
 }
Пример #2
0
 private void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) {
   ClassNode type = definition.getClassNode();
   if (!type.isResolved()) return;
   Class clazz = type.getTypeClass();
   if (clazz == Retention.class) {
     Expression exp = definition.getMember("value");
     if (!(exp instanceof PropertyExpression)) return;
     PropertyExpression pe = (PropertyExpression) exp;
     String name = pe.getPropertyAsString();
     RetentionPolicy policy = RetentionPolicy.valueOf(name);
     setRetentionPolicy(policy, root);
   } else if (clazz == Target.class) {
     Expression exp = definition.getMember("value");
     if (!(exp instanceof ListExpression)) return;
     ListExpression le = (ListExpression) exp;
     int bitmap = 0;
     for (Expression e : le.getExpressions()) {
       PropertyExpression element = (PropertyExpression) e;
       String name = element.getPropertyAsString();
       ElementType value = ElementType.valueOf(name);
       bitmap |= getElementCode(value);
     }
     root.setAllowedTargets(bitmap);
   }
 }
 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());
 }
 private Expression findStaticField(ClassNode staticImportType, String fieldName) {
   if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
     FieldNode field = staticImportType.getField(fieldName);
     if (field != null && field.isStatic())
       return new PropertyExpression(new ClassExpression(staticImportType), fieldName);
   }
   return null;
 }
 private Expression findStaticMethod(
     ClassNode staticImportType, String methodName, Expression args) {
   if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
     if (staticImportType.hasPossibleStaticMethod(methodName, args)) {
       return new StaticMethodCallExpression(staticImportType, methodName, args);
     }
   }
   return null;
 }
Пример #6
0
  /**
   * Returns the concrete class this classnode relates to. However, this method is inherently unsafe
   * as it may return null depending on the compile phase you are using. AST transformations should
   * never use this method directly, but rather obtain a new class node using {@link
   * #getPlainNodeReference()}.
   *
   * @return the class this classnode relates to. May return null.
   */
  public Class getTypeClass() {
    if (clazz != null) return clazz;
    if (redirect != null) return redirect.getTypeClass();

    ClassNode component = redirect().componentType;
    if (component != null && component.isResolved()) {
      return Array.newInstance(component.getTypeClass(), 0).getClass();
    }
    throw new GroovyBugError(
        "ClassNode#getTypeClass for " + getName() + " is called before the type class is set ");
  }
Пример #7
0
  public static ClassNode getFurthestUnresolvedParent(ClassNode classNode) {
    ClassNode parent = classNode.getSuperClass();

    while (parent != null
        && !getFullName(parent).equals("java.lang.Object")
        && !parent.isResolved()
        && !Modifier.isAbstract(parent.getModifiers())) {
      classNode = parent;
      parent = parent.getSuperClass();
    }
    return classNode;
  }
  private static Annotation getTransformClassAnnotation(ClassNode annotatedType) {
    if (!annotatedType.isResolved()) return null;

    for (Annotation ann : annotatedType.getTypeClass().getAnnotations()) {
      // because compiler clients are free to choose any GroovyClassLoader for
      // resolving ClassNodeS such as annotatedType, we have to compare by name,
      // and cannot cast the return value to GroovyASTTransformationClass
      if (ann.annotationType().getName().equals(GroovyASTTransformationClass.class.getName())) {
        return ann;
      }
    }

    return null;
  }
Пример #9
0
 public boolean isResolved() {
   if (clazz != null) return true;
   if (redirect != null) return redirect.isResolved();
   return componentType != null && componentType.isResolved();
 }