use of com.github.sviperll.adt4j.model.util.GenerationProcess in project adt4j by sviperll.
the class PredicatesReader method read.
private GenerationResult<Void> read(JMethod interfaceMethod, String predicateName, MemberAccess accessLevel) {
GenerationProcess generation = new GenerationProcess();
if (predicateName.equals(":auto")) {
predicateName = "is" + Source.capitalize(interfaceMethod.name());
}
PredicateConfigutation existingConfiguration = predicates.get(predicateName);
if (existingConfiguration == null) {
existingConfiguration = new PredicateConfigutation(interfaceMethod, accessLevel);
predicates.put(predicateName, existingConfiguration);
}
try {
existingConfiguration.put(interfaceMethod, accessLevel);
} catch (PredicateConfigurationException ex) {
generation.reportError(MessageFormat.format("Unable to generate {0} predicate: inconsistent access levels: {1}", predicateName, ex.getMessage()));
}
return generation.createGenerationResult(null);
}
use of com.github.sviperll.adt4j.model.util.GenerationProcess 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.util.GenerationProcess in project adt4j by sviperll.
the class ValueClassConfiguration method createInstance.
public static GenerationResult<ValueClassConfiguration> createInstance(VisitorDefinition visitorDefinition, JAnnotationUse annotation, JDefinedClass valueClass) {
GenerationProcess generation = new GenerationProcess();
String acceptMethodName = annotation.getParam("acceptMethodName", String.class);
MemberAccess acceptMethodAccess = annotation.getParam("acceptMethodAccess", MemberAccess.class);
boolean isPublic = annotation.getParam("isPublic", Boolean.class);
Caching hashCodeCaching = annotation.getParam("hashCodeCaching", Caching.class);
int hashCodeBase = annotation.getParam("hashCodeBase", Integer.class);
boolean isComparable = annotation.getParam("isComparable", Boolean.class);
float floatEpsilon = annotation.getParam("floatEpsilon", Float.class);
double doubleEpsilon = annotation.getParam("doubleEpsilon", Double.class);
FloatCustomization floatCustomization = new FloatCustomization(floatEpsilon, doubleEpsilon);
Serialization serialization = serialization(annotation);
ClassCustomization classCustomization = generation.processGenerationResult(classCustomization(annotation, visitorDefinition, valueClass));
AbstractJClass[] interfaces = annotation.getParam("implementsInterfaces", AbstractJClass[].class);
AcceptMethodCustomization acceptMethodCustomization = new AcceptMethodCustomization(acceptMethodName, acceptMethodAccess);
InterfacesCustomization interfaceCustomization = new InterfacesCustomization(isComparable, serialization, interfaces);
APICustomization apiCustomization = new APICustomization(isPublic, acceptMethodCustomization, interfaceCustomization);
ImplementationCustomization implementationCustomization = new ImplementationCustomization(hashCodeCaching, hashCodeBase, floatCustomization);
Customization customiztion = new Customization(classCustomization, apiCustomization, implementationCustomization);
return generation.createGenerationResult(new ValueClassConfiguration(visitorDefinition, customiztion));
}
use of com.github.sviperll.adt4j.model.util.GenerationProcess 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);
}
use of com.github.sviperll.adt4j.model.util.GenerationProcess 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));
}
Aggregations