Java Code Examples for javax.lang.model.element.ExecutableElement#getDefaultValue()

The following examples show how to use javax.lang.model.element.ExecutableElement#getDefaultValue() . 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: AbstractVerifier.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
protected static AnnotationValue findAnnotationValue(AnnotationMirror mirror, String name) {
    ExecutableElement valueMethod = null;
    for (ExecutableElement method : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
        if (method.getSimpleName().toString().equals(name)) {
            valueMethod = method;
            break;
        }
    }

    if (valueMethod == null) {
        return null;
    }

    AnnotationValue value = mirror.getElementValues().get(valueMethod);
    if (value == null) {
        value = valueMethod.getDefaultValue();
    }

    return value;
}
 
Example 2
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoubleValue() throws IOException {
  compile(Joiner.on('\n').join("public @interface Foo {", "  double value() default 42;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitDouble(double d, Void aVoid) {
          assertEquals(42.0, method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), d);
          return null;
        }
      },
      null);
  assertEquals("42.0", defaultValue.toString());
}
 
Example 3
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testLongValue() throws IOException {
  compile(Joiner.on('\n').join("public @interface Foo {", "  long value() default 42;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitLong(long l, Void aVoid) {
          assertEquals(42L, method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), l);
          return null;
        }
      },
      null);
  assertEquals("42L", defaultValue.toString());
}
 
Example 4
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntegerValue() throws IOException {
  compile(Joiner.on('\n').join("public @interface Foo {", "  int value() default 42;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitInt(int i, Void aVoid) {
          assertEquals(42, method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), i);
          return null;
        }
      },
      null);
  assertEquals("42", defaultValue.toString());
}
 
Example 5
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testShortValue() throws IOException {
  compile(Joiner.on('\n').join("public @interface Foo {", "  short value() default 42;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitShort(short s, Void aVoid) {
          assertEquals((short) 42, method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), s);
          return null;
        }
      },
      null);
  assertEquals("42", defaultValue.toString());
}
 
Example 6
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testCharValue() throws IOException {
  compile(Joiner.on('\n').join("public @interface Foo {", "  char value() default 'x';", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitChar(char c, Void aVoid) {
          assertEquals('x', method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), c);
          return null;
        }
      },
      null);
  assertEquals("'x'", defaultValue.toString());
}
 
Example 7
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassValueBuiltinType() throws IOException {
  compile(
      Joiner.on('\n')
          .join("public @interface Foo {", "  Class value() default String.class;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitType(TypeMirror t, Void aVoid) {
          assertSameType(elements.getTypeElement("java.lang.String").asType(), t);
          assertSame(t, defaultValue.getValue());
          return null;
        }
      },
      null);
  assertEquals("java.lang.String.class", defaultValue.toString());
}
 
Example 8
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testBooleanValue() throws IOException {
  compile(
      Joiner.on('\n').join("public @interface Foo {", "  boolean value() default true;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitBoolean(boolean b, Void aVoid) {
          assertTrue((boolean) defaultValue.getValue());
          assertEquals(defaultValue.getValue(), b);
          return null;
        }
      },
      null);
  assertEquals("true", defaultValue.toString());
}
 
Example 9
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayValue() throws IOException {
  compile(
      Joiner.on('\n')
          .join("public @interface Foo {", "  char[] value() default {'4', '2'};", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitArray(List<? extends AnnotationValue> vals, Void aVoid) {
          assertEquals(vals, defaultValue.getValue());
          assertThat(
              vals.stream().map(AnnotationValue::getValue).collect(Collectors.toList()),
              Matchers.contains('4', '2'));
          return null;
        }
      },
      null);

  assertEquals("{'4', '2'}", defaultValue.toString());
}
 
Example 10
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveClassValue() throws IOException {
  compile(
      Joiner.on('\n').join("public @interface Foo {", "  Class value() default int.class;", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitType(TypeMirror t, Void aVoid) {
          assertSameType(types.getPrimitiveType(TypeKind.INT), t);
          assertSame(t, defaultValue.getValue());
          return null;
        }
      },
      null);
  assertEquals("int.class", defaultValue.toString());
}
 
