Search in sources :

Example 6 with JClassAlreadyExistsException

use of com.helger.jcodemodel.JClassAlreadyExistsException in project adt4j by sviperll.

the class Stage0ValueClassModelFactory method createStage0Model.

public Stage0ValueClassModel createStage0Model(JDefinedClass bootModel, Visitor visitorAnnotation) {
    GenerationProcess generation = new GenerationProcess();
    JAnnotationUse annotation = null;
    for (JAnnotationUse anyAnnotation : bootModel.annotations()) {
        AbstractJClass annotationClass = anyAnnotation.getAnnotationClass();
        if (!annotationClass.isError()) {
            String fullName = annotationClass.fullName();
            if (fullName != null && fullName.equals(GenerateValueClassForVisitor.class.getName()))
                annotation = anyAnnotation;
        }
    }
    if (annotation == null)
        throw new IllegalStateException("ValueClassModelFactory can't be run for interface without " + GenerateValueClassForVisitor.class + " annotation");
    VisitorDefinition visitorModel = generation.processGenerationResult(VisitorDefinition.createInstance(bootModel, visitorAnnotation));
    ValueClassConfiguration configuration = generation.processGenerationResult(ValueClassConfiguration.createInstance(visitorModel, annotation));
    int mods = configuration.isValueClassPublic() ? JMod.PUBLIC : JMod.NONE;
    JDefinedClass valueClass;
    try {
        valueClass = factory.defineClass(bootModel._package().name(), mods, configuration.valueClassName());
    } catch (JClassAlreadyExistsException ex) {
        return new Stage0ValueClassModel("Class " + configuration.valueClassName() + " already exists");
    }
    JAnnotationUse generatedAnnotation = valueClass.annotate(Generated.class);
    generatedAnnotation.param("value", GenerateValueClassForVisitorProcessor.class.getName());
    Source.annotateParametersAreNonnullByDefault(valueClass);
    return new Stage0ValueClassModel(valueClass);
}
Also used : ValueClassConfiguration(com.github.sviperll.adt4j.model.config.ValueClassConfiguration) VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) GenerateValueClassForVisitor(com.github.sviperll.adt4j.GenerateValueClassForVisitor) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) GenerateValueClassForVisitorProcessor(com.github.sviperll.adt4j.GenerateValueClassForVisitorProcessor)

Example 7 with JClassAlreadyExistsException

use of com.helger.jcodemodel.JClassAlreadyExistsException in project adt4j by sviperll.

the class Stage1ValueClassModel method createResult.

public GenerationResult<JDefinedClass> createResult() {
    GenerationProcess generation = new GenerationProcess();
    generation.reportAllErrors(validateInterfaces());
    Map<String, FieldConfiguration> gettersConfigutation = generation.processGenerationResult(configuration.getGettersConfigutation(valueClass, types));
    Map<String, FieldConfiguration> updatersConfiguration = generation.processGenerationResult(configuration.getUpdatersConfiguration(valueClass, types));
    Map<String, PredicateConfigutation> predicates = generation.processGenerationResult(configuration.getPredicates());
    FinalValueClassModel result;
    if (generation.hasErrors()) {
        FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, null, configuration);
        result = FinalValueClassModel.createErrorModel(environment, types);
    } else {
        JDefinedClass acceptingInterface;
        try {
            acceptingInterface = createAcceptingInterface();
        } catch (JClassAlreadyExistsException ex) {
            throw new RuntimeException("Unexpected exception", ex);
        }
        if (configuration.isValueClassSerializable()) {
            acceptingInterface._extends(types._Serializable);
        }
        FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, acceptingInterface, configuration);
        result = FinalValueClassModel.createModel(environment, types);
    }
    result.buildSerialVersionUID();
    FinalValueClassModel.MethodBuilder methodBuilder = result.createMethodBuilder(configuration.serialization());
    Map<String, JMethod> constructorMethods = methodBuilder.buildConstructorMethods(configuration.serialization());
    methodBuilder.buildPrivateConstructor();
    if (configuration.isValueClassSerializable())
        methodBuilder.buildReadObjectMethod();
    methodBuilder.buildProtectedConstructor(configuration.serialization());
    methodBuilder.buildAcceptMethod();
    for (FieldConfiguration getter : gettersConfigutation.values()) {
        methodBuilder.generateGetter(getter);
    }
    for (FieldConfiguration updater : updatersConfiguration.values()) {
        methodBuilder.generateUpdater(updater);
    }
    for (Map.Entry<String, PredicateConfigutation> predicate : predicates.entrySet()) {
        methodBuilder.generatePredicate(predicate.getKey(), predicate.getValue());
    }
    if (configuration.isValueClassComparable()) {
        methodBuilder.buildCompareTo();
    }
    methodBuilder.buildEqualsMethod();
    methodBuilder.buildHashCodeMethod(configuration.hashCodeBase());
    methodBuilder.buildToStringMethod();
    try {
        result.buildFactory(constructorMethods);
    } catch (JClassAlreadyExistsException ex) {
        throw new RuntimeException("Unexpected exception :)", ex);
    }
    return generation.createGenerationResult(valueClass);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) PredicateConfigutation(com.github.sviperll.adt4j.model.config.PredicateConfigutation) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) JMethod(com.helger.jcodemodel.JMethod) Map(java.util.Map) FieldConfiguration(com.github.sviperll.adt4j.model.config.FieldConfiguration)

Aggregations

JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)7 JDefinedClass (com.helger.jcodemodel.JDefinedClass)4 AbstractJClass (com.helger.jcodemodel.AbstractJClass)3 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)2 JMethod (com.helger.jcodemodel.JMethod)2 IOException (java.io.IOException)2 Logger (org.robobinding.codegen.apt.Logger)2 RoundContext (org.robobinding.codegen.apt.RoundContext)2 WrappedTypeElement (org.robobinding.codegen.apt.element.WrappedTypeElement)2 Caching (com.github.sviperll.adt4j.Caching)1 GenerateValueClassForVisitor (com.github.sviperll.adt4j.GenerateValueClassForVisitor)1 GenerateValueClassForVisitorProcessor (com.github.sviperll.adt4j.GenerateValueClassForVisitorProcessor)1 FieldConfiguration (com.github.sviperll.adt4j.model.config.FieldConfiguration)1 PredicateConfigutation (com.github.sviperll.adt4j.model.config.PredicateConfigutation)1 ValueClassConfiguration (com.github.sviperll.adt4j.model.config.ValueClassConfiguration)1 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)1 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)1 JBlock (com.helger.jcodemodel.JBlock)1 JDirectClass (com.helger.jcodemodel.JDirectClass)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1