private void visitConstructorOrMethod(MethodNode node, int methodTarget) {
    visitAnnotations(node, methodTarget);
    for (int i = 0; i < node.getParameters().length; i++) {
      Parameter parameter = node.getParameters()[i];
      visitAnnotations(parameter, AnnotationNode.PARAMETER_TARGET);
    }

    if (this.currentClass.isAnnotationDefinition() && !node.isStaticConstructor()) {
      ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
      AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
      visitor.setReportClass(currentClass);
      visitor.checkReturnType(node.getReturnType(), node);
      if (node.getParameters().length > 0) {
        addError("Annotation members may not have parameters.", node.getParameters()[0]);
      }
      if (node.getExceptions().length > 0) {
        addError("Annotation members may not have a throws clause.", node.getExceptions()[0]);
      }
      ReturnStatement code = (ReturnStatement) node.getCode();
      if (code != null) {
        visitor.visitExpression(node.getName(), code.getExpression(), node.getReturnType());
        visitor.checkCircularReference(currentClass, node.getReturnType(), code.getExpression());
      }
      this.source.getErrorCollector().addCollectorContents(errorCollector);
    }
  }
 /**
  * Resolve metadata and details of the annotation.
  *
  * @param unvisited the node to visit
  * @return the visited node
  */
 private AnnotationNode visitAnnotation(AnnotationNode unvisited) {
   ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
   AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
   AnnotationNode visited = visitor.visit(unvisited);
   this.source.getErrorCollector().addCollectorContents(errorCollector);
   return visited;
 }
Exemplo n.º 3
0
  private void markEnumOrInnerConstructorParameterAsSynthetic(MethodVisitor mv, int i) {
    // IDEA's ClsPsi builder fails to annotate synthetic parameters
    if (state.getClassBuilderMode() == ClassBuilderMode.LIGHT_CLASSES) return;

    // This is needed to avoid RuntimeInvisibleParameterAnnotations error in javac:
    // see MethodWriter.visitParameterAnnotation()

    AnnotationVisitor av = mv.visitParameterAnnotation(i, "Ljava/lang/Synthetic;", true);
    if (av != null) {
      av.visitEnd();
    }
  }
Exemplo n.º 4
0
 /**
  * Handles the writing of a single annotation to an annotation visitor.
  *
  * @param annotationVisitor The annotation visitor the write process is to be applied on.
  * @param annotation The annotation to be written.
  * @param annotationValueFilter The value filter to apply for discovering which values of an
  *     annotation should be written.
  */
 private static void handle(
     AnnotationVisitor annotationVisitor,
     AnnotationDescription annotation,
     AnnotationValueFilter annotationValueFilter) {
   for (MethodDescription.InDefinedShape methodDescription :
       annotation.getAnnotationType().getDeclaredMethods()) {
     if (annotationValueFilter.isRelevant(annotation, methodDescription)) {
       apply(
           annotationVisitor,
           methodDescription.getReturnType().asErasure(),
           methodDescription.getName(),
           annotation.getValue(methodDescription));
     }
   }
   annotationVisitor.visitEnd();
 }
Exemplo n.º 5
0
 /**
  * Performs the writing of a given annotation value to an annotation visitor.
  *
  * @param annotationVisitor The annotation visitor the write process is to be applied on.
  * @param valueType The type of the annotation value.
  * @param name The name of the annotation type.
  * @param value The annotation's value.
  */
 public static void apply(
     AnnotationVisitor annotationVisitor, TypeDescription valueType, String name, Object value) {
   if (valueType.isAnnotation()) {
     handle(
         annotationVisitor.visitAnnotation(name, valueType.getDescriptor()),
         (AnnotationDescription) value,
         AnnotationValueFilter.Default.APPEND_DEFAULTS);
   } else if (valueType.isEnum()) {
     annotationVisitor.visitEnum(
         name, valueType.getDescriptor(), ((EnumerationDescription) value).getValue());
   } else if (valueType.isAssignableFrom(Class.class)) {
     annotationVisitor.visit(name, Type.getType(((TypeDescription) value).getDescriptor()));
   } else if (valueType.isArray()) {
     AnnotationVisitor arrayVisitor = annotationVisitor.visitArray(name);
     int length = Array.getLength(value);
     TypeDescription componentType = valueType.getComponentType();
     for (int index = 0; index < length; index++) {
       apply(arrayVisitor, componentType, NO_NAME, Array.get(value, index));
     }
     arrayVisitor.visitEnd();
   } else {
     annotationVisitor.visit(name, value);
   }
 }
 @Nullable
 private static org.jetbrains.org.objectweb.asm.AnnotationVisitor convertAnnotationVisitor(
     @NotNull AnnotationVisitor visitor, @NotNull String desc) {
   AnnotationArgumentVisitor v = visitor.visitAnnotation(classNameFromAsmDesc(desc));
   return v == null ? null : convertAnnotationVisitor(v);
 }
