Search in sources :

Example 6 with JTypeVar

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

the class FinalValueClassModel method buildFactoryClass.

private JDefinedClass buildFactoryClass(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
    JDefinedClass factoryClass = environment.buildValueClassInnerClass(JMod.PRIVATE | JMod.STATIC, environment.valueClassName() + "Factory", EClassType.CLASS);
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = factoryClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitor = environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException);
    factoryClass._implements(usedVisitor.getVisitorType());
    for (MethodUsage interfaceMethod : usedVisitor.methods()) {
        JMethod factoryMethod = factoryClass.method(interfaceMethod.mods().getValue() & ~JMod.ABSTRACT, usedValueClassType, interfaceMethod.name());
        for (JTypeVar visitorMethodTypeParameter : interfaceMethod.typeParams()) {
            JTypeVar typeParameter = factoryMethod.generify(visitorMethodTypeParameter.name());
            typeParameter.boundLike(visitorMethodTypeParameter);
        }
        MethodUsage usedInterfaceMethod = interfaceMethod.narrow(factoryMethod.typeParams());
        Source.annotateNonnull(factoryMethod);
        factoryMethod.annotate(Override.class);
        List<AbstractJClass> typeArguments = new ArrayList<>();
        typeArguments.addAll(factoryClass.typeParamList());
        typeArguments.addAll(factoryMethod.typeParamList());
        JMethod constructorMethod = constructorMethods.get(interfaceMethod.name());
        JInvocation staticInvoke = environment.invokeValueClassStaticMethod(constructorMethod, typeArguments.toArray(new AbstractJClass[typeArguments.size()]));
        for (VariableDeclaration param : usedInterfaceMethod.params()) {
            JVar argument = factoryMethod.param(param.mods().getValue(), param.type().declarable(), param.name());
            staticInvoke.arg(argument);
        }
        VariableDeclaration param = usedInterfaceMethod.varParam();
        if (param != null) {
            JVar argument = factoryMethod.varParam(param.mods().getValue(), param.type().elementType().declarable(), param.name());
            staticInvoke.arg(argument);
        }
        factoryMethod.body()._return(staticInvoke);
    }
    return factoryClass;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JTypeVar(com.helger.jcodemodel.JTypeVar) ArrayList(java.util.ArrayList) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableDeclaration(com.github.sviperll.adt4j.model.config.VariableDeclaration) MethodUsage(com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 7 with JTypeVar

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

the class Stage1ValueClassModel method createAcceptingInterface.

private JDefinedClass createAcceptingInterface() throws JClassAlreadyExistsException {
    JDefinedClass acceptingInterface = valueClass._class(JMod.PUBLIC, valueClass.name() + "Acceptor", EClassType.INTERFACE);
    // Hack to overcome bug in codeModel. We want private interface!!! Not public.
    acceptingInterface.mods().setPrivate();
    for (JTypeVar visitorTypeParameter : configuration.getValueTypeParameters()) {
        JTypeVar typeParameter = acceptingInterface.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    JMethod acceptMethod = acceptingInterface.method(JMod.PUBLIC, types._void, configuration.acceptMethodName());
    JTypeVar visitorResultType = configuration.visitorDefinition().getResultTypeParameter();
    AbstractJClass resultType;
    if (visitorResultType == null)
        resultType = types._Object;
    else {
        JTypeVar resultTypeVar = acceptMethod.generify(visitorResultType.name());
        resultTypeVar.boundLike(visitorResultType);
        resultType = resultTypeVar;
    }
    acceptMethod.type(resultType);
    JTypeVar visitorExceptionType = configuration.visitorDefinition().getExceptionTypeParameter();
    JTypeVar exceptionType = null;
    if (visitorExceptionType != null) {
        JTypeVar exceptionTypeParameter = acceptMethod.generify(visitorExceptionType.name());
        exceptionTypeParameter.boundLike(visitorExceptionType);
        exceptionType = exceptionTypeParameter;
        acceptMethod._throws(exceptionType);
    }
    AbstractJClass usedValueClassType = Source.narrowType(valueClass, valueClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitorType = configuration.visitorDefinition().narrowed(usedValueClassType, resultType, exceptionType);
    acceptMethod.param(usedVisitorType.getVisitorType(), "visitor");
    return acceptingInterface;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod)

Example 8 with JTypeVar

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

the class FinalValueClassModel method declareAcceptMethod.

private JMethod declareAcceptMethod(JDefinedClass caseClass, AbstractJClass usedValueClassType) {
    JMethod acceptMethod = caseClass.method(JMod.PUBLIC, types._void, environment.acceptMethodName());
    acceptMethod.annotate(Override.class);
    JTypeVar visitorResultTypeParameter = environment.visitorDefinition().getResultTypeParameter();
    AbstractJClass resultType;
    if (visitorResultTypeParameter == null)
        resultType = types._Object;
    else {
        JTypeVar resultTypeVar = acceptMethod.generify(visitorResultTypeParameter.name());
        resultTypeVar.boundLike(visitorResultTypeParameter);
        resultType = resultTypeVar;
    }
    acceptMethod.type(resultType);
    JTypeVar visitorExceptionTypeParameter = environment.visitorDefinition().getExceptionTypeParameter();
    JTypeVar exceptionType = null;
    if (visitorExceptionTypeParameter != null) {
        JTypeVar exceptionTypeParameter = acceptMethod.generify(visitorExceptionTypeParameter.name());
        exceptionTypeParameter.boundLike(visitorExceptionTypeParameter);
        exceptionType = exceptionTypeParameter;
        acceptMethod._throws(exceptionType);
    }
    VisitorDefinition.VisitorUsage usedVisitorType = environment.visitor(usedValueClassType, resultType, exceptionType);
    acceptMethod.param(usedVisitorType.getVisitorType(), "visitor");
    return acceptMethod;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod)

Example 9 with JTypeVar

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

the class FinalValueClassModel method buildCaseClass.

private JDefinedClass buildCaseClass(String interfaceMethodName, Serialization serialization) throws JClassAlreadyExistsException {
    JDefinedClass caseClass = environment.buildValueClassInnerClass(JMod.PRIVATE | JMod.STATIC, Source.capitalize(interfaceMethodName) + "Case" + environment.acceptingInterfaceName(), EClassType.CLASS);
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = caseClass.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedAcceptingInterfaceType = environment.acceptingInterfaceType(caseClass.typeParams());
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(caseClass.typeParams());
    VisitorDefinition.VisitorUsage usedVisitor = environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException);
    MethodUsage interfaceMethod = usedVisitor.findMethod(interfaceMethodName);
    if (interfaceMethod == null)
        throw new IllegalStateException("Method with given name not found: " + interfaceMethodName);
    JTypeVar[] methodArguments = new JTypeVar[interfaceMethod.typeParams().length];
    for (int i = 0; i < methodArguments.length; i++) {
        JTypeVar visitorMethodTypeParameter = interfaceMethod.typeParams()[i];
        JTypeVar typeParameter = caseClass.generify(visitorMethodTypeParameter.name());
        typeParameter.boundLike(visitorMethodTypeParameter);
        methodArguments[i] = typeParameter;
    }
    MethodUsage usedInterfaceMethod = interfaceMethod.narrow(methodArguments);
    caseClass._implements(usedAcceptingInterfaceType);
    if (serialization.isSerializable()) {
        caseClass._implements(types._Serializable);
        caseClass.field(JMod.PRIVATE | JMod.FINAL | JMod.STATIC, types._long, "serialVersionUID", JExpr.lit(serialization.serialVersionUIDForGeneratedCode()));
    }
    JMethod constructor = caseClass.constructor(JMod.NONE);
    for (VariableDeclaration param : usedInterfaceMethod.params()) {
        AbstractJType paramType = param.type().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType, param.name());
        JVar argument = constructor.param(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    VariableDeclaration param = usedInterfaceMethod.varParam();
    if (param != null) {
        AbstractJType paramType = param.type().elementType().declarable();
        JFieldVar field = caseClass.field(JMod.PRIVATE | JMod.FINAL, paramType.array(), param.name());
        JVar argument = constructor.varParam(paramType, param.name());
        constructor.body().assign(JExpr._this().ref(field), argument);
    }
    JMethod acceptMethod = declareAcceptMethod(caseClass, usedValueClassType);
    JInvocation invocation = JExpr.invoke(acceptMethod.params().get(0), usedInterfaceMethod.name());
    for (AbstractJClass argument : methodArguments) {
        invocation.narrow(argument);
    }
    for (VariableDeclaration param1 : usedInterfaceMethod.params()) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    VariableDeclaration param1 = usedInterfaceMethod.varParam();
    if (param1 != null) {
        invocation.arg(JExpr._this().ref(param1.name()));
    }
    acceptMethod.body()._return(invocation);
    return caseClass;
}
Also used : VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JTypeVar(com.helger.jcodemodel.JTypeVar) JFieldVar(com.helger.jcodemodel.JFieldVar) VariableDeclaration(com.github.sviperll.adt4j.model.config.VariableDeclaration) MethodUsage(com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

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