com.sun.codemodel.JAnnotationArrayMember Java Examples

The following examples show how to use com.sun.codemodel.JAnnotationArrayMember. 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: ImplementationBuilder.java    From aem-component-generator with Apache License 2.0 6 votes vote down vote up
private void addSlingAnnotations(JDefinedClass jDefinedClass, JClass adapterClass, String resourceType) {
    JAnnotationUse jAUse = jDefinedClass.annotate(codeModel.ref(Model.class));
    JAnnotationArrayMember adaptablesArray = jAUse.paramArray("adaptables");
    for (String adaptable : adaptables) {
        if ("resource".equalsIgnoreCase(adaptable)) {
            adaptablesArray.param(codeModel.ref(Resource.class));
        }
        if ("request".equalsIgnoreCase(adaptable)) {
            adaptablesArray.param(codeModel.ref(SlingHttpServletRequest.class));
        }
    }
    if (this.isAllowExporting) {
        jAUse.paramArray("adapters").param(adapterClass).param(codeModel.ref(ComponentExporter.class));
    } else {
        jAUse.param("adapters", adapterClass);
    }
    if (StringUtils.isNotBlank(resourceType)) {
        jAUse.param("resourceType", resourceType);
    }
    if (this.isAllowExporting) {
        jAUse = jDefinedClass.annotate(codeModel.ref(Exporter.class));
        jAUse.param("name", codeModel.ref(ExporterConstants.class).staticRef(SLING_MODEL_EXPORTER_NAME));
        jAUse.param("extensions", codeModel.ref(ExporterConstants.class).staticRef(SLING_MODEL_EXTENSION));
    }
}
 
Example #2
Source File: KubernetesCoreTypeAnnotator.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
protected void addClassesToPropertyFiles(JDefinedClass clazz) {
  String packageCategory = getPackageCategory(clazz.getPackage().name());
  if (moduleName.equals(packageCategory) /*&& shouldIncludeClass(clazz.name())*/) {
    JAnnotationArrayMember arrayMember = clazz.annotate(VelocityTransformations.class)
      .paramArray(ANNOTATION_VALUE);
    arrayMember.annotate(VelocityTransformation.class).param(ANNOTATION_VALUE, "/manifest.vm")
      .param("outputPath", moduleName + ".properties").param("gather", true);
  }
}
 
Example #3
Source File: AnnotatingVisitor.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public JAnnotationUse visitArrayAnnotationField(
		XArrayAnnotationField<?> field) {

	String fieldName = field.getName();
	final JAnnotationArrayMember annotationArrayMember = this.annotationUse
			.paramArray(fieldName);

	for (final XAnnotationValue<?> annotationValue : field
			.getAnnotationValues()) {
		annotationValue.accept(new AnnotatingArrayValueVisitor(
				this.codeModel, annotationArrayMember));
	}
	return this.annotationUse;
}
 
Example #4
Source File: ClassDiscoverer.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the annotations on the field to see if there is an XmlElements
 * annotation on it. If so, we'll check this annotation to see if it
 * refers to any classes that are external from our code schema compile.
 * If we find any, then we'll add them to our visitor.
 * @param outline root of the generated code
 * @param field parses the xml annotations looking for an external class
 * @param directClasses set of direct classes to append to
 * @throws IllegalAccessException throw if there's an error introspecting the annotations
 */
private static void parseXmlAnnotations(Outline outline, FieldOutline field, Set<String> directClasses) throws IllegalAccessException {
    if (field instanceof UntypedListField) {
        JFieldVar jfv = (JFieldVar) FieldHack.listField.get(field);
        for(JAnnotationUse jau : jfv.annotations()) {
            JClass jc = jau.getAnnotationClass();
            if (jc.fullName().equals(XmlElements.class.getName())) {
                JAnnotationArrayMember value = (JAnnotationArrayMember) jau.getAnnotationMembers().get("value");
                for(JAnnotationUse anno : value.annotations()) {
                    handleXmlElement(outline, directClasses, anno.getAnnotationMembers().get("type"));
                }
            }
        }
    }
}
 
Example #5
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception {
	JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME);
	JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME);
	JClass coreConstants = codeModel.ref(CoreConstants.class);
	JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements");
	
	// XmlRootElement
	JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class);
	rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD));
	
	// XmlAccessorType
	JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class);
	xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE);
	
	// XmlType
	JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class);
	xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD));
	JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder");
	for (FieldModel field : fields) {
		if (Util.isCommonElement(field.fieldName)) {
			propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName)));
		} else {
			propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName)));
		}
	}
	propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS"));
}
 
