Search in sources :

Example 1 with GenerationProcess

use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.

the class PredicatesReader method read.

private GenerationResult<Void> read(JMethod interfaceMethod, String predicateName, MemberAccess accessLevel) {
    GenerationProcess generation = new GenerationProcess();
    if (predicateName.equals(":auto")) {
        predicateName = "is" + Source.capitalize(interfaceMethod.name());
    }
    PredicateConfigutation existingConfiguration = predicates.get(predicateName);
    if (existingConfiguration == null) {
        existingConfiguration = new PredicateConfigutation(interfaceMethod, accessLevel);
        predicates.put(predicateName, existingConfiguration);
    }
    try {
        existingConfiguration.put(interfaceMethod, accessLevel);
    } catch (PredicateConfigurationException ex) {
        generation.reportError(MessageFormat.format("Unable to generate {0} predicate: inconsistent access levels: {1}", predicateName, ex.getMessage()));
    }
    return generation.createGenerationResult(null);
}
Also used : GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 2 with GenerationProcess

use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.

the class ValueClassConfiguration method getGettersConfigutation.

public GenerationResult<Map<String, FieldConfiguration>> getGettersConfigutation(JDefinedClass valueClass, Types types) {
    GenerationProcess generation = new GenerationProcess();
    AbstractJClass usedValueClassType = Source.narrowType(valueClass, valueClass.typeParams());
    Map<String, FieldConfiguration> gettersMap = new TreeMap<>();
    FieldReader reader = new FieldReader(gettersMap);
    VisitorDefinition.VisitorUsage narrowed = visitorDefinition.narrowed(usedValueClassType, visitorDefinition.getResultTypeParameter(), types._RuntimeException);
    for (MethodUsage interfaceMethod : narrowed.methods()) {
        for (VariableDeclaration param : interfaceMethod.params()) {
            generation.processGenerationResult(reader.readGetter(interfaceMethod, param, param.type().declarable(), false));
        }
        VariableDeclaration param = interfaceMethod.varParam();
        if (param != null) {
            generation.processGenerationResult(reader.readGetter(interfaceMethod, param, param.type().declarable(), true));
        }
    }
    return generation.createGenerationResult(gettersMap);
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) VariableDeclaration(com.github.sviperll.adt4j.model.config.VariableDeclaration) MethodUsage(com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage) TreeMap(java.util.TreeMap)

Example 3 with GenerationProcess

use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.

the class ValueClassConfiguration method createInstance.

public static GenerationResult<ValueClassConfiguration> createInstance(VisitorDefinition visitorDefinition, JAnnotationUse annotation, JDefinedClass valueClass) {
    GenerationProcess generation = new GenerationProcess();
    String acceptMethodName = annotation.getParam("acceptMethodName", String.class);
    MemberAccess acceptMethodAccess = annotation.getParam("acceptMethodAccess", MemberAccess.class);
    boolean isPublic = annotation.getParam("isPublic", Boolean.class);
    Caching hashCodeCaching = annotation.getParam("hashCodeCaching", Caching.class);
    int hashCodeBase = annotation.getParam("hashCodeBase", Integer.class);
    boolean isComparable = annotation.getParam("isComparable", Boolean.class);
    float floatEpsilon = annotation.getParam("floatEpsilon", Float.class);
    double doubleEpsilon = annotation.getParam("doubleEpsilon", Double.class);
    FloatCustomization floatCustomization = new FloatCustomization(floatEpsilon, doubleEpsilon);
    Serialization serialization = serialization(annotation);
    ClassCustomization classCustomization = generation.processGenerationResult(classCustomization(annotation, visitorDefinition, valueClass));
    AbstractJClass[] interfaces = annotation.getParam("implementsInterfaces", AbstractJClass[].class);
    AcceptMethodCustomization acceptMethodCustomization = new AcceptMethodCustomization(acceptMethodName, acceptMethodAccess);
    InterfacesCustomization interfaceCustomization = new InterfacesCustomization(isComparable, serialization, interfaces);
    APICustomization apiCustomization = new APICustomization(isPublic, acceptMethodCustomization, interfaceCustomization);
    ImplementationCustomization implementationCustomization = new ImplementationCustomization(hashCodeCaching, hashCodeBase, floatCustomization);
    Customization customiztion = new Customization(classCustomization, apiCustomization, implementationCustomization);
    return generation.createGenerationResult(new ValueClassConfiguration(visitorDefinition, customiztion));
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) MemberAccess(com.github.sviperll.adt4j.MemberAccess) Caching(com.github.sviperll.adt4j.Caching) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 4 with GenerationProcess

