Package com.helger.jcodemodel

Examples of com.helger.jcodemodel.JVar


    void buildProtectedConstructor(JFieldVar acceptorField, Serialization serialization) throws JClassAlreadyExistsException {
        JMethod constructor = valueClass.constructor(JMod.PROTECTED);
        JAnnotationUse annotation = constructor.annotate(SuppressWarnings.class);
        annotation.paramArray("value", "null");
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        JVar param = constructor.param(JMod.FINAL, usedValueClassType, "implementation");
        param.annotate(Nonnull.class);
        JConditional nullCheck = constructor.body()._if(JExpr.ref("implementation").eq(JExpr._null()));
        JInvocation nullPointerExceptionConstruction = JExpr._new(types._NullPointerException);
        nullPointerExceptionConstruction.arg(JExpr.lit("Argument shouldn't be null: 'implementation' argument in class constructor invocation: " + valueClass.fullName()));
        nullCheck._then()._throw(nullPointerExceptionConstruction);
View Full Code Here


            JInvocation staticInvoke = valueClass.staticInvoke(constructorMethod);
            for (JTypeVar typeArgument: factoryClass.typeParams())
                staticInvoke.narrow(typeArgument);
            for (JVar param: interfaceMethod.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, usedValueClassType, types._RuntimeException);
                JVar argument = factoryMethod.param(param.mods().getValue(), argumentType, param.name());
                staticInvoke.arg(argument);
            }
            JVar param = interfaceMethod.listVarParam();
            if (param != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, usedValueClassType, types._RuntimeException);
                JVar argument = factoryMethod.varParam(param.mods().getValue(), argumentType, param.name());
                staticInvoke.arg(argument);
            }
            factoryMethod.body()._return(staticInvoke);
        }
        return factoryClass;
View Full Code Here

            }
            AbstractJClass usedValueClassType = valueClass.narrow(constructorMethod.typeParams());
            constructorMethod.type(usedValueClassType);
            for (JVar param: interfaceMethod.params()) {
                AbstractJType paramType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, usedValueClassType, types._RuntimeException);
                JVar constructorMethodParam = constructorMethod.param(param.mods().getValue(), paramType, param.name());
                if (param.type().isReference())
                    constructorMethodParam.annotate(isNullable(param) ? Nullable.class : Nonnull.class);
            }
            JVar param = interfaceMethod.listVarParam();
            if (param != null) {
                AbstractJType paramType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, usedValueClassType, types._RuntimeException);
                JVar constructorMethodParam = constructorMethod.varParam(param.mods().getValue(), paramType, param.name());
                if (param.type().isReference())
                    constructorMethodParam.annotate(isNullable(param) ? Nullable.class : Nonnull.class);
            }

            AbstractJClass usedCaseClassType = caseClasses.get(interfaceMethod.name()).narrow(constructorMethod.typeParams());
            if (!interfaceMethod.params().isEmpty() || interfaceMethod.hasVarArgs()) {
                boolean hasNullChecks = false;
                for (JVar param1: interfaceMethod.params()) {
                    if (param1.type().isReference() && !isNullable(param1)) {
                        JConditional nullCheck = constructorMethod.body()._if(JExpr.ref(param1.name()).eq(JExpr._null()));
                        JInvocation nullPointerExceptionConstruction = JExpr._new(types._NullPointerException);
                        nullPointerExceptionConstruction.arg(JExpr.lit("Argument shouldn't be null: '" + param1.name() + "' argument in static method invocation: '" + constructorMethod.name() + "' in class " + valueClass.fullName()));
                        nullCheck._then()._throw(nullPointerExceptionConstruction);
                        hasNullChecks = true;
                    }
                }
                JVar param1 = interfaceMethod.listVarParam();
                if (param1 != null) {
                    if (param1.type().isReference() && !isNullable(param1)) {
                        JConditional nullCheck = constructorMethod.body()._if(JExpr.ref(param1.name()).eq(JExpr._null()));
                        JInvocation nullPointerExceptionConstruction = JExpr._new(types._NullPointerException);
                        nullPointerExceptionConstruction.arg(JExpr.lit("Argument shouldn't be null: '" + param1.name() + "' argument in static method invocation: '" + constructorMethod.name() + "' in class " + valueClass.fullName()));
                        nullCheck._then()._throw(nullPointerExceptionConstruction);
                        hasNullChecks = true;
                    }
                }
                if (hasNullChecks) {
                    JAnnotationUse annotation = constructorMethod.annotate(SuppressWarnings.class);
                    annotation.paramArray("value", "null");
                }

                JInvocation caseClassConstructorInvocation = JExpr._new(usedCaseClassType);
                for (JVar param2: interfaceMethod.params()) {
                    caseClassConstructorInvocation.arg(JExpr.ref(param2.name()));
                }
                JVar param2 = interfaceMethod.listVarParam();
                if (param2 != null) {
                    caseClassConstructorInvocation.arg(JExpr.ref(param2.name()));
                }
                JInvocation constructorInvocation = JExpr._new(usedValueClassType);
                constructorInvocation.arg(caseClassConstructorInvocation);
                constructorMethod.body()._return(constructorInvocation);
            } else {
View Full Code Here

        JMethod constructor = caseClass.constructor(JMod.NONE);
        for (JVar param: interfaceMethod.params()) {
            AbstractJType paramType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, usedValueClassType, types._RuntimeException);
            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);
        }
        JVar param = interfaceMethod.listVarParam();
        if (param != null) {
            AbstractJType paramType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, usedValueClassType, types._RuntimeException);
            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 = caseClass.method(JMod.PUBLIC, types._void, "accept");
        acceptMethod.annotate(Override.class);

        JTypeVar visitorResultType = visitorInterface.getResultTypeParameter();
        JTypeVar resultType = Types.generifyWithBoundsFrom(acceptMethod, visitorResultType.name(), visitorResultType);
        acceptMethod.type(resultType);

        JTypeVar visitorExceptionType = visitorInterface.getExceptionTypeParameter();
        JTypeVar exceptionType = null;
        if (visitorExceptionType != null) {
            exceptionType = Types.generifyWithBoundsFrom(acceptMethod, visitorExceptionType.name(), visitorExceptionType);
            acceptMethod._throws(exceptionType);
        }

        AbstractJClass usedVisitorType = visitorInterface.narrowed(usedValueClassType, resultType, exceptionType);
        acceptMethod.param(usedVisitorType, "visitor");
        JInvocation invocation = JExpr.invoke(JExpr.ref("visitor"), interfaceMethod.name());
        for (JVar param1: interfaceMethod.params()) {
            invocation.arg(JExpr._this().ref(param1.name()));
        }
        JVar param1 = interfaceMethod.listVarParam();
        if (param1 != null) {
            invocation.arg(JExpr._this().ref(param1.name()));
        }
        acceptMethod.body()._return(invocation);

        return caseClass;
    }
