Java Code Examples for com.sun.codemodel.JPackage#_interface

The following examples show how to use com.sun.codemodel.JPackage#_interface . 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: InterfaceBuilder.java    From aem-component-generator with Apache License 2.0 6 votes vote down vote up
@SafeVarargs
private final JDefinedClass buildInterface(String interfaceName, String comment, List<Property>... propertiesLists) {
    try {
        JPackage jPackage = codeModel._package(generationConfig.getProjectSettings().getModelInterfacePackage());
        JDefinedClass interfaceClass = jPackage._interface(interfaceName);
        interfaceClass.javadoc().append(comment);
        interfaceClass.annotate(codeModel.ref("org.osgi.annotation.versioning.ConsumerType"));

        if (this.isAllowExporting) {
            interfaceClass._extends(codeModel.ref(ComponentExporter.class));
        }
        if (propertiesLists != null) {
            for (List<Property> properties : propertiesLists) {
                addGettersWithoutFields(interfaceClass, properties);
            }
        }
        return interfaceClass;
    } catch (JClassAlreadyExistsException e) {
        LOG.error("Failed to generate child interface.", e);
    }

    return null;
}
 
Example 2
Source File: SpringFeignClientInterfaceDeclarationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JDefinedClass apply(ApiResourceMetadata controllerMetadata, JPackage generatableType) {

	String controllerClassName = controllerMetadata.getResourceName().concat("Client");
	JDefinedClass definedClass;
	try {
		definedClass = generatableType._interface(controllerClassName);
	} catch (JClassAlreadyExistsException e1) {
		definedClass = generatableType._getClass(controllerClassName);
	}
	return definedClass;
}
 
Example 3
Source File: ControllerInterfaceDeclarationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JDefinedClass apply(ApiResourceMetadata controllerMetadata, JPackage generatableType) {
	String controllerClassName = controllerMetadata.getName() + CONTROLLER_SUFFIX;
	JDefinedClass definedClass;
	try {
		definedClass = generatableType._interface(controllerClassName);
	} catch (JClassAlreadyExistsException e1) {
		definedClass = generatableType._getClass(controllerClassName);
	}
	return definedClass;
}
 
Example 4
Source File: ClientInterfaceDeclarationRule.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public JDefinedClass apply(ApiResourceMetadata controllerMetadata, JPackage generatableType) {
	String clientClassName = controllerMetadata.getName() + CLIENT_SUFFIX;
	JDefinedClass definedClass;
	try {
		definedClass = generatableType._interface(clientClassName);
	} catch (JClassAlreadyExistsException e1) {
		definedClass = generatableType._getClass(clientClassName);
	}
	return definedClass;
}
 
Example 5
Source File: ImplementsControllerInferfaceRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_shouldCreate_classImplementsInterfaceExpression() throws JClassAlreadyExistsException {

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jInterface = jPackage._interface("MyInterface");
	JDefinedClass jClass = jPackage._class("MyClass");

	rule = new ImplementsControllerInterfaceRule(jInterface);
	rule.apply(getControllerMetadata(), jClass);

	assertThat(jClass, is(notNullValue()));
	assertThat(jClass.name(), equalTo("MyClass"));
	assertThat(serializeModel(), containsString("public class MyClass"));
	assertThat(serializeModel(), containsString("implements MyInterface"));
}
 
Example 6
Source File: PMMLPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private JDefinedClass ensureInterface(JPackage _package, String name){

	try {
		return _package._interface(name);
	} catch(JClassAlreadyExistsException jcaee){
		return jcaee.getExistingClass();
	}
}
 
Example 7
Source File: GroupInterfaceGenerator.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
private DefinedInterfaceOutline createInterfaceDeclaration(final XSDeclaration groupDecl) throws SAXException {
	final PackageOutline packageOutline = findPackageForNamespace(groupDecl.getTargetNamespace());
	if (packageOutline == null) {
		this.pluginContext.errorHandler.error(new SAXParseException(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("error.package-not-found"), groupDecl.getTargetNamespace()), groupDecl.getLocator()));
		return null;
	}

	final JPackage container = packageOutline._package();
	final ClassOutline dummyImplementation = this.pluginContext.classesBySchemaComponent.get(PluginContext.getQName(groupDecl));
	if (dummyImplementation == null) {
		this.pluginContext.errorHandler.error(new SAXParseException(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("error.no-implementation"), this.pluginContext.outline.getModel().getNameConverter().toClassName(groupDecl.getName()), groupDecl.getTargetNamespace(), groupDecl.getName()), groupDecl.getLocator()));
		return null;
	}

	final String interfaceName = dummyImplementation.implClass.name();
	container.remove(dummyImplementation.implClass);
	final JDefinedClass groupInterface;
	final JDefinedClass supportInterface;
	try {
		groupInterface = container._interface(JMod.PUBLIC, interfaceName);
		supportInterface = this.settings.isGeneratingSupportInterface() ? container._interface(JMod.PUBLIC, interfaceName + this.settings.getSupportInterfaceNameSuffix())._implements(groupInterface) : null;
	} catch (final JClassAlreadyExistsException e) {
		this.pluginContext.errorHandler.error(new SAXParseException(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("error.interface-exists"), interfaceName, ""), groupDecl.getLocator()));
		return null;
	}
	final DefinedInterfaceOutline interfaceDecl = new DefinedInterfaceOutline(groupDecl, groupInterface, dummyImplementation, supportInterface);

	// Generate Javadoc with schema fragment
	final StringWriter out = new StringWriter();
	out.write("<pre>\n");
	final SchemaWriter sw = new SchemaWriter(new JavadocEscapeWriter(out));
	groupDecl.visit(sw);
	out.write("</pre>");

	final JDocComment comment = groupInterface.javadoc();
	comment.append(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("comment.generated-from-xs-decl.header")).
			append("\n").
			append(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("comment.generated-from-xs-decl.qname"),
					groupDecl.getTargetNamespace(),
					groupDecl.getName())).
			append("\n").
			append(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("comment.generated-from-xs-decl.locator"),
					groupDecl.getLocator().getSystemId(),
					groupDecl.getLocator().getLineNumber(),
					groupDecl.getLocator().getColumnNumber()))
			.append("\n")
			.append(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("comment.generated-from-xs-decl.source"))
			.append("\n")
			.append(out.toString());


	for (final PropertyUse propertyUse : findChildDecls(groupDecl)) {
		final FieldOutline field = findField(dummyImplementation, propertyUse);
		if (field != null) {
			generateProperty(interfaceDecl, field);
		}
	}

	if(this.declareVisitMethod) {
		final JDefinedClass target = supportInterface != null ? supportInterface : groupInterface;
		target.method(JMod.NONE, target, this.pluginContext.findPlugin(MetaPlugin.class).getVisitMethodName()).param(JMod.FINAL, PropertyVisitor.class, "visitor_");
	}

	return interfaceDecl;
}