com.sun.codemodel.JClassAlreadyExistsException Java Examples

The following examples show how to use com.sun.codemodel.JClassAlreadyExistsException. 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: ClassGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates {@link #innerClassGenerator} with inner class
 * if {@link #hasMaxIndexValue()} returns {@code true}.
 *
 * @return true if splitting happened.
 */
private boolean createNestedClass() {
  if (hasMaxIndexValue()) {
    // all new fields will be declared in the class from innerClassGenerator
    if (innerClassGenerator == null) {
      try {
        JDefinedClass innerClazz = clazz._class(JMod.NONE, clazz.name() + "0");
        innerClassGenerator = new ClassGenerator<>(codeGenerator, mappings, sig, evaluationVisitor, innerClazz, model, optionManager);
      } catch (JClassAlreadyExistsException e) {
        throw new DrillRuntimeException(e);
      }
      oldBlocks = blocks;
      innerClassGenerator.index = index;
      innerClassGenerator.maxIndex += index;
      // blocks from the inner class should be used
      setupInnerClassBlocks();
      innerClassField = clazz.field(JMod.NONE, model.ref(innerClassGenerator.clazz.name()), INNER_CLASS_FIELD_NAME);
      return true;
    }
    return innerClassGenerator.createNestedClass();
  }
  return false;
}
 
Example #2
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
private JDefinedClass generateBuilderClass(JDefinedClass clazz) {
    JDefinedClass builderClass = null;
    String builderClassName = getBuilderClassName(clazz);
    try {
        builderClass = clazz._class(JMod.PUBLIC | JMod.STATIC, builderClassName);
        if (builderInheritance) {
            for (JClass superClass = clazz._extends(); superClass != null; superClass = superClass._extends()) {
                JClass superClassBuilderClass = getBuilderClass(superClass);
                if (superClassBuilderClass != null) {
                    builderClass._extends(superClassBuilderClass);
                    break;
                }
            }
        }
    } catch (JClassAlreadyExistsException e) {
        this.log(Level.WARNING, "builderClassExists", builderClassName);
    }
    return builderClass;
}
 
Example #3
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 #4
Source File: MetaPlugin.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
private void generateMetaClass(final PluginContext pluginContext, final ClassOutline classOutline, final ErrorHandler errorHandler) throws SAXException {
	try {
		final JDefinedClass metaClass = classOutline.implClass._class(JMod.PUBLIC | JMod.STATIC, this.metaClassName);
		final JMethod visitMethod = generateVisitMethod(classOutline);
		for (final FieldOutline fieldOutline : classOutline.getDeclaredFields()) {
			if (this.extended) {
				generateExtendedMetaField(pluginContext, metaClass, visitMethod, fieldOutline);
			} else {
				generateNameOnlyMetaField(pluginContext, metaClass, fieldOutline);
			}
		}
		visitMethod.body()._return(JExpr._this());
	} catch (final JClassAlreadyExistsException e) {
		errorHandler.error(new SAXParseException(getMessage("error.metaClassExists", classOutline.implClass.name(), this.metaClassName), classOutline.target.getLocator()));
	}
}
 
Example #5
Source File: ModifierPlugin.java    From jaxb2-rich-contract-plugin with MIT License 6 votes vote down vote up
@Override
public boolean run(final Outline outline, final Options opt, final ErrorHandler errorHandler) throws SAXException {
	final PluginContext pluginContext = PluginContext.get(outline, opt, errorHandler);
	for (final ClassOutline classOutline : outline.getClasses()) {
		try {
			final GroupInterfacePlugin groupInterfacePlugin = pluginContext.findPlugin(GroupInterfacePlugin.class);
			if (groupInterfacePlugin != null) {
				ModifierGenerator.generateClass(pluginContext, new DefinedClassOutline(pluginContext, classOutline), this.modifierClassName, this.modifierClassName, groupInterfacePlugin.getGroupInterfacesForClass(pluginContext, classOutline.implClass.fullName()), this.modifierMethodName);
			} else {
				ModifierGenerator.generateClass(pluginContext, new DefinedClassOutline(pluginContext, classOutline), this.modifierClassName, this.modifierMethodName);
			}
		} catch (final JClassAlreadyExistsException e) {
			errorHandler.error(new SAXParseException(e.getMessage(), classOutline.target.getLocator()));
		}
	}
	return true;
}
 
Example #6
Source File: KubernetesCoreTypeAnnotator.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
protected void processBuildable(JDefinedClass clazz) {
  try {
    clazz.annotate(Buildable.class)
      .param("editableEnabled", false)
      .param("validationEnabled", false)
      .param("generateBuilderPackage", true)
      .param("lazyCollectionInitEnabled", false)
      .param("builderPackage", "io.fabric8.kubernetes.api.builder")
      .annotationParam("inline", Inline.class)
      .param("type", new JCodeModel()._class("io.fabric8.kubernetes.api.model.Doneable"))
      .param("prefix", "Doneable")
      .param(ANNOTATION_VALUE, "done");

  } catch (JClassAlreadyExistsException e) {
    e.printStackTrace();
  }
}
 
Example #7
Source File: ControllerMethodSignatureRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMethodRuleOnSecondEndpoint_shouldCreate_validMethodSignatureWithGenericResponseType()
		throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._interface("MyInterface");
	JMethod jMethod = rule.apply(getEndpointMetadata(2), jClass);

	assertThat(jMethod, is(notNullValue()));
	assertThat(jMethod.name(), equalTo("getNamedResponseTypeById"));
	assertThat(jMethod.mods().getValue(), equalTo(JMod.PUBLIC));
	assertThat(jMethod.annotations(), hasSize(0)); // no implicit
													// annotations
	assertThat(jMethod.body().isEmpty(), is(true));
	assertThat(jMethod.type().name(), equalTo("ResponseEntity<NamedResponseType>"));
	assertThat(serializeModel(), containsString("import com.gen.test.model.NamedResponseType"));
}
 
Example #8
Source File: SpringRulesTest.java    From springmvc-raml-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void applySpringMethodBodyRule_shouldCreate_valid_body() throws JClassAlreadyExistsException {
	SpringRestClientMethodBodyRule rule = new SpringRestClientMethodBodyRule("restTemplate", "baseUrl");

	JPackage jPackage = jCodeModel.rootPackage();
	JDefinedClass jClass = jPackage._class(JMod.PUBLIC, "MyClass");
	JMethod jMethod = jClass.method(JMod.PUBLIC, Object.class, "getBase");
	jMethod.param(jCodeModel._ref(String.class), "id");
	rule.apply(getEndpointMetadata(2), CodeModelHelper.ext(jMethod, jCodeModel));

	String serializeModel = serializeModel();
	// ensure that we are adding the ACCEPT headers
	assertThat(serializeModel, containsString("httpHeaders.setAccept(acceptsList);"));
	// ensure that we are concatinating the base URL with the request URI to
	// form the full url
	assertThat(serializeModel, containsString("String url = baseUrl.concat(\"/base/{id}\""));
	// ensure that we are setting url paths vars in the uri
	assertThat(serializeModel, containsString("uriComponents = uriComponents.expand(uriParamMap)"));
	// ensure that the exchange invocation is as expected
	assertThat(serializeModel, containsString(
			"return this.restTemplate.exchange(uriComponents.encode().toUri(), HttpMethod.GET, httpEntity, NamedResponseType.class);"));
}
 
Example #9
Source File: CodeGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
CodeGenerator(MappingSet mappingSet, TemplateClassDefinition<T> definition, OptionSet optionManager) {
  Preconditions.checkNotNull(definition.getSignature(),
      "The signature for defintion %s was incorrectly initialized.", definition);
  this.definition = definition;
  this.className = definition.getExternalInterface().getSimpleName() + "Gen" + definition.getNextClassNumber();
  this.fqcn = PACKAGE_NAME + "." + className;
  try {
    this.model = new JCodeModel();
    JDefinedClass clazz = model._package(PACKAGE_NAME)._class(className);
    rootGenerator = new ClassGenerator<>(this, mappingSet,
      definition.getSignature(), new EvaluationVisitor(),
      clazz, model, optionManager);
  } catch (JClassAlreadyExistsException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #10
Source File: CodeGenerator.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
CodeGenerator(CodeCompiler compiler, MappingSet mappingSet, TemplateClassDefinition<T> definition, FunctionContext functionContext) {
  Preconditions.checkNotNull(definition.getSignature(),
      "The signature for defintion %s was incorrectly initialized.", definition);
  this.definition = definition;
  this.compiler = compiler;
  this.className = definition.getExternalInterface().getSimpleName() + "Gen" + definition.getNextClassNumber();
  this.fqcn = PACKAGE_NAME + "." + className;
  try {
    this.model = new JCodeModel();
    JDefinedClass clazz = model._package(PACKAGE_NAME)._class(className);
    clazz = clazz._extends(model.directClass(definition.getTemplateClassName()));
    clazz.constructor(JMod.PUBLIC).body().invoke(SignatureHolder.INIT_METHOD);
    rootGenerator = new ClassGenerator<>(this, mappingSet, definition.getSignature(), new EvaluationVisitor(functionContext), clazz, model);
  } catch (JClassAlreadyExistsException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #11
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 #12
Source File: DelegatingMethodBodyRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRuleOnParametrizedEndpoint_shouldCreate_methodCall_onDelegate() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class(JMod.PUBLIC, "TestClass");
	JMethod jMethod = jClass.method(JMod.PUBLIC, Object.class, "getBaseById");
	jMethod.param(String.class, "id");
	jMethod = rule.apply(getEndpointMetadata(2), CodeModelHelper.ext(jMethod, jClass.owner()));

	assertThat(jMethod, is(notNullValue()));
	assertThat(jMethod.body().isEmpty(), is(false));
	assertThat(jMethod.params(), hasSize(1));
	assertThat(serializeModel(), containsString("return this.controllerDelegate.getBaseById(id);"));
}
 
Example #13
Source File: MethodCommentRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_shouldCreate_validMethodComment() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class("TestController");
	JMethod jMethod = jClass.method(JMod.PUBLIC, ResponseEntity.class, "getBaseById");
	JDocComment jDocComment = rule.apply(getEndpointMetadata(2), jMethod);
	assertNotNull(jDocComment);
	assertThat(serializeModel(), containsString("* Get base entity by ID"));
}
 
Example #14
Source File: SourceGenerator.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private JDefinedClass generateOntologyThing() throws OntologyToJavaException {
    try {
        final JDefinedClass ontologyThing = codeModel._class(JMod.PUBLIC, getOntologyName(this.packageName, this.ontologyName), ClassType.INTERFACE);
        ontologyThing._extends(codeModel.ref(Thing.class));
        // Track field names to the IRI.
        final Map<String, IRI> fieldIriMap = new HashMap<>();
        final Map<String, IRI> rangeMap = new HashMap<>();
        // Identify all no domain properties from THIS ontology
        identifyAllDomainProperties(model).stream().filter(resource -> resource instanceof IRI).forEach(resource -> {
            // Set a static final field for the IRI.
            final String fieldName = getName(false, (IRI) resource, this.model);
            final IRI range = getRangeOfProperty((IRI) resource);
            ontologyThing.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, String.class, fieldName + "_IRI",
                    JExpr.lit(resource.stringValue())).javadoc()
                    .add("IRI of the predicate that this property will represent.<br><br>Domain: " + range);
            fieldIriMap.put(fieldName + "_IRI", (IRI) resource);
            rangeMap.put(fieldName, range);
        });
        interfaceFieldIriMap.put(ontologyThing, fieldIriMap);
        interfaceFieldRangeMap.put(ontologyThing, rangeMap);
        //TODO, null, or create some kind of unique IRI?
        interfaces.put(null, ontologyThing);
        return ontologyThing;
    } catch (JClassAlreadyExistsException e) {
        throw new OntologyToJavaException("Ontology Super Thing class already exists, or conflicts with an existing class...", e);
    }
}
 
Example #15
Source File: MethodParamsRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_shouldCreate_validMethodParams() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class("TestController");
	JMethod jMethod = jClass.method(JMod.PUBLIC, ResponseEntity.class, "getBaseById");
	jMethod = rule.apply(getEndpointMetadata(2), ext(jMethod, jCodeModel));

	assertThat(jMethod.params(), hasSize(1));
	assertThat(serializeModel(), containsString("getBaseById(String id)"));
}
 
Example #16
Source File: ImplementMeMethodBodyRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyMethodRule_shouldCreate_validMethodSignature_withBody() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class("TestController");
	JMethod jMethod = jClass.method(JMod.PUBLIC, ResponseEntity.class, "getBase");
	jMethod = rule.apply(getEndpointMetadata(), CodeModelHelper.ext(jMethod, jClass.owner()));

	assertThat(jMethod.body().isEmpty(), is(false));
	assertThat(serializeModel(), containsString("return null; //TODO Autogenerated Method Stub. Implement me please."));
}
 
