jdk.internal.org.objectweb.asm.tree.AnnotationNode Java Examples

The following examples show how to use jdk.internal.org.objectweb.asm.tree.AnnotationNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventInstrumentation.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: EventInstrumentation.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example #3
Source File: EventInstrumentation.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example #4
Source File: AnnotationChecker.java    From NullAway with MIT License 6 votes vote down vote up
/**
 * If the given method matches the expected test method name 'expectNonnull', check if all the
 * parameters of the method has the 'javax.annotation.Nonnull' annotation on it exactly once. All
 * such methods are also expected to have at least one parameter with this annotation.
 *
 * @param method method to be checked.
 * @return True if 'javax.annotation.Nonnull' is present exactly once on all the parameters of
 *     matching methods.
 */
private static boolean checkTestMethodParamAnnotationByName(MethodNode method) {
  if (method.name.equals(expectNonnullParamsMethod)) {
    int numParameters = Type.getArgumentTypes(method.desc).length;
    if (numParameters == 0
        || method.visibleParameterAnnotations == null
        || method.visibleParameterAnnotations.length < numParameters) {
      return false;
    }
    for (List<AnnotationNode> annotations : method.visibleParameterAnnotations) {
      if (countAnnotations(annotations, BytecodeAnnotator.javaxNonnullDesc) != 1) {
        return false;
      }
    }
  }
  return true;
}
 
Example #5
Source File: AnnotationChecker.java    From NullAway with MIT License 6 votes vote down vote up
private static boolean checkExpectedAnnotation(
    List<AnnotationNode> annotations, String expectAnnotation, String actualAnnotation) {
  if (containsAnnotation(annotations, expectAnnotation)) {
    int numAnnotationsFound = countAnnotations(annotations, actualAnnotation);
    if (numAnnotationsFound != 1) {
      System.out.println(
          "Error: Annotation '"
              + actualAnnotation
              + "' was found "
              + numAnnotationsFound
              + " times.");
      return false;
    }
    return true;
  }
  return !containsAnnotation(annotations, actualAnnotation);
}
 
Example #6
Source File: AnnotationChecker.java    From NullAway with MIT License 5 votes vote down vote up
private static boolean checkMethodAnnotationsInClass(
    InputStream is, Map<String, String> expectedToActualAnnotations) throws IOException {
  ClassReader cr = new ClassReader(is);
  ClassNode cn = new ClassNode();
  cr.accept(cn, 0);

  for (MethodNode method : cn.methods) {
    if (!checkExpectedAnnotations(method.visibleAnnotations, expectedToActualAnnotations)
        && !checkTestMethodAnnotationByName(method)) {
      System.out.println(
          "Error: Invalid / Unexpected annotations found on method '" + method.name + "'");
      return false;
    }
    List<AnnotationNode>[] paramAnnotations = method.visibleParameterAnnotations;
    if (paramAnnotations == null) continue;
    for (List<AnnotationNode> annotations : paramAnnotations) {
      if (!checkExpectedAnnotations(annotations, expectedToActualAnnotations)
          && !checkTestMethodParamAnnotationByName(method)) {
        System.out.println(
            "Error: Invalid / Unexpected annotations found in a parameter of method '"
                + method.name
                + "'.");
        return false;
      }
    }
  }
  return true;
}
 
Example #7
Source File: AnnotationChecker.java    From NullAway with MIT License 5 votes vote down vote up
private static boolean checkExpectedAnnotations(
    List<AnnotationNode> annotations, Map<String, String> expectedToActualAnnotations) {
  for (Map.Entry<String, String> item : expectedToActualAnnotations.entrySet()) {
    if (!checkExpectedAnnotation(annotations, item.getKey(), item.getValue())) {
      return false;
    }
  }
  return true;
}
 
Example #8
Source File: AnnotationChecker.java    From NullAway with MIT License 5 votes vote down vote up
private static int countAnnotations(List<AnnotationNode> annotations, String annotation) {
  if (annotations == null) return 0;
  int count = 0;
  for (AnnotationNode annotationNode : annotations) {
    if (annotationNode.desc.equals(annotation)) {
      count++;
    }
  }
  return count;
}
 
Example #9
Source File: AnnotationChecker.java    From NullAway with MIT License 4 votes vote down vote up
private static boolean containsAnnotation(List<AnnotationNode> annotations, String annotation) {
  return countAnnotations(annotations, annotation) > 0;
}