Package com.helger.jcodemodel

Examples of com.helger.jcodemodel.JMethod


                AbstractJClass exception = exceptions.iterator().next();
                if (!typeParameters.isException(exception))
                    throw new SourceException("Visitor methods throws exception, not declared as type-variable: " + method.name() + ": " + exception);
            }

            JMethod exitingValue = methods.put(method.name(), method);
            if (exitingValue != null) {
                throw new SourceException("Method overloading is not supported for visitor interfaces: two methods with the same name: " + method.name());
            }
        }
        return methods;
View Full Code Here


        invocation1.arg(JExpr._new(anonymousClass1));
        hashCodeMethod.body()._return(invocation1);
    }

    void buildToStringMethod() throws SourceException {
        JMethod toStringMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, types._String, "toString");
        toStringMethod.annotate(Override.class);
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        AbstractJClass visitorType = visitorInterface.narrowed(usedValueClassType, types._String, types._RuntimeException);

        JDefinedClass anonymousClass1 = valueClass.owner().anonymousClass(visitorType);
        for (JMethod interfaceMethod1: visitorInterface.methods()) {
            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);
                }
            }
            invocation = visitorMethod1.body().invoke(result, "append");
            invocation.arg("}");
            visitorMethod1.body()._return(result.invoke("toString"));
        }
        JInvocation invocation1 = JExpr._this().invoke("accept");
        invocation1.arg(JExpr._new(anonymousClass1));
        toStringMethod.body()._return(invocation1);
    }
View Full Code Here

        }
    }

    private void generateGetter(FieldConfiguration configuration) {
        String getterName = configuration.name();
        JMethod getterMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, configuration.type(), getterName);
        if (configuration.flags().isNullable())
            getterMethod.annotate(Nullable.class);
        else
            getterMethod.annotate(Nonnull.class);
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        AbstractJClass visitorType = visitorInterface.narrowed(usedValueClassType, configuration.type().boxify(), types._RuntimeException);

        JDefinedClass anonymousClass1 = valueClass.owner().anonymousClass(visitorType);
        GetterBody body = new GetterBody(configuration, anonymousClass1);
        for (JMethod interfaceMethod1: visitorInterface.methods()) {
            body.generateCase(interfaceMethod1);
        }
        JInvocation invocation1 = JExpr._this().invoke("accept");
        invocation1.arg(JExpr._new(anonymousClass1));
        getterMethod.body()._return(invocation1);
    }
View Full Code Here

    private void generateUpdater(FieldConfiguration configuration) throws SourceException {
        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);
        for (JMethod interfaceMethod1: visitorInterface.methods()) {
            body.generateCase(interfaceMethod1);
        }
        JInvocation invocation1 = JExpr._this().invoke("accept");
        invocation1.arg(JExpr._new(anonymousClass1));
        updaterMethod.body()._return(invocation1);
    }
View Full Code Here

        }
    }

    private void generatePredicate(String name) {
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        JMethod predicateMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, types._boolean, name);
        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);
            for (JVar param: interfaceMethod1.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, types._Boolean, types._RuntimeException);
                visitorMethod1.param(param.mods().getValue(), argumentType, param.name());
            }
            JVar param = interfaceMethod1.listVarParam();
            if (param != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, types._Boolean, types._RuntimeException);
                visitorMethod1.varParam(param.mods().getValue(), argumentType, param.name());
            }
            boolean result = false;
            for (JAnnotationUse annotationUsage: interfaceMethod1.annotations()) {
                if (annotationUsage.getAnnotationClass().fullName().equals(GeneratePredicate.class.getName())) {
                    @SuppressWarnings("null") String predicateName = (String)annotationUsage.getConstantParam("value").nativeValue();
                    if (predicateName.equals(name)) {
                        result = true;
                        break;
                    }
                }
            }
            visitorMethod1.body()._return(JExpr.lit(result));
        }

        JInvocation invocation1 = JExpr._this().invoke("accept");
        invocation1.arg(JExpr._new(anonymousClass1));
        predicateMethod.body()._return(invocation1);