Example #17
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 #18
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 #19
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 #20
Source File: DelegatingMethodBodyRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_shouldCreate_methodCall_onDelegate() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class(JMod.PUBLIC, "TestClass");
	JMethod jMethod = jClass.method(JMod.PUBLIC, Object.class, "getBase");
	jMethod = rule.apply(getEndpointMetadata(), CodeModelHelper.ext(jMethod, jClass.owner()));

	assertThat(jMethod, is(notNullValue()));
	assertThat(jMethod.body().isEmpty(), is(false));
	assertThat(jMethod.params(), hasSize(0));
	assertThat(serializeModel(), containsString("return this.controllerDelegate.getBase();"));
}
 
Example #21
Source File: DelegatingMethodBodyRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_onEmptyFieldName_shouldFallBackToDefaultFieldName() throws JClassAlreadyExistsException {

	rule = new DelegatingMethodBodyRule("");

	JDefinedClass jClass = jCodeModel.rootPackage()._class(JMod.PUBLIC, "TestClass");
	JMethod jMethod = jClass.method(JMod.PUBLIC, Object.class, "getBase");
	jMethod = rule.apply(getEndpointMetadata(), CodeModelHelper.ext(jMethod, jClass.owner()));

	assertThat(jMethod, is(notNullValue()));
	assertThat(jMethod.body().isEmpty(), is(false));
	assertThat(jMethod.params(), hasSize(0));
	assertThat(serializeModel(), containsString("return this.delegate.getBase();"));
}
 
