Search in sources :

Example 1 with JTypeVar

use of com.helger.jcodemodel.JTypeVar 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)

Example 2 with JTypeVar

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

the class VisitorDefinition method createMethodMap.

private static GenerationResult<Map<String, JMethod>> createMethodMap(JDefinedClass jVisitorModel, SpecialTypeVariables specialTypeVariables) {
    GenerationProcess generation = new GenerationProcess();
    Map<String, JMethod> methods = new TreeMap<>();
    for (JMethod method : jVisitorModel.methods()) {
        AbstractJType methodType = method.type();
        if (methodType == null)
            generation.reportError(MessageFormat.format("Visitor method result type is missing: {0}", method.name()));
        else if (methodType.isError()) {
            generation.reportError(MessageFormat.format("Visitor method result type is erroneous: {0}", method.name()));
        } else if (!specialTypeVariables.isResult(method.type())) {
            generation.reportError(MessageFormat.format("Visitor method is only allowed to return type declared as a result type of visitor: {0}: expecting {1}, found: {2}", method.name(), specialTypeVariables.resultTypeParameter().name(), methodType.fullName()));
        }
        for (JTypeVar typeVariable : method.typeParamList()) {
            for (AbstractJClass bound : typeVariable.bounds()) {
                if (bound.containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                    generation.reportError(MessageFormat.format("Visitor method type-parameters shouldn''t depend on result type: {0}: {1} type-variable", method.name(), typeVariable.name()));
                }
            }
        }
        for (JVar parameter : method.listParams()) {
            if (parameter.type().containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                generation.reportError(MessageFormat.format("Visitor method shouldn''t have result type as a parameter: {0}: result type-parameter: {1}", method.name(), specialTypeVariables.resultTypeParameter().name()));
            }
        }
        Collection<AbstractJClass> exceptions = method.getThrows();
        if (exceptions.size() > 1)
            generation.reportError(MessageFormat.format("Visitor method is allowed to throw no exceptions or throw single exception, declared as type-variable: {0}", method.name()));
        else if (exceptions.size() == 1) {
            AbstractJClass exception = exceptions.iterator().next();
            if (exception.isError())
                generation.reportError(MessageFormat.format("Visitor method exception type is erroneous: {0}", method.name()));
            else if (!specialTypeVariables.isException(exception))
                generation.reportError(MessageFormat.format("Visitor method throws exception, not declared as type-variable: {0}: {1}", method.name(), exception.fullName()));
        }
        JMethod exitingValue = methods.put(method.name(), method);
        if (exitingValue != null) {
            generation.reportError(MessageFormat.format("Method overloading is not supported for visitor interfaces: two methods with the same name: {0}", method.name()));
        }
        for (JVar param : method.params()) {
            generation.processGenerationResult(Source.getNullability(param));
        }
    }
    return generation.createGenerationResult(methods);
}
Also used : JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) JMethod(com.helger.jcodemodel.JMethod) TreeMap(java.util.TreeMap) JVar(com.helger.jcodemodel.JVar)

Example 3 with JTypeVar

use of com.helger.jcodemodel.JTypeVar 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 4 with JTypeVar

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

the class Stage1ValueClassModel method fullySpecifyClassHeader.

void fullySpecifyClassHeader() {
    for (JTypeVar visitorTypeParameter : configuration.getValueTypeParameters()) {
        JTypeVar typeParameter = valueClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    for (AbstractJClass iface : configuration.implementsInterfaces()) {
        valueClass._implements(iface.typeParams().length == 0 ? iface : iface.narrow(valueClass.typeParams()));
    }
    AbstractJClass extendsClass = configuration.valueClassExtends();
    valueClass._extends(extendsClass.typeParams().length == 0 ? extendsClass : extendsClass.narrow(valueClass.typeParams()));
    if (configuration.isValueClassSerializable()) {
        valueClass._implements(types._Serializable);
    }
    if (configuration.isValueClassComparable()) {
        valueClass._implements(types._Comparable.narrow(Source.narrowType(configuration.wrapValueClass(valueClass), valueClass.typeParams())));
    }
}
Also used : JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 5 with JTypeVar

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

the class FinalValueClassModel method buildFactory.

JMethod buildFactory(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
    JDefinedClass factory = buildFactoryClass(constructorMethods);
    JFieldVar factoryField = environment.buildValueClassField(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, factory, "FACTORY");
    JAnnotationUse fieldAnnotationUse = factoryField.annotate(SuppressWarnings.class);
    JAnnotationArrayMember paramArray = fieldAnnotationUse.paramArray("value");
    paramArray.param("unchecked");
    paramArray.param("rawtypes");
    factoryField.init(JExpr._new(factory));
    JMethod factoryMethod = environment.buildValueClassMethod(Source.toJMod(environment.factoryMethodAccessLevel()) | JMod.STATIC, "factory");
    Source.annotateNonnull(factoryMethod);
    JAnnotationUse methodAnnotationUse = factoryMethod.annotate(SuppressWarnings.class);
    methodAnnotationUse.param("value", "unchecked");
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = factoryMethod.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryMethod.typeParams());
    factoryMethod.type(environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException).getVisitorType());
    factoryMethod.body()._return(factoryField);
    return factoryMethod;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JAnnotationArrayMember(com.helger.jcodemodel.JAnnotationArrayMember) JMethod(com.helger.jcodemodel.JMethod)

Aggregations

JTypeVar (com.helger.jcodemodel.JTypeVar)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 JMethod (com.helger.jcodemodel.JMethod)7 JDefinedClass (com.helger.jcodemodel.JDefinedClass)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)3 JVar (com.helger.jcodemodel.JVar)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)2 AbstractJType (com.helger.jcodemodel.AbstractJType)2 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)2 JFieldVar (com.helger.jcodemodel.JFieldVar)2 JInvocation (com.helger.jcodemodel.JInvocation)2 ArrayList (java.util.ArrayList)2 WrapsGeneratedValueClass (com.github.sviperll.adt4j.WrapsGeneratedValueClass)1 JAnnotationArrayMember (com.helger.jcodemodel.JAnnotationArrayMember)1 TreeMap (java.util.TreeMap)1