Exemplo n.º 1
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 Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
      PropertyExpression pe = (PropertyExpression) exp;
      if (pe.getObjectExpression() instanceof ClassExpression) {
        ClassExpression ce = (ClassExpression) pe.getObjectExpression();
        ClassNode type = ce.getType();
        if (type.isEnum()) return exp;
        Expression constant = findConstant(type.getField(pe.getPropertyAsString()));
        // GRECLIPSE edit
        // if (constant != null) return constant;
        if (constant != null) {
          String name = pe.getText().replace('$', '.');
          Object alias = pe.getNodeMetaData("static.import.alias");
          if (alias != null && !alias.equals(pe.getPropertyAsString())) {
            name += " as " + alias;
          }
          // store the qualified name to facilitate organizing static imports
          constant.setNodeMetaData("static.import", name);

          return constant;
        }
        // GRECLIPSE end
      }
    } else if (exp instanceof ListExpression) {
      ListExpression le = (ListExpression) exp;
      ListExpression result = new ListExpression();
      for (Expression e : le.getExpressions()) {
        result.addExpression(transformInlineConstants(e));
      }
      return result;
    }

    return exp;
  }
Exemplo n.º 3
0
  private Expression annotationValueToExpression(Object value) {
    if (value == null
        || value instanceof String
        || value instanceof Number
        || value instanceof Character
        || value instanceof Boolean) return new ConstantExpression(value);

    if (value instanceof Class)
      return new ClassExpression(ClassHelper.makeWithoutCaching((Class) value));

    if (value.getClass().isArray()) {
      ListExpression elementExprs = new ListExpression();
      int len = Array.getLength(value);
      for (int i = 0; i != len; ++i)
        elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
      return elementExprs;
    }

    return null;
  }
  private Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
      PropertyExpression pe = (PropertyExpression) exp;
      if (pe.getObjectExpression() instanceof ClassExpression) {
        ClassExpression ce = (ClassExpression) pe.getObjectExpression();
        ClassNode type = ce.getType();
        if (type.isEnum()) return exp;
        Expression constant = findConstant(type.getField(pe.getPropertyAsString()));
        if (constant != null) return constant;
      }
    } else if (exp instanceof ListExpression) {
      ListExpression le = (ListExpression) exp;
      ListExpression result = new ListExpression();
      for (Expression e : le.getExpressions()) {
        result.addExpression(transformInlineConstants(e));
      }
      return result;
    }

    return exp;
  }
  private List<String> getKnownImmutables(AnnotationNode node) {
    final ArrayList<String> immutables = new ArrayList<String>();

    final Expression expression = node.getMember(MEMBER_KNOWN_IMMUTABLES);
    if (expression == null) return immutables;

    if (!(expression instanceof ListExpression)) {
      addError(
          "Use the Groovy list notation [el1, el2] to specify known immutable property names via \""
              + MEMBER_KNOWN_IMMUTABLES
              + "\"",
          node);
      return immutables;
    }

    final ListExpression listExpression = (ListExpression) expression;
    for (Expression listItemExpression : listExpression.getExpressions()) {
      if (listItemExpression instanceof ConstantExpression) {
        immutables.add((String) ((ConstantExpression) listItemExpression).getValue());
      }
    }

    return immutables;
  }
Exemplo n.º 6
0
 private void configureAnnotation(AnnotationNode node, Annotation annotation) {
   Class type = annotation.annotationType();
   if (type == Retention.class) {
     Retention r = (Retention) annotation;
     RetentionPolicy value = r.value();
     setRetentionPolicy(value, node);
     node.setMember(
         "value",
         new PropertyExpression(
             new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)),
             value.toString()));
   } else if (type == Target.class) {
     Target t = (Target) annotation;
     ElementType[] elements = t.value();
     ListExpression elementExprs = new ListExpression();
     for (ElementType element : elements) {
       elementExprs.addExpression(
           new PropertyExpression(
               new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
     }
     node.setMember("value", elementExprs);
   } else {
     Method[] declaredMethods = type.getDeclaredMethods();
     for (int i = 0; i < declaredMethods.length; i++) {
       Method declaredMethod = declaredMethods[i];
       try {
         Object value = declaredMethod.invoke(annotation);
         Expression valueExpression = annotationValueToExpression(value);
         if (valueExpression == null) continue;
         node.setMember(declaredMethod.getName(), valueExpression);
       } catch (IllegalAccessException e) {
       } catch (InvocationTargetException e) {
       }
     }
   }
 }
Exemplo n.º 7
0
 protected void weaveMixinClass(ClassNode classNode, Class mixinClass) {
   ListExpression listExpression = new ListExpression();
   listExpression.addExpression(new ClassExpression(new ClassNode(mixinClass)));
   weaveMixinsIntoClass(classNode, listExpression);
 }
 public void visitListExpression(ListExpression expression) {
   visitListOfExpressions(expression.getExpressions());
 }
Exemplo n.º 9
0
  @Override
  public Object visitListExpression(ListExpression listExpression, Object arg) throws Exception {
    MethodVisitor mv = ((InheritedAttributes) arg).mv;

    if (listExpression.expressionType.equalsIgnoreCase("Ljava/util/List;")) {

      mv.visitTypeInsn(NEW, "java/util/ArrayList");

      mv.visitInsn(DUP);
      mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
    } else if (listExpression.expressionType.startsWith("Ljava/util/List<")) {

      mv.visitTypeInsn(NEW, "java/util/ArrayList");
      mv.visitInsn(DUP);
      mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);

      for (Expression expr : listExpression.expressionList) {

        // expr.
        mv.visitInsn(DUP);
        expr.visit(this, arg);

        if (listExpression.expressionType.equalsIgnoreCase("Ljava/util/List<I>;"))
          mv.visitMethodInsn(
              INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        else if (listExpression.expressionType.equalsIgnoreCase("Ljava/util/List<Z>;"))
          mv.visitMethodInsn(
              INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
        //	else if(listExpression.expressionType.startsWith("Ljava/util/List<Ljava/util/List"))
        //	mv.visitMethodInsn(INVOKESTATIC, "java/util/ArrayList", "valueOf",
        // "(I)Ljava/lang/Integer;",false);

        mv.visitMethodInsn(
            INVOKEVIRTUAL, "java/util/ArrayList", "add", "(Ljava/lang/Object;)Z", false);
        mv.visitInsn(POP);
      }
    }
    /*else if(listExpression.expressionType.equalsIgnoreCase("Ljava/util/List<Z>;"))
    {

    	mv.visitTypeInsn(NEW, "java/util/ArrayList");
    	mv.visitInsn(DUP);
    	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);


    	for(Expression expr: listExpression.expressionList)
    	{

    		mv.visitInsn(DUP);
    		expr.visit(this, arg);
    		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;",false);

    		mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(Ljava/lang/Object;)Z", false);
    		mv.visitInsn(POP);
    	}
    }
    else
    {

    	mv.visitTypeInsn(NEW, "java/util/ArrayList");
    	mv.visitInsn(DUP);
    	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);


    	for(Expression expr: listExpression.expressionList)
    	{

    		mv.visitInsn(DUP);
    		expr.visit(this, arg);
    		//mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(java/lang/String)Ljava/lang/Boolean;",false);

    		mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(Ljava/lang/Object;)Z", false);
    		mv.visitInsn(POP);
    	}
    }
    */

    // if(listExpression.expressionType.startsWith("Ljava/util/List<Ljava/util/List"))
    //	listExpression.setType(removeLayer(listExpression.expressionType));
    // else
    listExpression.setType(emptyList);
    return listExpression.expressionType;
  }