Example #22
Source File: DelegatingMethodBodyRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_onNullFieldName_shouldFallBackToDefaultFieldName() throws JClassAlreadyExistsException {

	rule = new DelegatingMethodBodyRule(null);

	JDefinedClass jClass = jCodeModel.rootPackage()._class(JMod.PUBLIC, "TestClass");
	JMethod jMethod = jClass.method(JMod.PUBLIC, Object.class, "getBase");
	jMethod = rule.apply(getEndpointMetadata(), CodeModelHelper.ext(jMethod, jClass.owner()));

	assertThat(jMethod, is(notNullValue()));
	assertThat(jMethod.body().isEmpty(), is(false));
	assertThat(jMethod.params(), hasSize(0));
	assertThat(serializeModel(), containsString("return this.delegate.getBase();"));
}
 
Example #23
Source File: ClassCommentRuleTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void applyRule_shouldCreate_validClassComment() throws JClassAlreadyExistsException {

	JDefinedClass jClass = jCodeModel.rootPackage()._class("BaseController");
	JDocComment jDocComment = rule.apply(getControllerMetadata(), jClass);
	assertNotNull(jDocComment);
	assertThat(serializeModel(), containsString("* The BaseController class"));
}
 
Example #24
Source File: CodeModelUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static JDefinedClass getOrCreateClass(JClassContainer container,
		int flags, String name) {
	try {
		return container._class(flags, name);
	} catch (JClassAlreadyExistsException jcaeex) {
		return jcaeex.getExistingClass();
	}
}
 