View Full Code Here

        invocation1.arg(JExpr._new(anonymousClass1));
        predicateMethod.body()._return(invocation1);
    }

    void buildCompareTo() throws SourceException {
        JMethod compareToMethod = valueClass.method(JMod.PUBLIC | JMod.FINAL, types._int, "compareTo");
        compareToMethod.annotate(Override.class);
        VariableNameSource nameSource = new VariableNameSource();
        AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
        JVar that = compareToMethod.param(JMod.FINAL, usedValueClassType, nameSource.get("that"));
        AbstractJClass visitorType = visitorInterface.narrowed(usedValueClassType, types._Integer, types._RuntimeException);

        JDefinedClass anonymousClass1 = valueClass.owner().anonymousClass(visitorType);
        JMethod[] methods = visitorInterface.methods().toArray(new JMethod[visitorInterface.methods().size()]);
        for (int interfaceMethodIndex1 = 0; interfaceMethodIndex1 < methods.length; interfaceMethodIndex1++) {
            JMethod interfaceMethod1 = methods[interfaceMethodIndex1];
            JMethod visitorMethod1 = anonymousClass1.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, types._Integer, interfaceMethod1.name());
            visitorMethod1.annotate(Override.class);
            visitorMethod1.annotate(Nonnull.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._Integer, 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._Integer, 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 (int interfaceMethodIndex2 = 0; interfaceMethodIndex2 < methods.length; interfaceMethodIndex2++) {
                JMethod interfaceMethod2 = methods[interfaceMethodIndex2];
                JMethod visitorMethod2 = anonymousClass2.method(interfaceMethod2.mods().getValue() & ~JMod.ABSTRACT, types._Integer, interfaceMethod2.name());
                visitorMethod2.annotate(Override.class);
                visitorMethod2.annotate(Nonnull.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._Integer, 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._Integer, types._RuntimeException);
                    JVar argument2 = visitorMethod2.varParam(param2.mods().getValue(), argumentType, nameSource2.get(param2.name()));
                    varArgument2 = argument2;
                }

                if (!interfaceMethod1.name().equals(interfaceMethod2.name())) {
                    int result = (interfaceMethodIndex1 < interfaceMethodIndex2 ? -1 : (interfaceMethodIndex1 == interfaceMethodIndex2 ? 0 : 1));
                    visitorMethod2.body()._return(JExpr.lit(result));
                } else {
                    if (!interfaceMethod1.params().isEmpty() || interfaceMethod1.hasVarArgs()) {
                        CompareToMethod compareToMethodModel = new CompareToMethod(types, visitorMethod2.body(), nameSource2);
                        CompareToMethod.Body body = compareToMethodModel.createBody();
                        for (int i = 0; i < interfaceMethod1.params().size(); i++) {
                            JVar argument1 = arguments1.get(i);
                            JVar argument2 = arguments2.get(i);
                            JVar param = interfaceMethod1.params().get(i);
                            if (isNullable(param))
                                body.appendNullableValue(argument1.type(), argument1, argument2);
                            else
                                body.appendNotNullValue(argument1.type(), argument1, argument2);
                        }
                        if (varArgument1 != null) {
                            JVar param = interfaceMethod1.listVarParam();
                            if (isNullable(param))
                                body.appendNullableValue(varArgument1.type(), varArgument1, varArgument2);
                            else
                                body.appendNotNullValue(varArgument1.type(), varArgument1, varArgument2);
                        }
                    }
                    visitorMethod2.body()._return(JExpr.lit(0));
                }
            }

            JInvocation invocation2 = that.invoke("accept");
            invocation2.arg(JExpr._new(anonymousClass2));
View Full Code Here

            this.configuration = configuration;
        }

        private void generateCase(JMethod interfaceMethod1) {
            AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
            JMethod visitorMethod1 = visitor.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, configuration.type().boxify(), interfaceMethod1.name());
            visitorMethod1.annotate(Override.class);
            if (configuration.flags().isNullable())
                visitorMethod1.annotate(Nullable.class);
            else
                visitorMethod1.annotate(Nonnull.class);
            boolean isGettable = false;
            for (JVar param: interfaceMethod1.params()) {
                AbstractJType paramType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, configuration.type().boxify(), types._RuntimeException);
                JVar argument = visitorMethod1.param(param.mods().getValue(), paramType, param.name());
                if (configuration.isFieldValue(interfaceMethod1, param.name())) {
                    visitorMethod1.body()._return(argument);
                    isGettable = true;
                }
            }
            JVar param = interfaceMethod1.listVarParam();
            if (param != null) {
                AbstractJType paramType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, configuration.type().boxify(), types._RuntimeException);
                JVar argument = visitorMethod1.varParam(param.mods().getValue(), paramType, param.name());
                if (configuration.isFieldValue(interfaceMethod1, param.name())) {
                    visitorMethod1.body()._return(argument);
                    isGettable = true;
                }
            }
            if (!isGettable) {
                JInvocation exceptionInvocation = JExpr._new(types._IllegalStateException);
                exceptionInvocation.arg(configuration.name() + " is not accessible in this case: " + interfaceMethod1.name());
                visitorMethod1.body()._throw(exceptionInvocation);
            }
        }
