Java Code Examples for com.sun.codemodel.JDefinedClass#_implements

The following examples show how to use com.sun.codemodel.JDefinedClass#_implements . 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: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 6 votes vote down vote up
private byte[] generateJava() throws Exception {
	
	JDefinedClass classModel = codeModel._class(JMod.PUBLIC | JMod.FINAL, className, ClassType.CLASS);
	Class<?> contractInterface = Class.forName(contractInterfaceName);
	classModel._implements(contractInterface);
          classModel._extends(AbstractDataTransferObject.class);
	
	List<FieldModel> fields = determineFields(contractInterface);
	
	renderConstantsClass(classModel);
	renderElementsClass(classModel, fields);
	renderClassLevelAnnotations(classModel, fields);
	renderFields(classModel, fields);
	renderFutureElementsField(classModel);			
	renderPrivateJaxbConstructor(classModel, fields);
	renderBuilderConstructor(classModel, fields);
	renderGetters(classModel, fields);
	renderBuilderClass(classModel, fields, contractInterface);

	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	codeModel.build(new SingleStreamCodeWriter(outputStream));
	return outputStream.toByteArray();
	
}
 
Example 2
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void createReportingValueClass(JCodeModel codeModel, Class<? extends Value<?>> valueClazz, JPrimitiveType type) throws JClassAlreadyExistsException {
	JClass reportClazz = codeModel.ref(Report.class);

	JDefinedClass reportingValueClazz = codeModel._class(JMod.PUBLIC, asReportingClass(valueClazz), ClassType.CLASS);
	reportingValueClazz._extends(codeModel.ref(valueClazz));
	reportingValueClazz._implements(codeModel.ref(HasReport.class));

	JFieldVar reportField = reportingValueClazz.field(JMod.PRIVATE, reportClazz, "report", JExpr._null());

	createCopyMethod(reportingValueClazz);
	createOperationMethods(reportingValueClazz, valueClazz, type);
	createReportMethod(reportingValueClazz);
	createExpressionMethods(reportingValueClazz);
	createAccessorMethods(reportingValueClazz, reportField);
	createFormatMethod(reportingValueClazz, type);
}
 
Example 3
Source File: SourceGenerator.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private void recurseImplementations(final JDefinedClass impl, final JClass interfaceClass) {
    // implement the supplied interface class.
    impl._implements(interfaceClass);
    if (interfaceClass instanceof JDefinedClass) {
        // If the interface is a JDefinedClass (will be), then we'll recurse
        // up the lineage.
        generateFieldAccessorsForEachInterfaceMethod(impl, (JDefinedClass) interfaceClass);
        interfaceClass._implements().forEachRemaining(parentInterface -> {
            recurseImplementations(impl, parentInterface);
        });
    }
}
 
Example 4
Source File: SpringRulesTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDelegateFieldDeclarationRule_shouldCreate_validAutowiredField() throws JClassAlreadyExistsException {
	SpringDelegateFieldDeclerationRule rule = new SpringDelegateFieldDeclerationRule("delegate");

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jClass = jPackage._class(JMod.PUBLIC, "MyClass");
	jClass._implements(Serializable.class);
	rule.apply(getControllerMetadata(), jClass);

	assertThat(serializeModel(), containsString("import org.springframework.beans.factory.annotation.Autowired;"));
	assertThat(serializeModel(), containsString("@Autowired"));
	assertThat(serializeModel(), containsString("private Serializable delegate;"));
}
 
Example 5
Source File: ClassFieldDeclarationRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyClassFieldDeclarationRule_shouldCreate_validAutowiredField() throws JClassAlreadyExistsException {
	ClassFieldDeclarationRule rule = new ClassFieldDeclarationRule("field", String.class);

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jClass = jPackage._class(JMod.PUBLIC, "MyClass");
	jClass._implements(Serializable.class);
	rule.apply(getControllerMetadata(), jClass);

	assertThat(serializeModel(), containsString("import org.springframework.beans.factory.annotation.Autowired;"));
	assertThat(serializeModel(), containsString("@Autowired"));
	assertThat(serializeModel(), containsString("private String field;"));
}
 