Example #25
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 #26
Source File: ModifierGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private ModifierGenerator(final PluginContext pluginContext, final DefinedTypeOutline classOutline, final String modifierClassName, final String modifierInterfaceName, final Collection<TypeOutline> interfaces, final String modifierMethodName, final boolean implement) throws JClassAlreadyExistsException {
	this.classOutline = classOutline;
	final JDefinedClass definedClass = classOutline.getImplClass();
	this.implement = implement;
	this.modifierClass = definedClass._class(JMod.PUBLIC, modifierClassName, classOutline.getImplClass().getClassType());
	if(interfaces != null) {
		for (final TypeOutline interfaceOutline : interfaces) {
			this.modifierClass._implements( pluginContext.ref(interfaceOutline.getImplClass(), modifierInterfaceName, true));
		}
	}
	final JFieldRef cachedModifierField;
	if(!"java.lang.Object".equals(definedClass._extends().fullName())) {
		this.modifierClass._extends(pluginContext.ref(definedClass._extends(), modifierClassName, false));
		cachedModifierField = JExpr.refthis(ModifierGenerator.MODIFIER_CACHE_FIELD_NAME);
	} else {
		if(implement) {
			cachedModifierField = JExpr._this().ref(definedClass.field(JMod.PROTECTED | JMod.TRANSIENT, this.modifierClass, ModifierGenerator.MODIFIER_CACHE_FIELD_NAME));
		} else {
			cachedModifierField = null;
		}
	}

	final JDefinedClass typeDefinition = classOutline.isInterface() && ((DefinedInterfaceOutline)classOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)classOutline).getSupportInterface() : definedClass;
	final JMethod modifierMethod = typeDefinition.method(JMod.PUBLIC, this.modifierClass, modifierMethodName);
	if(this.implement) {
		final JConditional ifCacheNull = modifierMethod.body()._if(JExpr._null().eq(cachedModifierField));
		ifCacheNull._then().assign(cachedModifierField, JExpr._new(this.modifierClass));
		modifierMethod.body()._return(JExpr.cast(this.modifierClass, cachedModifierField));
	}

}
 
