Search in sources :

Example 1 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass 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 AbstractJClass

use of com.helger.jcodemodel.AbstractJClass 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));
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) MemberAccess(com.github.sviperll.adt4j.MemberAccess) Caching(com.github.sviperll.adt4j.Caching) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 3 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass 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);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) AbstractJClass(com.helger.jcodemodel.AbstractJClass) WrapsGeneratedValueClass(com.github.sviperll.adt4j.WrapsGeneratedValueClass) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 4 with AbstractJClass

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

the class VisitorDefinition method createMethodMap.

private static GenerationResult<Map<String, JMethod>> createMethodMap(JDefinedClass jVisitorModel, SpecialTypeVariables specialTypeVariables) {
    GenerationProcess generation = new GenerationProcess();
    Map<String, JMethod> methods = new TreeMap<>();
    for (JMethod method : jVisitorModel.methods()) {
        AbstractJType methodType = method.type();
        if (methodType == null)
            generation.reportError(MessageFormat.format("Visitor method result type is missing: {0}", method.name()));
        else if (methodType.isError()) {
            generation.reportError(MessageFormat.format("Visitor method result type is erroneous: {0}", method.name()));
        } else if (!specialTypeVariables.isResult(method.type())) {
            generation.reportError(MessageFormat.format("Visitor method is only allowed to return type declared as a result type of visitor: {0}: expecting {1}, found: {2}", method.name(), specialTypeVariables.resultTypeParameter().name(), methodType.fullName()));
        }
        for (JTypeVar typeVariable : method.typeParamList()) {
            for (AbstractJClass bound : typeVariable.bounds()) {
                if (bound.containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                    generation.reportError(MessageFormat.format("Visitor method type-parameters shouldn''t depend on result type: {0}: {1} type-variable", method.name(), typeVariable.name()));
                }
            }
        }
        for (JVar parameter : method.listParams()) {
            if (parameter.type().containsTypeVar(specialTypeVariables.resultTypeParameter())) {
                generation.reportError(MessageFormat.format("Visitor method shouldn''t have result type as a parameter: {0}: result type-parameter: {1}", method.name(), specialTypeVariables.resultTypeParameter().name()));
            }
        }
        Collection<AbstractJClass> exceptions = method.getThrows();
        if (exceptions.size() > 1)
            generation.reportError(MessageFormat.format("Visitor method is allowed to throw no exceptions or throw single exception, declared as type-variable: {0}", method.name()));
        else if (exceptions.size() == 1) {
            AbstractJClass exception = exceptions.iterator().next();
            if (exception.isError())
                generation.reportError(MessageFormat.format("Visitor method exception type is erroneous: {0}", method.name()));
            else if (!specialTypeVariables.isException(exception))
                generation.reportError(MessageFormat.format("Visitor method throws exception, not declared as type-variable: {0}: {1}", method.name(), exception.fullName()));
        }
        JMethod exitingValue = methods.put(method.name(), method);
        if (exitingValue != null) {
            generation.reportError(MessageFormat.format("Method overloading is not supported for visitor interfaces: two methods with the same name: {0}", method.name()));
        }
        for (JVar param : method.params()) {
            generation.processGenerationResult(Source.getNullability(param));
        }
    }
    return generation.createGenerationResult(methods);
}
Also used : JTypeVar(com.helger.jcodemodel.JTypeVar) AbstractJType(com.helger.jcodemodel.AbstractJType) AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess) TreeMap(java.util.TreeMap) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 5 with AbstractJClass

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

the class Source method substitute.

public static AbstractJType substitute(AbstractJType type, JTypeVar typeVariable, AbstractJType variableValue) {
    if (type == typeVariable)
        return variableValue;
    else if (!(type instanceof AbstractJClass)) {
        return type;
    } else {
        if (type.isArray())
            return substitute(type.elementType(), typeVariable, variableValue).array();
        else if (type instanceof JTypeWildcard) {
            JTypeWildcard wildcard = (JTypeWildcard) type;
            AbstractJClass bound = (AbstractJClass) substitute(wildcard.bound(), typeVariable, variableValue);
            return bound.wildcard(wildcard.boundMode());
        } else {
            /*
                 * When we get type with type-parameters we should substitute
                 * type-parameters.
                 */
            AbstractJClass genericType = (AbstractJClass) type;
            if (genericType.getTypeParameters().isEmpty()) {
                return genericType;
            } else {
                AbstractJClass result = genericType.erasure();
                for (AbstractJClass typeArgument : genericType.getTypeParameters()) {
                    result = result.narrow(substitute(typeArgument, typeVariable, variableValue));
                }
                return result;
            }
        }
    }
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) JTypeWildcard(com.helger.jcodemodel.JTypeWildcard)

Aggregations

AbstractJClass (com.helger.jcodemodel.AbstractJClass)132 JVar (com.helger.jcodemodel.JVar)53 JBlock (com.helger.jcodemodel.JBlock)40 JMethod (com.helger.jcodemodel.JMethod)38 JInvocation (com.helger.jcodemodel.JInvocation)33 TypeMirror (javax.lang.model.type.TypeMirror)25 IJExpression (com.helger.jcodemodel.IJExpression)24 JDefinedClass (com.helger.jcodemodel.JDefinedClass)19 JFieldVar (com.helger.jcodemodel.JFieldVar)14 VariableElement (javax.lang.model.element.VariableElement)14 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)9 JConditional (com.helger.jcodemodel.JConditional)9 ExecutableElement (javax.lang.model.element.ExecutableElement)9 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)8 JTypeVar (com.helger.jcodemodel.JTypeVar)8 DeclaredType (javax.lang.model.type.DeclaredType)8 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)6 JFieldRef (com.helger.jcodemodel.JFieldRef)6 ArrayList (java.util.ArrayList)6 BundleHelper (org.androidannotations.helper.BundleHelper)6