Example 6
Source File: ClassFieldDeclarationRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyClassFieldDeclarationRule_shouldCreate_validNonAutowiredField() throws JClassAlreadyExistsException {
	ClassFieldDeclarationRule rule = new ClassFieldDeclarationRule("field", String.class, false);

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jClass = jPackage._class(JMod.PUBLIC, "MyClass");
	jClass._implements(Serializable.class);
	rule.apply(getControllerMetadata(), jClass);

	assertFalse(serializeModel().contains("import org.springframework.beans.factory.annotation.Autowired;"));
	assertFalse((serializeModel().contains("@Autowired")));
	assertThat(serializeModel(), containsString("private String field;"));
}
 
Example 7
Source File: ClassFieldDeclarationRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyClassFieldDeclarationRule_shouldCreate_validValueAnnotedField() throws JClassAlreadyExistsException {
	ClassFieldDeclarationRule rule = new ClassFieldDeclarationRule("field", String.class, "${sample}");

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jClass = jPackage._class(JMod.PUBLIC, "MyClass");
	jClass._implements(Serializable.class);
	rule.apply(getControllerMetadata(), jClass);

	assertFalse(serializeModel().contains("import org.springframework.beans.factory.annotation.Autowired;"));
	assertFalse((serializeModel().contains("@Autowired")));
	assertThat(serializeModel(), containsString("@Value(\"${sample}\")"));
	assertThat(serializeModel(), containsString("private String field;"));
}
 
Example 8
Source File: ClassUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean isImplementing(JDefinedClass theClass,
		JClass theInterface) {
	for (Iterator<JClass> iterator = theClass._implements(); iterator
			.hasNext();) {
		final JClass implementedInterface = iterator.next();
		if (theInterface.equals(implementedInterface)) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: JClassUtilsTest.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void correctlyChecksIsInstanceOf()
		throws JClassAlreadyExistsException {

	final JClass arrayList = codeModel.ref("java.util.ArrayList");
	Assert.assertTrue(JClassUtils.isInstanceOf(arrayList, Collection.class));
	final JDefinedClass subArrayList = codeModel._class("SubArrayList");
	subArrayList._extends(arrayList);
	Assert.assertTrue(JClassUtils.isInstanceOf(subArrayList,
			Collection.class));

	final JClass subArrayListOfObjects = subArrayList.narrow(Object.class);
	Assert.assertTrue(JClassUtils.isInstanceOf(subArrayListOfObjects,
			Collection.class));

	final JDefinedClass subExternalizable = codeModel
			._class("SubExternalizable");
	subExternalizable._implements(Externalizable.class);
	Assert.assertTrue(JClassUtils.isInstanceOf(subExternalizable,
			Externalizable.class));

	subArrayList._implements(subExternalizable);
	Assert.assertTrue(JClassUtils.isInstanceOf(subArrayList,
			Externalizable.class));

	Assert.assertFalse(JClassUtils.isInstanceOf(codeModel.NULL,
			Collection.class));

}
 
Example 10
Source File: InheritancePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private JClass generateImplements(final JDefinedClass theClass,
		final ImplementsInterface implementsInterface,
		Map<String, JClass> knownClasses) {

	String _interface = implementsInterface.getInterfaceName();
	if (_interface != null) {
		final JClass targetClass = parseClass(_interface, theClass.owner(),
				knownClasses);
		theClass._implements(targetClass);
		return targetClass;
	} else {
		return null;
	}
}
 
Example 11
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderBuilderClass(JDefinedClass classModel, List<FieldModel> fields, Class<?> contractInterface) throws Exception {
	
	// define constants class
	JDefinedClass builderClass = classModel._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, Util.BUILDER_CLASS_NAME);
	
	// create a literal version of the Builder class so that the code generator won't pre-pend Builder class references with outermost class
	JClass literalBuilderClass = codeModel.ref("Builder");
	
	// generate the javadoc on the top of the Elements class
	JDocComment javadoc = builderClass.javadoc();
	javadoc.append(Util.generateBuilderJavadoc(classModel.name(), contractInterface.getSimpleName()));
	
	builderClass._implements(contractInterface);
	builderClass._implements(ModelBuilder.class);
	builderClass._implements(Serializable.class);
	
	// render the builder fields
	for (FieldModel fieldModel : fields) {
		builderClass.field(JMod.PRIVATE, fieldModel.fieldType, fieldModel.fieldName);
	}
	
	// render default empty constructor for builder
	JMethod constructor = builderClass.constructor(JMod.PRIVATE);
	constructor.body().directStatement("// TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods");

	renderBuilderDefaultCreate(builderClass, literalBuilderClass);
	renderBuilderCreateContract(builderClass, literalBuilderClass, fields, contractInterface);
	renderBuild(builderClass);
	renderGetters(builderClass, fields);
	renderSetters(builderClass, fields);
}
 
Example 12
Source File: JaxRsEnumRule.java    From apicurio-studio with Apache License 2.0 4 votes vote down vote up
private void addInterfaces(JDefinedClass jclass, JsonNode javaInterfaces) {
    for (JsonNode i : javaInterfaces) {
        jclass._implements(resolveType(jclass._package(), i.asText()));
    }
}
 
Example 13
Source File: ImplementsControllerInterfaceRule.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public JDefinedClass apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) {
	return generatableType._implements(this.interfaceType);
}
 