Exemplo n.º 7
0
 /**
  * Makes the given method visitor visit this method.
  *
  * @param mv a method visitor.
  */
 public void accept(final MethodVisitor mv) {
   // visits the method parameters
   int i, j, n;
   n = parameters == null ? 0 : parameters.size();
   for (i = 0; i < n; i++) {
     ParameterNode parameter = parameters.get(i);
     mv.visitParameter(parameter.name, parameter.access);
   }
   // visits the method attributes
   if (annotationDefault != null) {
     AnnotationVisitor av = mv.visitAnnotationDefault();
     AnnotationNode.accept(av, null, annotationDefault);
     if (av != null) {
       av.visitEnd();
     }
   }
   n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
   for (i = 0; i < n; ++i) {
     AnnotationNode an = visibleAnnotations.get(i);
     an.accept(mv.visitAnnotation(an.desc, true));
   }
   n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
   for (i = 0; i < n; ++i) {
     AnnotationNode an = invisibleAnnotations.get(i);
     an.accept(mv.visitAnnotation(an.desc, false));
   }
   n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations.size();
   for (i = 0; i < n; ++i) {
     TypeAnnotationNode an = visibleTypeAnnotations.get(i);
     an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, true));
   }
   n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations.size();
   for (i = 0; i < n; ++i) {
     TypeAnnotationNode an = invisibleTypeAnnotations.get(i);
     an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, false));
   }
   n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length;
   for (i = 0; i < n; ++i) {
     List<?> l = visibleParameterAnnotations[i];
     if (l == null) {
       continue;
     }
     for (j = 0; j < l.size(); ++j) {
       AnnotationNode an = (AnnotationNode) l.get(j);
       an.accept(mv.visitParameterAnnotation(i, an.desc, true));
     }
   }
   n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length;
   for (i = 0; i < n; ++i) {
     List<?> l = invisibleParameterAnnotations[i];
     if (l == null) {
       continue;
     }
     for (j = 0; j < l.size(); ++j) {
       AnnotationNode an = (AnnotationNode) l.get(j);
       an.accept(mv.visitParameterAnnotation(i, an.desc, false));
     }
   }
   if (visited) {
     instructions.resetLabels();
   }
   n = attrs == null ? 0 : attrs.size();
   for (i = 0; i < n; ++i) {
     mv.visitAttribute(attrs.get(i));
   }
   // visits the method's code
   if (instructions.size() > 0) {
     mv.visitCode();
     // visits try catch blocks
     n = tryCatchBlocks == null ? 0 : tryCatchBlocks.size();
     for (i = 0; i < n; ++i) {
       tryCatchBlocks.get(i).updateIndex(i);
       tryCatchBlocks.get(i).accept(mv);
     }
     // visits instructions
     instructions.accept(mv);
     // visits local variables
     n = localVariables == null ? 0 : localVariables.size();
     for (i = 0; i < n; ++i) {
       localVariables.get(i).accept(mv);
     }
     // visits local variable annotations
     n = visibleLocalVariableAnnotations == null ? 0 : visibleLocalVariableAnnotations.size();
     for (i = 0; i < n; ++i) {
       visibleLocalVariableAnnotations.get(i).accept(mv, true);
     }
     n = invisibleLocalVariableAnnotations == null ? 0 : invisibleLocalVariableAnnotations.size();
     for (i = 0; i < n; ++i) {
       invisibleLocalVariableAnnotations.get(i).accept(mv, false);
     }
     // visits maxs
     mv.visitMaxs(maxStack, maxLocals);
     visited = true;
   }
   mv.visitEnd();
 }
 public void visitAnnotation(
     Clazz clazz, Method method, int parameterIndex, Annotation annotation) {
   if (accepted(annotation.getType(clazz))) {
     annotationVisitor.visitAnnotation(clazz, method, parameterIndex, annotation);
   }
 }
 public void visitAnnotation(Clazz clazz, Field field, Annotation annotation) {
   if (accepted(annotation.getType(clazz))) {
     annotationVisitor.visitAnnotation(clazz, field, annotation);
   }
 }