use of com.helger.jcodemodel.JMethod 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;
}
use of com.helger.jcodemodel.JMethod 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.helger.jcodemodel.JMethod 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;
}
use of com.helger.jcodemodel.JMethod in project adt4j by sviperll.
the class Stage1ValueClassModel method createResult.
public GenerationResult<JDefinedClass> createResult() {
GenerationProcess generation = new GenerationProcess();
generation.reportAllErrors(validateInterfaces());
Map<String, FieldConfiguration> gettersConfigutation = generation.processGenerationResult(configuration.getGettersConfigutation(valueClass, types));
Map<String, FieldConfiguration> updatersConfiguration = generation.processGenerationResult(configuration.getUpdatersConfiguration(valueClass, types));
Map<String, PredicateConfigutation> predicates = generation.processGenerationResult(configuration.getPredicates());
FinalValueClassModel result;
if (generation.hasErrors()) {
FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, null, configuration);
result = FinalValueClassModel.createErrorModel(environment, types);
} else {
JDefinedClass acceptingInterface;
try {
acceptingInterface = createAcceptingInterface();
} catch (JClassAlreadyExistsException ex) {
throw new RuntimeException("Unexpected exception", ex);
}
if (configuration.isValueClassSerializable()) {
acceptingInterface._extends(types._Serializable);
}
FinalValueClassModelEnvironment environment = new FinalValueClassModelEnvironment(valueClass, acceptingInterface, configuration);
result = FinalValueClassModel.createModel(environment, types);
}
result.buildSerialVersionUID();
FinalValueClassModel.MethodBuilder methodBuilder = result.createMethodBuilder(configuration.serialization());
Map<String, JMethod> constructorMethods = methodBuilder.buildConstructorMethods(configuration.serialization());
methodBuilder.buildPrivateConstructor();
if (configuration.isValueClassSerializable())
methodBuilder.buildReadObjectMethod();
methodBuilder.buildProtectedConstructor(configuration.serialization());
methodBuilder.buildAcceptMethod();
for (FieldConfiguration getter : gettersConfigutation.values()) {
methodBuilder.generateGetter(getter);
}
for (FieldConfiguration updater : updatersConfiguration.values()) {
methodBuilder.generateUpdater(updater);
}
for (Map.Entry<String, PredicateConfigutation> predicate : predicates.entrySet()) {
methodBuilder.generatePredicate(predicate.getKey(), predicate.getValue());
}
if (configuration.isValueClassComparable()) {
methodBuilder.buildCompareTo();
}
methodBuilder.buildEqualsMethod();
methodBuilder.buildHashCodeMethod(configuration.hashCodeBase());
methodBuilder.buildToStringMethod();
try {
result.buildFactory(constructorMethods);
} catch (JClassAlreadyExistsException ex) {
throw new RuntimeException("Unexpected exception :)", ex);
}
return generation.createGenerationResult(valueClass);
}
use of com.helger.jcodemodel.JMethod in project adt4j by sviperll.
the class ValueClassConfiguration method getPredicates.
public GenerationResult<Map<String, PredicateConfigutation>> getPredicates() {
GenerationProcess generation = new GenerationProcess();
Map<String, PredicateConfigutation> predicates = new TreeMap<>();
PredicatesReader predicatesReader = new PredicatesReader(predicates);
for (JMethod interfaceMethod : visitorDefinition.methodDefinitions()) {
for (JAnnotationUse annotationUsage : interfaceMethod.annotations()) {
generation.processGenerationResult(predicatesReader.read(interfaceMethod, annotationUsage));
}
}
return generation.createGenerationResult(predicates);
}
Aggregations