View Full Code Here

            this.nameSource = nameSource;
        }

        private void generateCase(JMethod interfaceMethod1) {
            AbstractJClass usedValueClassType = valueClass.narrow(valueClass.typeParams());
            JMethod visitorMethod1 = visitor.method(interfaceMethod1.mods().getValue() & ~JMod.ABSTRACT, usedValueClassType, interfaceMethod1.name());
            visitorMethod1.annotate(Override.class);
            visitorMethod1.annotate(Nonnull.class);
            JInvocation invocation = valueClass.staticInvoke(interfaceMethod1.name());
            for (JTypeVar typeArgument: valueClass.typeParams())
                invocation.narrow(typeArgument);
            for (JVar param: interfaceMethod1.params()) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type(), usedValueClassType, configuration.type().boxify(), types._RuntimeException);
                JVar argument = visitorMethod1.param(param.mods().getValue(), argumentType, nameSource.get(param.name()));
                if (configuration.isFieldValue(interfaceMethod1, param.name())) {
                    invocation.arg(newValue);
                } else {
                    invocation.arg(argument);
                }
            }
            JVar param = interfaceMethod1.listVarParam();
            if (param != null) {
                AbstractJType argumentType = visitorInterface.substituteSpecialType(param.type().elementType(), usedValueClassType, configuration.type().boxify(), types._RuntimeException);
                JVar argument = visitorMethod1.varParam(param.mods().getValue(), argumentType, nameSource.get(param.name()));
                if (configuration.isFieldValue(interfaceMethod1, param.name())) {
                    invocation.arg(newValue);
                } else {
                    invocation.arg(argument);
                }
            }
            visitorMethod1.body()._return(invocation);
        }
View Full Code Here

        for (JTypeVar visitorTypeParameter: visitorInterface.getValueTypeParameters()) {
            Types.generifyWithBoundsFrom(acceptingInterface, visitorTypeParameter.name(), visitorTypeParameter);
        }

        JMethod acceptMethod = acceptingInterface.method(JMod.PUBLIC, types._void, "accept");

        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 usedValueClassType = valueClass.narrow(valueClass.typeParams());
        AbstractJClass usedVisitorType = visitorInterface.narrowed(usedValueClassType, resultType, exceptionType);
        acceptMethod.param(usedVisitorType, "visitor");

        return acceptingInterface;
    }
View Full Code Here

            newClass._implements((AbstractJClass)toJType(iface, environment));
        }
        for (Element enclosedElement: element.getEnclosedElements()) {
            if (enclosedElement.getKind().equals(ElementKind.METHOD)) {
                ExecutableElement executable = (ExecutableElement)enclosedElement;
                JMethod method = newClass.method(toJMod(executable.getModifiers()), codeModel.VOID, executable.getSimpleName().toString());
                TypeEnvironment methodEnvironment = environment.enclosed();
                Annotator methodAnnotator = new Annotator(method, environment);
                methodAnnotator.annotate(executable.getAnnotationMirrors());
                for (TypeParameterElement parameter: executable.getTypeParameters()) {
                    JTypeVar typeVariable = method.generify(parameter.getSimpleName().toString());
                    methodEnvironment.put(typeVariable.name(), typeVariable);
                    for (TypeMirror type: parameter.getBounds()) {
                        typeVariable.bound((AbstractJClass)toJType(type, methodEnvironment));
                    }
                }
                method.type(toJType(executable.getReturnType(), methodEnvironment));
                for (TypeMirror type: executable.getThrownTypes()) {
                    AbstractJClass throwable = (AbstractJClass)toJType(type, methodEnvironment);
                    method._throws(throwable);
                }

                List<? extends VariableElement> parameters = executable.getParameters();
                int n = 0;
                for (VariableElement variable: parameters) {
                    String parameterName = variable.getSimpleName().toString();
                    TypeMirror parameterTypeMirror = variable.asType();
                    AbstractJType parameterType = toJType(parameterTypeMirror, methodEnvironment);

                    JVar param;
                    if (executable.isVarArgs() && n == parameters.size() - 1) {
                        param = method.varParam(toJMod(variable.getModifiers()), parameterType.elementType(), parameterName);
                    } else {
                        param = method.param(toJMod(variable.getModifiers()), parameterType, parameterName);
                    }
                    Annotator parametorAnnotator = new Annotator(param, methodEnvironment);
                    parametorAnnotator.annotate(variable.getAnnotationMirrors());
                    n++;
                }
View Full Code Here

TOP

Related Classes of com.helger.jcodemodel.JMethod

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.