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);
}
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;
}
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;
}
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);
}
Aggregations