use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.

the class ValueClassConfiguration method classCustomization.

private static GenerationResult<ClassCustomization> classCustomization(JAnnotationUse annotation, VisitorDefinition visitorDefinition, JDefinedClass valueClass) throws ClassCastException, NullPointerException {
    GenerationProcess generation = new GenerationProcess();
    AbstractJClass extendsClass = annotation.getParam("extendsClass", AbstractJClass.class);
    AbstractJClass wrapperClass = annotation.getParam("wrapperClass", AbstractJClass.class);
    if (wrapperClass == null)
        throw new NullPointerException("wrapperClass annotation argument should never be null");
    String wrapperClassFullName = wrapperClass.fullName();
    if (wrapperClassFullName == null)
        throw new NullPointerException("wrapperClass.fullName() is null");
    if (wrapperClassFullName.equals("java.lang.Object"))
        wrapperClass = null;
    String className = annotation.getParam("className", String.class);
    if (className == null)
        throw new NullPointerException("className annotation argument should never be null");
    if (wrapperClass == null) {
        if (className.equals(":auto")) {
            className = autoClassName(visitorDefinition.visitorName());
        }
    } else {
        AbstractJClass wrapperClassErasure = wrapperClass.erasure();
        if (wrapperClassErasure instanceof JDefinedClass) {
            JDefinedClass definition = (JDefinedClass) wrapperClassErasure;
            JAnnotationUse wrapsGeneratedAnnotation = null;
            for (JAnnotationUse wrapperAnnotaion : definition.annotations()) {
                String annotationClassFullName = wrapperAnnotaion.getAnnotationClass().erasure().fullName();
                if (annotationClassFullName != null && annotationClassFullName.equals(WrapsGeneratedValueClass.class.getName())) {
                    wrapsGeneratedAnnotation = wrapperAnnotaion;
                }
            }
            if (wrapsGeneratedAnnotation == null)
                generation.reportError(MessageFormat.format("Wrapper class should be annotated with @{0} annotation.", com.github.sviperll.adt4j.WrapsGeneratedValueClass.class.getName()));
            else {
                AbstractJClass visitor = wrapsGeneratedAnnotation.getParam("visitor", AbstractJClass.class);
                if (visitor == null || visitor.fullName() == null || !visitor.fullName().equals(visitorDefinition.qualifiedName()))
                    generation.reportError("@" + WrapsGeneratedValueClass.class.getName() + " annotation should have " + visitorDefinition.qualifiedName() + " as visitor argument");
            }
        }
        if (!className.equals(":auto")) {
            generation.reportError("You shouldn't define className when wrapperClass is used. Generated class name is derived from wrapper class' extends clause.");
        } else {
            AbstractJClass extendedClass = wrapperClass._extends();
            boolean extendedClassError = false;
            if (extendedClass != null) {
                if (extendedClass.isError()) {
                    className = extendedClass.name();
                } else {
                    if (valueClass == null) {
                        extendedClassError = true;
                    } else {
                        String valueClassFullName = valueClass.fullName();
                        if (valueClassFullName == null || !valueClassFullName.equals(extendedClass.erasure().fullName()))
                            extendedClassError = true;
                        else
                            className = valueClass.name();
                    }
                }
            }
            if (extendedClass == null || extendedClassError) {
                generation.reportError("Wrapper class should explicitly extend non-existing class, that class is to be generated");
                className = autoClassName(visitorDefinition.visitorName());
            } else {
                boolean typeParamsError = false;
                List<? extends AbstractJClass> typeArguments = extendedClass.getTypeParameters();
                List<JTypeVar> generatedTypeParameters = visitorDefinition.nonspecialTypeParameters();
                JTypeVar[] wrapperTypeParameters = wrapperClass.typeParams();
                if (wrapperTypeParameters.length != typeArguments.size() || wrapperTypeParameters.length != generatedTypeParameters.size())
                    typeParamsError = true;
                else {
                    for (int i = 0; i < wrapperTypeParameters.length; i++) {
                        JTypeVar wrapperTypeParameter = wrapperTypeParameters[i];
                        if (typeArguments.get(i) != wrapperTypeParameter) {
                            typeParamsError = true;
                            break;
                        }
                    }
                }
                if (typeParamsError) {
                    generation.reportError("Wrapper class should declare same type-parameters as generated class and should extend generated class with all type-arguments applied");
                }
            }
        }
    }
    ClassCustomization classCustomization = new ClassCustomization(className, wrapperClass, extendsClass);
    return generation.createGenerationResult(classCustomization);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJClass(com.helger.jcodemodel.AbstractJClass) WrapsGeneratedValueClass(com.github.sviperll.adt4j.WrapsGeneratedValueClass) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 5 with GenerationProcess

use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.

the class VisitorDefinition method createInstance.

public static GenerationResult<VisitorDefinition> createInstance(JDefinedClass jVisitorModel, Visitor annotation) {
    GenerationProcess generation = new GenerationProcess();
    JTypeVar resultType = null;
    JTypeVar exceptionType = null;
    JTypeVar selfType = null;
    List<JTypeVar> valueClassTypeParameters = new ArrayList<>();
    for (JTypeVar typeVariable : jVisitorModel.typeParams()) {
        if (typeVariable.name().equals(annotation.resultVariableName()))
            resultType = typeVariable;
        else if (typeVariable.name().equals(annotation.selfReferenceVariableName()))
            selfType = typeVariable;
        else if (typeVariable.name().equals(annotation.exceptionVariableName()))
            exceptionType = typeVariable;
        else
            valueClassTypeParameters.add(typeVariable);
    }
    if (resultType == null) {
        generation.reportError(MessageFormat.format("Result type-variable is not found for visitor, expecting: {0}", annotation.resultVariableName()));
        resultType = jVisitorModel.typeParams().length == 0 ? null : jVisitorModel.typeParams()[0];
    }
    if (exceptionType == null && !annotation.exceptionVariableName().equals(":none")) {
        generation.reportError(MessageFormat.format("Exception type-variable is not found for visitor, expecting: {0}", annotation.exceptionVariableName()));
    }
    if (selfType == null && !annotation.selfReferenceVariableName().equals(":none")) {
        generation.reportError(MessageFormat.format("Self reference type-variable is not found for visitor, expecting: {0}", annotation.selfReferenceVariableName()));
    }
    SpecialTypeVariables specialTypeVariables = new SpecialTypeVariables(resultType, exceptionType, selfType);
    Map<String, JMethod> methods = generation.processGenerationResult(createMethodMap(jVisitorModel, specialTypeVariables));
    return generation.createGenerationResult(new VisitorDefinition(jVisitorModel, methods, specialTypeVariables, valueClassTypeParameters));
}
Also used : JTypeVar(com.helger.jcodemodel.JTypeVar) ArrayList(java.util.ArrayList) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) JMethod(com.helger.jcodemodel.JMethod)

Aggregations

GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)9 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)7 MemberAccess (com.github.sviperll.adt4j.MemberAccess)4 JMethod (com.helger.jcodemodel.JMethod)4 TreeMap (java.util.TreeMap)4 JDefinedClass (com.helger.jcodemodel.JDefinedClass)3 JTypeVar (com.helger.jcodemodel.JTypeVar)3 GenerateValueClassForVisitor (com.github.sviperll.adt4j.GenerateValueClassForVisitor)2 ValueClassConfiguration (com.github.sviperll.adt4j.model.config.ValueClassConfiguration)2 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)2 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)2 JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)2 Caching (com.github.sviperll.adt4j.Caching)1 GeneratePredicate (com.github.sviperll.adt4j.GeneratePredicate)1 GenerateValueClassForVisitorProcessor (com.github.sviperll.adt4j.GenerateValueClassForVisitorProcessor)1 WrapsGeneratedValueClass (com.github.sviperll.adt4j.WrapsGeneratedValueClass)1 FieldConfiguration (com.github.sviperll.adt4j.model.config.FieldConfiguration)1 PredicateConfigutation (com.github.sviperll.adt4j.model.config.PredicateConfigutation)1