Java Code Examples for com.sun.codemodel.JAnnotationUse#param()

The following examples show how to use com.sun.codemodel.JAnnotationUse#param() . 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: AnnotationInspectorTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void generateModel() throws Exception {
    final JCodeModel jCodeModel = new JCodeModel();
    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
    JAnnotationUse jAnnotationUse = jClass.annotate(InitialDataSets.class);
    jAnnotationUse.param("value", "Script.file");
    jClass.annotate(Cleanup.class);
    final JFieldVar jField = jClass.field(JMod.PRIVATE, String.class, "testField");
    jField.annotate(PersistenceContext.class);
    final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
    jAnnotationUse = jMethod.annotate(InitialDataSets.class);
    jAnnotationUse.param("value", "InitialDataSets.file");
    jAnnotationUse = jMethod.annotate(ApplyScriptsAfter.class);
    jAnnotationUse.param("value", "ApplyScriptsAfter.file");

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    cut = loadClass(testFolder.getRoot(), jClass.name());
}
 
Example 2
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 3
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceContextWithoutUnitNameSpecified() 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");
    emField.annotate(PersistenceContext.class);
    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 RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(JpaUnitException.class));
    assertThat(failure.getException().getMessage(), containsString("No Persistence"));
}
 
Example 4
Source File: SpringFeignClientClassAnnotationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JAnnotationUse apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) {
	JAnnotationUse feignClient = generatableType.annotate(FeignClient.class);

	feignClient.param("path", controllerMetadata.getControllerUrl());
	feignClient.param("name", getClientName(controllerMetadata));

	return feignClient;
}
 
Example 5
Source File: SpringRequestMappingClassAnnotationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JAnnotationUse apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) {
	JAnnotationUse requestMapping = generatableType.annotate(RequestMapping.class);
	requestMapping.param("value", controllerMetadata.getControllerUrl());
	try {
		String mediaType = generateMediaType(controllerMetadata);
		if (mediaType != null) {
			requestMapping.param("produces", mediaType);
		}
	} catch (Exception e) {
		throw new InvalidCodeModelException("Your model contains an invalid media type", e);
	}

	return requestMapping;
}
 
Example 6
Source File: RamlTypeHelper.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Adds appropriate <code>pattern</code> attribute to provided annotation on
 * {@link Date} property.
 * 
 * @param jAnnotationUse
 *            annotation to add pattern. Can be for: {@link JsonFormat} or
 *            {@link DateTimeFormat}
 * @param type
 *            RAML type of the property
 * @param format
 *            of date if specified
 */
public static void annotateDateWithPattern(JAnnotationUse jAnnotationUse, String type, String format) {

	String param = type.toUpperCase();
	switch (param) {
		case "DATE-ONLY":
			// example: 2013-09-29
			jAnnotationUse.param("pattern", "yyyy-MM-dd");
			break;
		case "TIME-ONLY":
			// example: 19:46:19
			jAnnotationUse.param("pattern", "HH:mm:ss");
			break;
		case "DATETIME-ONLY":
			// example: 2013-09-29T19:46:19
			jAnnotationUse.param("pattern", "yyyy-MM-dd'T'HH:mm:ss");
			break;
		case "DATETIME":
			if ("rfc2616".equalsIgnoreCase(format)) {
				// example: Tue, 15 Nov 1994 12:45:26 GMT
				jAnnotationUse.param("pattern", "EEE, dd MMM yyyy HH:mm:ss z");
			} else {
				jAnnotationUse.param("pattern", "yyyy-MM-dd'T'HH:mm:ssXXX");
			}
			break;
		default:
			jAnnotationUse.param("pattern", "yyyy-MM-dd'T'HH:mm:ss");
	}
}
 