Example 11
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalEnumValue() throws IOException {
  compile(
      Joiner.on('\n')
          .join(
              "import java.lang.annotation.*;",
              "public @interface Foo {",
              "  FooEnum value() default FooEnum.Bar;",
              "}",
              "enum FooEnum { Foo, Bar, Baz };"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  VariableElement sourceField = findField("Bar", elements.getTypeElement("FooEnum"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitEnumConstant(VariableElement constant, Void aVoid) {
          assertSame(sourceField, constant);
          assertSame(constant, defaultValue.getValue());
          return null;
        }
      },
      null);
  assertEquals("FooEnum.Bar", defaultValue.toString());
}
 
Example 12
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringValue() throws IOException {
  compile(
      Joiner.on('\n').join("public @interface Foo {", "  String value() default \"42\";", "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitString(String s, Void aVoid) {
          assertEquals("42", method.getDefaultValue().getValue());
          assertEquals(defaultValue.getValue(), s);
          return null;
        }
      },
      null);
  assertEquals("\"42\"", defaultValue.toString());
}
 
Example 13
Source File: ClassVisitorDriverFromElement.java    From buck with Apache License 2.0 5 votes vote down vote up
private void visitDefaultValue(ExecutableElement e, MethodVisitor methodVisitor) {
  AnnotationValue defaultValue = e.getDefaultValue();
  if (defaultValue == null) {
    return;
  }

  AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotationDefault();
  visitAnnotationValue(null, defaultValue, annotationVisitor);
  annotationVisitor.visitEnd();
}
 
Example 14
Source File: ElementUtil.java    From j2objc with Apache License 2.0 5 votes vote down vote up
Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValuesWithDefaults(
    AnnotationMirror annotation) {
  DeclaredType type = annotation.getAnnotationType();
  Map<ExecutableElement, AnnotationValue> map = new LinkedHashMap<>(
      annotation.getElementValues());
  for (ExecutableElement method : getMethods((TypeElement) type.asElement())) {
    AnnotationValue defaultValue = method.getDefaultValue();
    if (defaultValue != null && !map.containsKey(method)) {
      map.put(method, defaultValue);
    }
  }
  return map;
}
 
Example 15
Source File: SuperficialValidation.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override public Boolean visitExecutable(ExecutableElement e, Void p) {
  AnnotationValue defaultValue = e.getDefaultValue();
  return isValidBaseElement(e)
      && (defaultValue == null || validateAnnotationValue(defaultValue, e.getReturnType()))
      && validateType(e.getReturnType())
      && validateTypes(e.getThrownTypes())
      && validateElements(e.getTypeParameters())
      && validateElements(e.getParameters());
}
 
Example 16
Source File: SimpleAnnotationMirror.java    From auto with Apache License 2.0 5 votes vote down vote up
private SimpleAnnotationMirror(
    TypeElement annotationType, Map<String, ? extends AnnotationValue> namedValues) {
  checkArgument(
      annotationType.getKind().equals(ElementKind.ANNOTATION_TYPE),
      "annotationType must be an annotation: %s",
      annotationType);
  Map<String, AnnotationValue> values = new LinkedHashMap<>();
  Map<String, AnnotationValue> unusedValues = new LinkedHashMap<>(namedValues);
  List<String> missingMembers = new ArrayList<>();
  for (ExecutableElement method : methodsIn(annotationType.getEnclosedElements())) {
    String memberName = method.getSimpleName().toString();
    if (unusedValues.containsKey(memberName)) {
      values.put(memberName, unusedValues.remove(memberName));
    } else if (method.getDefaultValue() != null) {
      values.put(memberName, method.getDefaultValue());
    } else {
      missingMembers.add(memberName);
    }
  }
  
  checkArgument(
      unusedValues.isEmpty(),
      "namedValues has entries for members that are not in %s: %s",
      annotationType,
      unusedValues);
  checkArgument(
      missingMembers.isEmpty(), "namedValues is missing entries for: %s", missingMembers);

  this.annotationType = annotationType;
  this.namedValues = ImmutableMap.copyOf(namedValues);
  this.elementValues =
      methodsIn(annotationType.getEnclosedElements())
          .stream()
          .collect(toImmutableMap(e -> e, e -> values.get(e.getSimpleName().toString())));
}
 
Example 17
Source File: ElementTo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
public Method apply(ExecutableElement executableElement) {
    Map<AttributeKey, Object> attributes = new HashMap<>();
    if (executableElement.getDefaultValue() != null) {
       attributes.put(Attributeable.DEFAULT_VALUE, executableElement.getDefaultValue().getValue());
    }
    MethodBuilder methodBuilder = new MethodBuilder()
            .withModifiers(TypeUtils.modifiersToInt(executableElement.getModifiers()))
            .withName(executableElement.getSimpleName().toString())
            .withReturnType(MIRROR_TO_TYPEREF.apply(executableElement.getReturnType()))
            .withAttributes(attributes);



    //Populate constructor parameters
    for (VariableElement variableElement : executableElement.getParameters()) {
        methodBuilder = methodBuilder.addToArguments(PROPERTY.apply(variableElement));

        List<ClassRef> exceptionRefs = new ArrayList<ClassRef>();
        for (TypeMirror thrownType : executableElement.getThrownTypes()) {
            if (thrownType instanceof ClassRef) {
                exceptionRefs.add((ClassRef) thrownType);
            }
        }
        methodBuilder = methodBuilder.withExceptions(exceptionRefs);
    }

    List<ClassRef> annotationRefs = new ArrayList<ClassRef>();
    for (AnnotationMirror annotationMirror : executableElement.getAnnotationMirrors()) {
        methodBuilder.withAnnotations(ANNOTATION_REF.apply(annotationMirror));
    }
    return methodBuilder.build();
}
 
Example 18
Source File: VisibleMemberMap.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Filter the annotation type members and return either the required
 * members or the optional members, depending on the value of the
 * required parameter.
 *
 * @param typeElement The annotation type to process.
 * @param required
 * @return the annotation type members and return either the required
 * members or the optional members, depending on the value of the
 * required parameter.
 */
private List<Element> filterAnnotations(TypeElement typeElement, boolean required) {
    List<Element> members = utils.getAnnotationMethods(typeElement);
    List<Element> targetMembers = new ArrayList<>();
    for (Element member : members) {
        ExecutableElement ee = (ExecutableElement)member;
        if ((required && ee.getDefaultValue() == null)
                || ((!required) && ee.getDefaultValue() != null)) {
            targetMembers.add(member);
        }
    }
    return targetMembers;
}
 
Example 19
Source File: AnnotationTypeOptionalMemberWriterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void addDefaultValueInfo(Element member, Content annotationDocTree) {
    if (utils.isAnnotationType(member)) {
        ExecutableElement ee = (ExecutableElement)member;
        AnnotationValue value = ee.getDefaultValue();
        if (value != null) {
            Content dt = HtmlTree.DT(contents.default_);
            Content dl = HtmlTree.DL(dt);
            Content dd = HtmlTree.DD(new StringContent(value.toString()));
            dl.addContent(dd);
            annotationDocTree.addContent(dl);
        }
    }
}
 
Example 20
Source File: TreeBackedAnnotationValueTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassArrayValue() throws IOException {
  compile(
      Joiner.on('\n')
          .join(
              "public @interface Foo {",
              "  Class[] value() default {int.class, Foo.class};",
              "}"));

  ExecutableElement method = findMethod("value", elements.getTypeElement("Foo"));
  AnnotationValue defaultValue = method.getDefaultValue();
  defaultValue.accept(
      new TestVisitor() {
        @Override
        public Void visitArray(List<? extends AnnotationValue> vals, Void aVoid) {
          assertEquals(vals, defaultValue.getValue());
          assertSameType(
              types.getPrimitiveType(TypeKind.INT), (TypeMirror) vals.get(0).getValue());

          assertSameType(
              elements.getTypeElement("Foo").asType(), (TypeMirror) vals.get(1).getValue());
          return null;
        }
      },
      null);
  assertEquals("{int.class, Foo.class}", defaultValue.toString());
}