Search in sources :

Example 1 with MethodUsage

use of com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage 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 2 with MethodUsage

use of com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage 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 3 with MethodUsage

use of com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage 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)

Example 4 with MethodUsage

use of com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage in project adt4j by sviperll.

the class ValueClassConfiguration method getUpdatersConfiguration.

public GenerationResult<Map<String, FieldConfiguration>> getUpdatersConfiguration(JDefinedClass valueClass, Types types) {
    GenerationProcess generation = new GenerationProcess();
    AbstractJClass usedValueClassType = Source.narrowType(valueClass, valueClass.typeParams());
    Map<String, FieldConfiguration> updatersMap = new TreeMap<>();
    FieldReader reader = new FieldReader(updatersMap);
    VisitorDefinition.VisitorUsage narrowed = visitorDefinition.narrowed(usedValueClassType, visitorDefinition.getResultTypeParameter(), types._RuntimeException);
    for (MethodUsage interfaceMethod : narrowed.methods()) {
        for (VariableDeclaration param : interfaceMethod.params()) {
            generation.processGenerationResult(reader.readUpdater(interfaceMethod, param, param.type().declarable(), false));
        }
        VariableDeclaration param = interfaceMethod.varParam();
        if (param != null) {
            generation.processGenerationResult(reader.readUpdater(interfaceMethod, param, param.type().declarable(), true));
        }
    }
    return generation.createGenerationResult(updatersMap);
}
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)

Aggregations

VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)4 MethodUsage (com.github.sviperll.adt4j.model.config.VisitorDefinition.MethodUsage)4 AbstractJClass (com.helger.jcodemodel.AbstractJClass)4 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)2 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)2 JDefinedClass (com.helger.jcodemodel.JDefinedClass)2 JInvocation (com.helger.jcodemodel.JInvocation)2 JMethod (com.helger.jcodemodel.JMethod)2 JTypeVar (com.helger.jcodemodel.JTypeVar)2 JVar (com.helger.jcodemodel.JVar)2 TreeMap (java.util.TreeMap)2 AbstractJType (com.helger.jcodemodel.AbstractJType)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 ArrayList (java.util.ArrayList)1