Search in sources :

Example 1 with JAnnotationUse

use of com.helger.jcodemodel.JAnnotationUse 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 2 with JAnnotationUse

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

the class FinalValueClassModel method buildFactory.

JMethod buildFactory(Map<String, JMethod> constructorMethods) throws JClassAlreadyExistsException {
    JDefinedClass factory = buildFactoryClass(constructorMethods);
    JFieldVar factoryField = environment.buildValueClassField(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, factory, "FACTORY");
    JAnnotationUse fieldAnnotationUse = factoryField.annotate(SuppressWarnings.class);
    JAnnotationArrayMember paramArray = fieldAnnotationUse.paramArray("value");
    paramArray.param("unchecked");
    paramArray.param("rawtypes");
    factoryField.init(JExpr._new(factory));
    JMethod factoryMethod = environment.buildValueClassMethod(Source.toJMod(environment.factoryMethodAccessLevel()) | JMod.STATIC, "factory");
    Source.annotateNonnull(factoryMethod);
    JAnnotationUse methodAnnotationUse = factoryMethod.annotate(SuppressWarnings.class);
    methodAnnotationUse.param("value", "unchecked");
    for (JTypeVar visitorTypeParameter : environment.getValueTypeParameters()) {
        JTypeVar typeParameter = factoryMethod.generify(visitorTypeParameter.name());
        typeParameter.boundLike(visitorTypeParameter);
    }
    AbstractJClass usedValueClassType = environment.wrappedValueClassType(factoryMethod.typeParams());
    factoryMethod.type(environment.visitor(usedValueClassType, usedValueClassType, types._RuntimeException).getVisitorType());
    factoryMethod.body()._return(factoryField);
    return factoryMethod;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JFieldVar(com.helger.jcodemodel.JFieldVar) JTypeVar(com.helger.jcodemodel.JTypeVar) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JAnnotationArrayMember(com.helger.jcodemodel.JAnnotationArrayMember) JMethod(com.helger.jcodemodel.JMethod)

Example 3 with JAnnotationUse

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

the class Stage0ValueClassModel method createStage1Model.

public GenerationResult<Stage1ValueClassModel> createStage1Model(JDefinedClass jVisitorModel, Visitor visitorAnnotation) {
    GenerationProcess generation = new GenerationProcess();
    if (error != null) {
        generation.reportError(error);
        return generation.createGenerationResult(null);
    } else {
        JAnnotationUse annotation = null;
        for (JAnnotationUse anyAnnotation : jVisitorModel.annotations()) {
            AbstractJClass annotationClass = anyAnnotation.getAnnotationClass();
            if (!annotationClass.isError()) {
                String fullName = annotationClass.fullName();
                if (fullName != null && fullName.equals(GenerateValueClassForVisitor.class.getName()))
                    annotation = anyAnnotation;
            }
        }
        if (annotation == null)
            throw new IllegalStateException("ValueClassModelFactory can't be run for interface without " + GenerateValueClassForVisitor.class + " annotation");
        VisitorDefinition visitorModel = generation.processGenerationResult(VisitorDefinition.createInstance(jVisitorModel, visitorAnnotation));
        ValueClassConfiguration configuration = generation.processGenerationResult(ValueClassConfiguration.createInstance(visitorModel, annotation, valueClass));
        Stage1ValueClassModel result = createStage1Model(configuration);
        return generation.createGenerationResult(result);
    }
}
Also used : ValueClassConfiguration(com.github.sviperll.adt4j.model.config.ValueClassConfiguration) VisitorDefinition(com.github.sviperll.adt4j.model.config.VisitorDefinition) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) GenerateValueClassForVisitor(com.github.sviperll.adt4j.GenerateValueClassForVisitor) AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 4 with JAnnotationUse

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

the class FieldReader method readUpdater.