Example 7
Source File: SpringShortcutMappingMethodAnnotationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JAnnotationUse apply(ApiActionMetadata endpointMetadata, JMethod generatableType) {
	JAnnotationUse requestMappingAnnotation;
	switch (RequestMethod.valueOf(endpointMetadata.getActionType().name())) {
		case GET:
			requestMappingAnnotation = generatableType.annotate(GetMapping.class);
			break;
		case POST:
			requestMappingAnnotation = generatableType.annotate(PostMapping.class);
			break;
		case PUT:
			requestMappingAnnotation = generatableType.annotate(PutMapping.class);
			break;
		case PATCH:
			requestMappingAnnotation = generatableType.annotate(PatchMapping.class);
			break;
		case DELETE:
			requestMappingAnnotation = generatableType.annotate(DeleteMapping.class);
			break;
		default:
			requestMappingAnnotation = generatableType.annotate(RequestMapping.class);
			requestMappingAnnotation.param("method", RequestMethod.valueOf(endpointMetadata.getActionType().name()));
	}

	if (StringUtils.isNotBlank(endpointMetadata.getUrl())) {
		requestMappingAnnotation.param("value", endpointMetadata.getUrl());
	}
	return requestMappingAnnotation;
}
 
Example 8
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderField(JDefinedClass classModel, FieldModel fieldModel) {
	JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName);
	JAnnotationUse annotation = field.annotate(XmlElement.class);
	if (Util.isCommonElement(fieldModel.fieldName)) {
		JClass coreConstants = codeModel.ref(CoreConstants.class);
		JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements");
		annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName)));
	} else {
		JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME);
		JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName));
		annotation.param("name", fieldXmlNameRef);
	}
	annotation.param("required", false);
}
 
Example 9
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderFutureElementsField(JDefinedClass classModel) throws Exception {
	JType collectionType = codeModel.parseType("java.util.Collection<org.w3c.dom.Element>");
	JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, collectionType, "_futureElements");
	field.init(JExpr._null());
	JAnnotationUse annotation = field.annotate(SuppressWarnings.class);
	annotation.param("value", "unused");
	field.annotate(XmlAnyElement.class);
}
 
Example 10
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() 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 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 11
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceUnitWithoutUnitNameSpecified() 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, EntityManagerFactory.class, "emf");
    emField.annotate(PersistenceUnit.class);
    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 RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(JpaUnitException.class));
    assertThat(failure.getException().getMessage(), containsString("No Persistence"));
}
 
Example 12
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceUnitFieldOfWrongType() 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, "emf");
    emField.annotate(PersistenceUnit.class);
    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 RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class));
    assertThat(failure.getException().getMessage(),
            containsString("annotated with @PersistenceUnit is not of type EntityManagerFactory"));
}
 
Example 13
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 14
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceContextAndPersistenceUnitFields() 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 emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
    emf1Field.annotate(PersistenceContext.class);
    final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf");
    emf2Field.annotate(PersistenceUnit.class);
    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 RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class));
    assertThat(failure.getException().getMessage(), containsString("either @PersistenceUnit or @PersistenceContext"));
}
 
Example 15
Source File: JaxbValidationsPlugins.java    From krasa-jaxb-tools with Apache License 2.0 5 votes vote down vote up
private void annotateMultiplePattern(final List<XSFacet> patternList, final JAnnotationUse patternAnnotation) {
    StringBuilder sb = new StringBuilder();
    for (XSFacet xsFacet : patternList) {
        final String value = xsFacet.getValue().value;
        // cxf-codegen fix
        if (!"\\c+".equals(value)) {
            sb.append("(").append(replaceXmlProprietals(value)).append(")|");
        }
    }
    patternAnnotation.param("regexp", sb.substring(0, sb.length() - 1));
}
 
Example 16
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithoutPersistenceContextField() 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 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 RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class));
    assertThat(failure.getException().getMessage(), containsString("EntityManagerFactory or EntityManager field annotated"));
}
 
Example 17
Source File: JpaUnitRuleTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithPersistenceUnitWithKonfiguredUnitNameSpecified() 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, EntityManagerFactory.class, "emf");
    final JAnnotationUse jAnnotation = emField.annotate(PersistenceUnit.class);
    jAnnotation.param("unitName", "test-unit-1");
    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 18