Example 14
Source File: ClassUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void _implements(JDefinedClass theClass, JClass theInterface) {
	if (!isImplementing(theClass, theInterface))
		theClass._implements(theInterface);
}
 
Example 15
Source File: AutoInheritancePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void generateImplements(JDefinedClass theClass, String name) {
	if (name != null) {
		final JClass targetClass = theClass.owner().ref(name);
		theClass._implements(targetClass);
	}
}
 
Example 16
Source File: CreateJAXBElementNameCallback.java    From jaxb-visitor with Apache License 2.0 4 votes vote down vote up
@Override
protected void run(Set<ClassOutline> classes, Set<JClass> directClasses) {
    setOutput(outline.getClassFactory().createInterface(jpackage, "Named", null));
    getOutput().method(JMod.PUBLIC, void.class, SETTER).param(QName.class, "name");
    getOutput().method(JMod.PUBLIC, QName.class, GETTER);

    Set<ClassOutline> named = onlyNamed(outline, classes);

    JClass jaxbElementClass = outline.getCodeModel().ref(JAXBElement.class).narrow(outline.getCodeModel().ref(Object.class).wildcard());

    for(ClassOutline classOutline : named) {
        JDefinedClass implClass = classOutline.implClass;
        // implement the interface
        implClass._implements(getOutput());
        /*
            @XmlTransient
            private QName jaxbElementName;
         */
        implClass.field(JMod.PRIVATE, QName.class, FIELD).annotate(XmlTransient.class);
        /*
           public void setJAXBElementName(QName name) {
               this.jaxbElementName = name;
           }
        */
        JMethod setter = implClass.method(JMod.PUBLIC, void.class, SETTER);
        setter.param(QName.class, "name");
        setter.body().assign(JExpr._this().ref(FIELD), JExpr.ref("name"));
        /*
           public QName getJAXBElementName() {
               return this.jaxbElementName;
           }
        */
        JMethod getter = implClass.method(JMod.PUBLIC, QName.class, GETTER);
        getter.body()._return(JExpr._this().ref(FIELD));

        /*
            public void afterUnmarshal(Unmarshaller u, Object parent) {
                if (parent instanceof JAXBElement) {
                    this.jaxbElementName = ((JAXBElement)parent).getName()
                }
            }
         */
        JMethod after = implClass.method(JMod.PUBLIC, void.class, "afterUnmarshal");
        after.param(Unmarshaller.class, "u");
        after.param(Object.class, "parent");
        after.body()._if(JExpr.ref("parent")._instanceof(jaxbElementClass))
                ._then().assign(JExpr._this().ref(FIELD),
                                JExpr.invoke(JExpr.cast(jaxbElementClass, JExpr.ref("parent")), "getName"));
    }
}