GenerationResult<Void> readUpdater(MethodUsage interfaceMethod, VariableDeclaration param, AbstractJType paramType, boolean isVarArg) {
    GenerationProcess generation = new GenerationProcess();
    for (JAnnotationUse annotationUsage : param.annotations()) {
        AbstractJClass annotationClass = annotationUsage.getAnnotationClass();
        if (!annotationClass.isError()) {
            String annotationClassName = annotationClass.fullName();
            if (annotationClassName != null && annotationClassName.equals(Updater.class.getName())) {
                String updaterName = annotationUsage.getParam("name", String.class);
                if (updaterName == null || updaterName.equals(":auto"))
                    updaterName = "with" + Source.capitalize(param.name());
                MemberAccess accessLevel = annotationUsage.getParam("access", MemberAccess.class);
                boolean isNullable = Source.isNullable(param);
                FieldFlags flags = new FieldFlags(isNullable, isVarArg, accessLevel);
                FieldConfiguration configuration = new FieldConfiguration(updaterName, paramType, flags);
                generation.processGenerationResult(read(interfaceMethod, param, configuration));
            }
        }
    }
    return generation.<Void>createGenerationResult(null);
}
Also used : MemberAccess(com.github.sviperll.adt4j.MemberAccess) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) AbstractJClass(com.helger.jcodemodel.AbstractJClass) GenerationProcess(com.github.sviperll.adt4j.model.util.GenerationProcess)

Example 5 with JAnnotationUse

use of com.helger.jcodemodel.JAnnotationUse in project androidannotations by androidannotations.

the class APTCodeModelHelper method addSuppressWarnings.

public void addSuppressWarnings(IJAnnotatable generatedElement, String annotationValue) {
    Collection<JAnnotationUse> annotations = generatedElement.annotations();
    for (JAnnotationUse annotationUse : annotations) {
        if (SuppressWarnings.class.getCanonicalName().equals(annotationUse.getAnnotationClass().fullName())) {
            AbstractJAnnotationValue value = annotationUse.getParam("value");
            StringWriter code = new StringWriter();
            JFormatter formatter = new JFormatter(code);
            formatter.generable(value);
            if (!code.toString().contains(annotationValue)) {
                if (value instanceof JAnnotationArrayMember) {
                    ((JAnnotationArrayMember) value).param(annotationValue);
                } else {
                    String foundValue = code.toString().substring(1, code.toString().length() - 1);
                    JAnnotationArrayMember newParamArray = annotationUse.paramArray("value");
                    newParamArray.param(foundValue).param(annotationValue);
                }
            }
            return;
        }
    }
    generatedElement.annotate(SuppressWarnings.class).param("value", annotationValue);
}
Also used : StringWriter(java.io.StringWriter) AbstractJAnnotationValue(com.helger.jcodemodel.AbstractJAnnotationValue) JFormatter(com.helger.jcodemodel.JFormatter) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) JAnnotationArrayMember(com.helger.jcodemodel.JAnnotationArrayMember)

Aggregations

JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)11 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)7 MemberAccess (com.github.sviperll.adt4j.MemberAccess)3 JDefinedClass (com.helger.jcodemodel.JDefinedClass)3 GenerateValueClassForVisitor (com.github.sviperll.adt4j.GenerateValueClassForVisitor)2 ValueClassConfiguration (com.github.sviperll.adt4j.model.config.ValueClassConfiguration)2 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)2 JAnnotationArrayMember (com.helger.jcodemodel.JAnnotationArrayMember)2 JMethod (com.helger.jcodemodel.JMethod)2 JTypeVar (com.helger.jcodemodel.JTypeVar)2 GeneratePredicate (com.github.sviperll.adt4j.GeneratePredicate)1 GenerateValueClassForVisitorProcessor (com.github.sviperll.adt4j.GenerateValueClassForVisitorProcessor)1 WrapsGeneratedValueClass (com.github.sviperll.adt4j.WrapsGeneratedValueClass)1 AbstractJAnnotationValue (com.helger.jcodemodel.AbstractJAnnotationValue)1 JClassAlreadyExistsException (com.helger.jcodemodel.JClassAlreadyExistsException)1 JFieldVar (com.helger.jcodemodel.JFieldVar)1 JFormatter (com.helger.jcodemodel.JFormatter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1