Example #27
Source File: GroupInterfaceGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private void generateBuilderInterface(final Map<String, BuilderOutline> builderOutlines, final DefinedInterfaceOutline interfaceOutline) throws SAXException {
	try {
		builderOutlines.put(interfaceOutline.getImplClass().fullName(), new BuilderOutline(interfaceOutline,
				/* interfaceOutline.getImplClass()._class(JMod.NONE, this.settings.getBuilderGeneratorSettings().getFluentClassName().getInterfaceName(), ClassType.INTERFACE) */
				interfaceOutline.getImplClass()._class(JMod.NONE, this.settings.getBuilderGeneratorSettings().getBuilderClassName().getInterfaceName(), ClassType.INTERFACE)
				/*interfaceOutline.getImplClass()._class(JMod.NONE, this.settings.getBuilderGeneratorSettings().getWrapperClassName().getInterfaceName(), ClassType.INTERFACE),
				interfaceOutline.getImplClass()._class(JMod.NONE, this.settings.getBuilderGeneratorSettings().getModifierClassName().getInterfaceName(), ClassType.INTERFACE) */
				));
	} catch (final JClassAlreadyExistsException e) {
		this.pluginContext.errorHandler.error(new SAXParseException(MessageFormat.format(GroupInterfaceGenerator.RESOURCE_BUNDLE.getString("error.interface-exists"), interfaceOutline.getImplClass().fullName(), PluginContext.BUILDER_INTERFACE_NAME), interfaceOutline.getSchemaComponent().getLocator()));
	}
}
 
Example #28
Source File: ImplementationBuilder.java    From aem-component-generator with Apache License 2.0 5 votes vote down vote up
private void buildImplementation(List<Property> properties, String modelClassName) {
    try {
        JClass childInterfaceClass = codeModel.ref(generationConfig.getProjectSettings().getModelInterfacePackage() + "." + modelClassName);
        JDefinedClass implClass = this.implPackage._class(modelClassName + "Impl")._implements(childInterfaceClass);
        addSlingAnnotations(implClass, childInterfaceClass, null);
        addFieldVars(implClass, properties, Constants.PROPERTY_TYPE_PRIVATE);
        addGetters(implClass);
        addExportedTypeMethod(implClass);
    } catch (JClassAlreadyExistsException ex) {
        LOG.error("Failed to generate child implementation classes.", ex);
    }
}
 
Example #29
Source File: ImplementationBuilder.java    From aem-component-generator with Apache License 2.0 5 votes vote down vote up
public JDefinedClass build(String resourceType) throws JClassAlreadyExistsException {
    JDefinedClass jc = this.implPackage._class(this.className)._implements(this.interfaceClass);
    addSlingAnnotations(jc, this.interfaceClass, resourceType);

    addFieldVars(jc, globalProperties, Constants.PROPERTY_TYPE_GLOBAL);
    addFieldVars(jc, sharedProperties, Constants.PROPERTY_TYPE_SHARED);
    addFieldVars(jc, privateProperties, Constants.PROPERTY_TYPE_PRIVATE);

    addGetters(jc);
    addExportedTypeMethod(jc);
    return jc;
}
 
Example #30
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();
	}
}