Source File: MethodParamsRule.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
protected JVar paramQueryForm(ApiParameterMetadata paramMetaData, CodeModelHelper.JExtMethod generatableType,
		ApiActionMetadata endpointMetadata) {

	String javaName = paramMetaData.getJavaName();

	if (addParameterJavadoc) {
		String paramComment = "";
		if (paramMetaData.getRamlParam() != null && StringUtils.hasText(paramMetaData.getRamlParam().getDescription())) {
			paramComment = NamingHelper.cleanForJavadoc(paramMetaData.getRamlParam().getDescription());
		}
		generatableType.get().javadoc().addParam(javaName + " " + paramComment);
	}

	JClass type = null;
	if (paramMetaData.getRamlParam() instanceof RJP10V2RamlUriParameter && paramMetaData.isNullable()) {
		// for optional uri parameters use java.util.Optional since Spring
		// doesn't support optional uri parameters
		type = generatableType.owner().ref(Optional.class).narrow(paramMetaData.getType());
	}
	if (type == null) {
		type = generatableType.owner().ref(paramMetaData.getType());
	}

	if (!allowArrayParameters && paramMetaData.isArray()) {
		type = generatableType.owner().ref(paramMetaData.getType().getComponentType());
	} else {
		// TODO should this be blank?
	}

	JVar jVar = null;

	// data types as query parameters
	RamlAbstractParam ramlParam = paramMetaData.getRamlParam();
	if (ramlParam.getType() == RamlParamType.DATA_TYPE && ramlParam instanceof RJP10V2RamlQueryParameter) {
		JClass jc = findFirstClassBySimpleName(paramMetaData.getCodeModel(), ramlParam.getRawType());
		jVar = generatableType.get().param(jc, paramMetaData.getName());

		if (Config.getPojoConfig().isIncludeJsr303Annotations() && !RamlActionType.PATCH.equals(endpointMetadata.getActionType())
				&& jVar.type() instanceof JDefinedClass) {
			// skip Valid annotation for PATCH actions since it's a partial
			// update so some required fields might be omitted
			boolean isPOJO = ((JDefinedClass) jVar.type())._package().name().startsWith(Config.getBasePackage());
			if (isPOJO) {
				jVar.annotate(Valid.class);
			}
		}
		return jVar;
	}

	jVar = generatableType.get().param(type, javaName);
	if (paramMetaData.getRamlParam().getPattern() != null) {
		jVar.annotate(Pattern.class).param("regexp", paramMetaData.getRamlParam().getPattern());
	}

	if (paramMetaData.getRamlParam().getMinLength() != null || paramMetaData.getRamlParam().getMaxLength() != null) {
		JAnnotationUse jAnnotationUse = jVar.annotate(Size.class);
		if (paramMetaData.getRamlParam().getMinLength() != null) {
			jAnnotationUse.param("min", paramMetaData.getRamlParam().getMinLength());
		}
		if (paramMetaData.getRamlParam().getMaxLength() != null) {
			jAnnotationUse.param("max", paramMetaData.getRamlParam().getMaxLength());
		}
	}

	if (paramMetaData.getRamlParam().getMinimum() != null) {
		jVar.annotate(Min.class).param("value", paramMetaData.getRamlParam().getMinimum().longValue());
	}
	if (paramMetaData.getRamlParam().getMaximum() != null) {
		jVar.annotate(Max.class).param("value", paramMetaData.getRamlParam().getMaximum().longValue());
	}

	return jVar;
}
 
Example 19
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 20
Source File: CodeCreator.java    From jaxb-visitor with Apache License 2.0 4 votes vote down vote up
void setOutput(JDefinedClass output) {
    JAnnotationUse annotationUse = output.annotate(Generated.class);
    annotationUse.param("value", "Generated by jaxb-visitor");
    this.output = output;
}