Example #6
Source File: JpaUnitRuleTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassWithPersistenceContextWithWithOverwrittenConfiguration() throws Exception {
    // GIVEN
    final JCodeModel jCodeModel = new JCodeModel();
    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
    final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
    ruleField.annotate(Rule.class);
    final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
    ruleField.init(instance);
    final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
    final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
    jAnnotation.param("unitName", "test-unit-1");
    final JAnnotationArrayMember propArray = jAnnotation.paramArray("properties");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.url").param("value",
            "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.driver").param("value", "org.h2.Driver");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.password").param("value", "test");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.user").param("value", "test");
    final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
    jMethod.annotate(Test.class);

    buildModel(testFolder.getRoot(), jCodeModel);
    compileModel(testFolder.getRoot());

    final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
    final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut);

    final RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
    verify(listener).testStarted(descriptionCaptor.capture());
    assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
    assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));

    verify(listener).testFinished(descriptionCaptor.capture());
    assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
    assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
 
Example #7
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassWithPersistenceContextWithWithOverwrittenConfiguration() throws Exception {
    // GIVEN
    final JCodeModel jCodeModel = new JCodeModel();
    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
    final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
    jAnnotationUse.param("value", JpaUnitRunner.class);
    final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
    final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
    jAnnotation.param("unitName", "test-unit-1");
    final JAnnotationArrayMember propArray = jAnnotation.paramArray("properties");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.url").param("value",
            "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.driver").param("value", "org.h2.Driver");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.password").param("value", "test");
    propArray.annotate(PersistenceProperty.class).param("name", "javax.persistence.jdbc.user").param("value", "test");
    final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
    jMethod.annotate(Test.class);

    buildModel(testFolder.getRoot(), jCodeModel);
    compileModel(testFolder.getRoot());

    final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    final RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
    verify(listener).testStarted(descriptionCaptor.capture());
    assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
    assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));

    verify(listener).testFinished(descriptionCaptor.capture());
    assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
    assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
 
Example #8
Source File: KubernetesCoreTypeAnnotator.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) {
  JAnnotationArrayMember annotationValue = clazz.annotate(JsonPropertyOrder.class).paramArray(ANNOTATION_VALUE);

  annotationValue.param(API_VERSION);
  annotationValue.param(KIND);
  annotationValue.param(METADATA);
  for (Iterator<String> properties = propertiesNode.fieldNames(); properties.hasNext();) {
    String next = properties.next();
    if (!next.equals(API_VERSION) && !next.equals(KIND) && !next.equals(METADATA)) {
      annotationValue.param(next);
    }
  }

  clazz.annotate(ToString.class);
  clazz.annotate(EqualsAndHashCode.class);
  processBuildable(clazz);

  if (clazz.fields().containsKey(KIND) && clazz.fields().containsKey(METADATA)) {
    String resourceName;

    if (clazz.name().endsWith("List")) {
      resourceName = clazz.name().substring(0, clazz.name().length() - 4);
      pendingLists.put(resourceName, clazz);
    } else {
      resourceName = clazz.name();
      pendingResources.put(clazz.name(), clazz);
    }
    if (pendingResources.containsKey(resourceName) && pendingLists.containsKey(resourceName)) {
      JDefinedClass resourceClass = pendingResources.get(resourceName);
      JDefinedClass resourceListClass = pendingLists.get(resourceName);

      String apiVersion = propertiesNode.get(API_VERSION).get("default").toString().replaceAll(Pattern.quote("\""), "");
      String apiGroup = "";
      if (apiVersion.contains("/")) {
        apiGroup = apiVersion.substring(0, apiVersion.lastIndexOf('/'));
        apiVersion = apiVersion.substring(apiGroup.length() + 1);
      }
      String packageSuffix = getPackageSuffix(apiVersion);

      resourceClass.annotate(ApiVersion.class).param(ANNOTATION_VALUE, apiVersion);
      resourceClass.annotate(ApiGroup.class).param(ANNOTATION_VALUE, apiGroup);
      resourceClass.annotate(PackageSuffix.class).param(ANNOTATION_VALUE, packageSuffix);
      resourceListClass.annotate(ApiVersion.class).param(ANNOTATION_VALUE, apiVersion);
      resourceListClass.annotate(ApiGroup.class).param(ANNOTATION_VALUE, apiGroup);
      resourceListClass.annotate(PackageSuffix.class).param(ANNOTATION_VALUE, packageSuffix);
      pendingLists.remove(resourceName);
      pendingResources.remove(resourceName);
      addClassesToPropertyFiles(resourceClass);
    }
  }
}
 
Example #9
Source File: AnnotatingArrayValueVisitor.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public AnnotatingArrayValueVisitor(JCodeModel codeModel,
		JAnnotationArrayMember annotationArrayMember) {
	super(codeModel, annotationArrayMember);
	this.annotationArrayMember = annotationArrayMember;
}
 
Example #10
Source File: AnnotatingArrayValueVisitor.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JAnnotationArrayMember visit(XStringAnnotationValue value) {
	return annotationArrayMember.param(StringUtils.normalizeString(value
			.getValue()));

}