View Full Code Here

        JMethod equalsMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, types._boolean, "equals");
        VariableNameSource nameSource = new VariableNameSource();
        equalsMethod.annotate(Override.class);
        JAnnotationUse annotationUse = equalsMethod.annotate(SuppressWarnings.class);
        annotationUse.param("value", "unchecked");
        JVar thatObject = equalsMethod.param(types._Object, nameSource.get("thatObject"));
        JConditional _if = equalsMethod.body()._if(JExpr._this().eq(thatObject));
        _if._then()._return(JExpr.TRUE);
        JConditional elseif = _if._elseif(thatObject._instanceof(valueClass).not());
        elseif._then()._return(JExpr.FALSE);
        JBlock _else = elseif._else();
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        JVar that = _else.decl(JMod.FINAL, usedValueClassType, nameSource.get("that"), JExpr.cast(usedValueClassType, thatObject));
        AbstractJClass visitorType = visitorInterface.narrowed(usedValueClassType, types._Boolean, types._RuntimeException);

        JDefinedClass anonymousClass1 = valueClass.owner().anonymousClass(visitorType);
        for (JMethod interfaceMethod1: visitorInterface.methods()) {
            JMethod visitorMethod1 = anonymousClass1.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, types._Boolean, interfaceMethod1.name());
            visitorMethod1.annotate(Nonnull.class);
            visitorMethod1.annotate(Override.class);

            VariableNameSource nameSource1 = nameSource.forBlock();
            List<JVar> arguments1 = new ArrayList<JVar>();
            JVar varArgument1 = null;
            for (JVar param1: interfaceMethod1.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param1.type(), usedValueClassType, types._Boolean, types._RuntimeException);
                JVar argument1 = visitorMethod1.param(param1.mods().getValue() | JMod.FINAL, argumentType, nameSource1.get(param1.name()));
                arguments1.add(argument1);
            }
            JVar param1 = interfaceMethod1.listVarParam();
            if (param1 != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param1.type().elementType(), usedValueClassType, types._Boolean, types._RuntimeException);
                JVar argument1 = visitorMethod1.varParam(param1.mods().getValue() | JMod.FINAL, argumentType, nameSource1.get(param1.name()));
                varArgument1 = argument1;
            }

            JDefinedClass anonymousClass2 = valueClass.owner().anonymousClass(visitorType);
            for (JMethod interfaceMethod2: visitorInterface.methods()) {
                JMethod visitorMethod2 = anonymousClass2.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, types._Boolean, interfaceMethod2.name());
                visitorMethod2.annotate(Nonnull.class);
                visitorMethod2.annotate(Override.class);

                VariableNameSource nameSource2 = nameSource1.forBlock();
                List<JVar> arguments2 = new ArrayList<JVar>();
                JVar varArgument2 = null;
                for (JVar param2: interfaceMethod2.params()) {
                    AbstractJType argumentType = visitorInterface.substituteSpecialType(param2.type(), usedValueClassType, types._Boolean, types._RuntimeException);
                    JVar argument2 = visitorMethod2.param(param2.mods().getValue(), argumentType, nameSource2.get(param2.name()));
                    arguments2.add(argument2);
                }
                JVar param2 = interfaceMethod2.listVarParam();
                if (param2 != null) {
                    AbstractJType argumentType = visitorInterface.substituteSpecialType(param2.type().elementType(), usedValueClassType, types._Boolean, types._RuntimeException);
                    JVar argument2 = visitorMethod2.varParam(param2.mods().getValue(), argumentType, nameSource2.get(param2.name()));
                    varArgument2 = argument2;
                }
                if (!interfaceMethod1.name().equals(interfaceMethod2.name()))
                    visitorMethod2.body()._return(JExpr.FALSE);
                else {
                    EqualsMethod body = new EqualsMethod(types, visitorMethod2.body(), nameSource2);
                    for (int i = 0; i < arguments1.size(); i++) {
                        JVar field1 = arguments1.get(i);
                        JVar field2 = arguments2.get(i);
                        JVar param = interfaceMethod1.params().get(i);
                        if (isNullable(param))
                            body.appendNullableValue(field1.type(), field1, field2);
                        else
                            body.appendNotNullValue(field1.type(), field1, field2);
                    }
                    if (varArgument1 != null) {
                        JVar param = interfaceMethod1.listVarParam();
                        if (isNullable(param))
                            body.appendNullableValue(varArgument1.type(), varArgument1, varArgument2);
                        else
                            body.appendNotNullValue(varArgument1.type(), varArgument1, varArgument2);
                    }
View Full Code Here

            JMethod visitorMethod1 = anonymousClass1.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, types._Integer, interfaceMethod1.name());
            visitorMethod1.annotate(Nonnull.class);
            visitorMethod1.annotate(Override.class);
            VariableNameSource nameSource = new VariableNameSource();
            List<JVar> arguments = new ArrayList<JVar>();
            JVar varArgument = null;
            for (JVar param: interfaceMethod1.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, types._Integer, types._RuntimeException);
                JVar argument = visitorMethod1.param(param.mods().getValue(), argumentType, nameSource.get(param.name()));
                arguments.add(argument);
            }
            JVar param = interfaceMethod1.listVarParam();
            if (param != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, types._Integer, types._RuntimeException);
                JVar argument = visitorMethod1.varParam(param.mods().getValue(), argumentType, nameSource.get(param.name()));
                varArgument = argument;
            }

            HashCodeMethod methodModel = new HashCodeMethod(types, hashCodeBase, visitorMethod1.body(), nameSource);
            HashCodeMethod.Body body = methodModel.createBody(tag);
            for (int i = 0; i < arguments.size(); i++) {
                param = interfaceMethod1.params().get(i);
                JVar argument = arguments.get(i);
                if (isNullable(param))
                    body.appendNullableValue(argument.type(), argument);
                else
                    body.appendNotNullValue(argument.type(), argument);
            }
            if (varArgument != null) {
                if (isNullable(param))
                    body.appendNullableValue(varArgument.type(), varArgument);
                else
View Full Code Here

            JMethod visitorMethod1 = anonymousClass1.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, types._String, interfaceMethod1.name());
            visitorMethod1.annotate(Nonnull.class);
            visitorMethod1.annotate(Override.class);
            VariableNameSource nameSource = new VariableNameSource();
            List<JVar> arguments = new ArrayList<JVar>();
            JVar varArgument = null;
            for (JVar param: interfaceMethod1.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, types._Integer, types._RuntimeException);
                JVar argument = visitorMethod1.param(param.mods().getValue() | JMod.FINAL, argumentType, nameSource.get(param.name()));
                arguments.add(argument);
            }
            JVar param = interfaceMethod1.listVarParam();
            if (param != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, types._Integer, types._RuntimeException);
                JVar argument = visitorMethod1.varParam(param.mods().getValue() | JMod.FINAL, argumentType, nameSource.get(param.name()));
                varArgument = argument;
            }

            JVar result = visitorMethod1.body().decl(types._StringBuilder, nameSource.get("result"), JExpr._new(types._StringBuilder));
            JInvocation invocation = visitorMethod1.body().invoke(result, "append");
            invocation.arg(valueClass.name() + "." + capitalize(interfaceMethod1.name()) + "{");
            ToStringMethodBody body = new ToStringMethodBody(visitorMethod1.body(), result);
            if (!arguments.isEmpty() || varArgument != null) {
                JVar argument = arguments.get(0);
                body.appendParam(argument.type(), interfaceMethod1.params().get(0).name(), argument);
                for (int i = 1; i < arguments.size(); i++) {
                    invocation = visitorMethod1.body().invoke(result, "append");
                    invocation.arg(", ");
                    argument = arguments.get(i);
                    body.appendParam(argument.type(), interfaceMethod1.params().get(i).name(), argument);
                }
                if (varArgument != null) {
                    invocation = visitorMethod1.body().invoke(result, "append");
                    invocation.arg(", ");
                    body.appendParam(varArgument.type(), interfaceMethod1.listVarParam().name(), varArgument);
View Full Code Here

                        }
                        configuration.put(paramType, interfaceMethod, param.name(), new FieldFlags(isNullable, false));
                    }
                }
            }
            JVar param = interfaceMethod.listVarParam();
            if (param != null) {
                for (JAnnotationUse annotationUsage: param.annotations()) {
                    if (annotationUsage.getAnnotationClass().fullName().equals(Getter.class.getName())) {
                        AbstractJType paramType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, visitorInterface.getResultTypeParameter(), types._RuntimeException);
                        @SuppressWarnings("null") String getterName = (String)annotationUsage.getConstantParam("value").nativeValue();
                        boolean isNullable = isNullable(param);
                        FieldConfiguration configuration = gettersMap.get(getterName);
                        if (configuration == null) {
                            configuration = new FieldConfiguration(getterName, paramType);
                            gettersMap.put(getterName, configuration);
                        }
                        configuration.put(paramType, interfaceMethod, param.name(), new FieldFlags(isNullable, true));
                    }
                }
            }
        }
        for (FieldConfiguration configuration: gettersMap.values()) {
View Full Code Here

                        }
                        configuration.put(paramType, interfaceMethod, param.name(), new FieldFlags(isNullable, false));
                    }
                }
            }
            JVar param = interfaceMethod.listVarParam();
            if (param != null) {
                for (JAnnotationUse annotationUsage: param.annotations()) {
                    if (annotationUsage.getAnnotationClass().fullName().equals(Updater.class.getName())) {
                        AbstractJType paramType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, visitorInterface.getResultTypeParameter(), types._RuntimeException);
                        @SuppressWarnings("null") String updaterName = (String)annotationUsage.getConstantParam("value").nativeValue();
                        boolean isNullable = isNullable(param);
                        FieldConfiguration configuration = updatersMap.get(updaterName);
                        if (configuration == null) {
                            configuration = new FieldConfiguration(updaterName, paramType);
                            updatersMap.put(updaterName, configuration);
                        }
                        configuration.put(paramType, interfaceMethod, param.name(), new FieldFlags(isNullable, true));
                    }
                }
            }
        }
        for (FieldConfiguration configuration: updatersMap.values()) {
View Full Code Here

        VariableNameSource nameSource = new VariableNameSource();
        String updaterName = configuration.name();
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        JMethod updaterMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, usedValueClassType, updaterName);
        updaterMethod.annotate(Nonnull.class);
        JVar newValue;
        if (configuration.flags().isVarArg())
            newValue = updaterMethod.varParam(JMod.FINAL, configuration.type().elementType(), nameSource.get("newValue"));
        else
            newValue = updaterMethod.param(JMod.FINAL, configuration.type(), nameSource.get("newValue"));
        if (configuration.flags().isNullable()) {
            newValue.annotate(Nullable.class);
        } else {
            newValue.annotate(Nonnull.class);
        }
        AbstractJClass visitorType = visitorInterface.narrowed(usedValueClassType, usedValueClassType, types._RuntimeException);

        JDefinedClass anonymousClass1 = valueClass.owner().anonymousClass(visitorType);
        UpdaterBody body = new UpdaterBody(configuration, anonymousClass1, newValue, nameSource);
View Full Code Here

TOP

Related Classes of com.helger.jcodemodel.JVar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.