Example usage for com.google.gwt.core.ext.typeinfo JConstructor getParameters

List of usage examples for com.google.gwt.core.ext.typeinfo JConstructor getParameters

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.typeinfo JConstructor getParameters.

Prototype

JParameter[] getParameters();

Source Link

Usage

From source file:com.github.nmorel.gwtjackson.rebind.bean.BeanProcessor.java

License:Apache License

/**
 * Look for the method to create a new instance of the bean. If none are found or the bean is abstract or an interface, we considered it
 * as non instantiable.//ww w.  j  a v a  2s .  c o  m
 *
 * @param typeOracle the oracle
 * @param logger logger
 * @param beanType type to look for constructor
 * @param builder current bean builder
 */
private static void determineInstanceCreator(RebindConfiguration configuration, JacksonTypeOracle typeOracle,
        TreeLogger logger, JClassType beanType, BeanInfoBuilder builder) {
    if (isObjectOrSerializable(beanType)) {
        return;
    }

    Optional<JClassType> mixinClass = configuration.getMixInAnnotations(beanType);

    List<JClassType> accessors = new ArrayList<JClassType>();
    if (mixinClass.isPresent()) {
        accessors.add(mixinClass.get());
    }
    accessors.add(beanType);

    // Look for a builder class
    Optional<Annotation> jsonDeserialize = CreatorUtils
            .getAnnotation("com.fasterxml.jackson.databind.annotation.JsonDeserialize", accessors);
    if (jsonDeserialize.isPresent()) {
        Optional<JClassType> builderClass = typeOracle.getClassFromJsonDeserializeAnnotation(logger,
                jsonDeserialize.get(), "builder");
        if (builderClass.isPresent()) {
            builder.setBuilder(builderClass.get());
            return;
        }
    }

    // we search for @JsonCreator annotation
    JConstructor creatorDefaultConstructor = null;
    JConstructor creatorConstructor = null;

    // we keep the list containing the mixin creator and the real creator
    List<? extends JAbstractMethod> creators = Collections.emptyList();

    if (null == beanType.isInterface() && !beanType.isAbstract()) {
        for (JConstructor constructor : beanType.getConstructors()) {
            if (constructor.getParameters().length == 0) {
                creatorDefaultConstructor = constructor;
                continue;
            }

            // A constructor is considered as a creator if
            // - he is annotated with JsonCreator and
            //   * all its parameters are annotated with JsonProperty
            //   * or it has only one parameter
            // - or all its parameters are annotated with JsonProperty

            List<JConstructor> constructors = new ArrayList<JConstructor>();
            if (mixinClass.isPresent() && null == mixinClass.get().isInterface()) {
                JConstructor mixinConstructor = mixinClass.get()
                        .findConstructor(constructor.getParameterTypes());
                if (null != mixinConstructor) {
                    constructors.add(mixinConstructor);
                }
            }
            constructors.add(constructor);

            Optional<JsonIgnore> jsonIgnore = getAnnotation(JsonIgnore.class, constructors);
            if (jsonIgnore.isPresent() && jsonIgnore.get().value()) {
                continue;
            }

            boolean isAllParametersAnnotatedWithJsonProperty = isAllParametersAnnotatedWith(constructors.get(0),
                    JsonProperty.class);
            if ((isAnnotationPresent(JsonCreator.class, constructors)
                    && ((isAllParametersAnnotatedWithJsonProperty)
                            || (constructor.getParameters().length == 1)))
                    || isAllParametersAnnotatedWithJsonProperty) {
                creatorConstructor = constructor;
                creators = constructors;
                break;
            }
        }
    }

    JMethod creatorFactory = null;
    if (null == creatorConstructor) {
        // searching for factory method
        for (JMethod method : beanType.getMethods()) {
            if (method.isStatic()) {

                List<JMethod> methods = new ArrayList<JMethod>();
                if (mixinClass.isPresent() && null == mixinClass.get().isInterface()) {
                    JMethod mixinMethod = mixinClass.get().findMethod(method.getName(),
                            method.getParameterTypes());
                    if (null != mixinMethod && mixinMethod.isStatic()) {
                        methods.add(mixinMethod);
                    }
                }
                methods.add(method);

                Optional<JsonIgnore> jsonIgnore = getAnnotation(JsonIgnore.class, methods);
                if (jsonIgnore.isPresent() && jsonIgnore.get().value()) {
                    continue;
                }

                if (isAnnotationPresent(JsonCreator.class, methods) && (method.getParameters().length == 1
                        || isAllParametersAnnotatedWith(methods.get(0), JsonProperty.class))) {
                    creatorFactory = method;
                    creators = methods;
                    break;
                }
            }
        }
    }

    final Optional<JAbstractMethod> creatorMethod;
    boolean defaultConstructor = false;

    if (null != creatorConstructor) {
        creatorMethod = Optional.<JAbstractMethod>of(creatorConstructor);
    } else if (null != creatorFactory) {
        creatorMethod = Optional.<JAbstractMethod>of(creatorFactory);
    } else if (null != creatorDefaultConstructor) {
        defaultConstructor = true;
        creatorMethod = Optional.<JAbstractMethod>of(creatorDefaultConstructor);
    } else {
        creatorMethod = Optional.absent();
    }

    builder.setCreatorMethod(creatorMethod);
    builder.setCreatorDefaultConstructor(defaultConstructor);

    if (creatorMethod.isPresent() && !defaultConstructor) {
        if (creatorMethod.get().getParameters().length == 1
                && !isAllParametersAnnotatedWith(creators.get(0), JsonProperty.class)) {
            // delegation constructor
            builder.setCreatorDelegation(true);
            builder.setCreatorParameters(ImmutableMap.of(BeanJsonDeserializerCreator.DELEGATION_PARAM_NAME,
                    creatorMethod.get().getParameters()[0]));
        } else {
            // we want the property name define in the mixin and the parameter defined in the real creator method
            ImmutableMap.Builder<String, JParameter> creatorParameters = ImmutableMap.builder();
            for (int i = 0; i < creatorMethod.get().getParameters().length; i++) {
                creatorParameters.put(
                        creators.get(0).getParameters()[i].getAnnotation(JsonProperty.class).value(),
                        creators.get(creators.size() - 1).getParameters()[i]);
            }
            builder.setCreatorParameters(creatorParameters.build());
        }
    }
}

From source file:com.github.nmorel.gwtjackson.rebind.RebindConfiguration.java

License:Apache License

/**
 * Search a static method or constructor to instantiate the mapper and return a {@link String} calling it.
 */// w ww.  j  a  v a  2  s.  com
private MapperInstance getInstance(JType mappedType, JClassType classType, boolean isSerializers) {
    int nbParam = 0;
    if (null != mappedType.isGenericType() && (!isSerializers || !typeOracle.isEnumSupertype(mappedType))) {
        nbParam = mappedType.isGenericType().getTypeParameters().length;
    }

    // we first look at static method
    for (JMethod method : classType.getMethods()) {
        // method must be public static, return the instance type and take no parameters
        if (method.isStatic() && null != method.getReturnType().isClassOrInterface()
                && classType.isAssignableTo(method.getReturnType().isClassOrInterface())
                && method.getParameters().length == nbParam && method.isPublic()) {
            MapperType[] parameters = getParameters(mappedType, method, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, method, parameters);
        }
    }

    // then we search the default constructor
    for (JConstructor constructor : classType.getConstructors()) {
        if (constructor.isPublic() && constructor.getParameters().length == nbParam) {
            MapperType[] parameters = getParameters(mappedType, constructor, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, constructor, parameters);
        }
    }

    logger.log(Type.WARN, "Cannot instantiate the custom serializer/deserializer "
            + classType.getQualifiedSourceName() + ". It will be ignored");
    return null;
}

From source file:com.github.nmorel.gwtjackson.rebind.RebindConfiguration.java

License:Apache License

/**
 * Search a static method or constructor to instantiate the key mapper and return a {@link String} calling it.
 *///  w w w. j  a  va2s.  co  m
private MapperInstance getKeyInstance(JType mappedType, JClassType classType, boolean isSerializers) {
    int nbParam = 0;
    if (!isSerializers && typeOracle.isEnumSupertype(mappedType)) {
        nbParam = 1;
    }

    // we first look at static method
    for (JMethod method : classType.getMethods()) {
        // method must be public static, return the instance type and take no parameters
        if (method.isStatic() && null != method.getReturnType().isClassOrInterface()
                && classType.isAssignableTo(method.getReturnType().isClassOrInterface())
                && method.getParameters().length == nbParam && method.isPublic()) {
            MapperType[] parameters = getParameters(mappedType, method, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, method, parameters);
        }
    }

    // then we search the default constructor
    for (JConstructor constructor : classType.getConstructors()) {
        if (constructor.isPublic() && constructor.getParameters().length == nbParam) {
            MapperType[] parameters = getParameters(mappedType, constructor, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, constructor, parameters);
        }
    }

    logger.log(Type.WARN, "Cannot instantiate the custom key serializer/deserializer "
            + classType.getQualifiedSourceName() + ". It will be ignored");
    return null;
}

From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java

License:Apache License

private void printMatchingParameters(SourceWriter out, JConstructor constructorToCall) {
    JParameter[] params = constructorToCall.getParameters();
    for (int i = 0; i < params.length; i++) {
        if (i > 0) {
            out.print(", ");
        }//  ww  w .j av a  2s.c o m
        JParameter param = params[i];
        out.print(param.getType().getParameterizedQualifiedSourceName());
        out.print(" ");
        out.print(param.getName());
    }
}

From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java

License:Apache License

private void printMatchingSuperCall(SourceWriter out, JConstructor constructorToCall) {
    if (constructorToCall.getParameters().length == 0) {
        return; // will be added automatically
    }/*  w w  w . j  a v  a2 s  .  c  o m*/

    out.print("super(");

    JParameter[] params = constructorToCall.getParameters();
    for (int i = 0; i < params.length; i++) {
        if (i > 0) {
            out.print(", ");
        }
        out.print(params[i].getName());
    }
    out.println(");");
}

From source file:com.gwtent.gen.GenUtils.java

License:Apache License

public static boolean hasPublicDefaultConstructor(JClassType classType) {
    for (JConstructor constructor : classType.getConstructors()) {
        if ((constructor.getParameters().length == 0) && constructor.isPublic())
            return true;
    }/*from   w w w  .java  2 s .  c o  m*/

    return false;
}

From source file:fr.onevu.gwt.uibinder.rebind.JClassTypeAdapter.java

License:Apache License

/**
 * Creates a mock GWT constructor for the given java constructor.
 *
 * @param realConstructor the java constructor
 * @param enclosingType the type to which the constructor belongs
 * @return the GWT constructor//from ww  w  .  jav a2s  .  c  om
 */
private JConstructor adaptConstructor(final Constructor<?> realConstructor, JClassType enclosingType) {
    final JConstructor constructor = createMock(JConstructor.class);

    addCommonAbstractMethodBehaviour(realConstructor, constructor, enclosingType);
    addAnnotationBehaviour(realConstructor, constructor);

    // Parameters
    expect(constructor.getParameters()).andStubAnswer(new IAnswer<JParameter[]>() {
        public JParameter[] answer() throws Throwable {
            return adaptParameters(realConstructor.getParameterTypes(),
                    realConstructor.getParameterAnnotations(), constructor);
        }
    });

    // Thrown exceptions
    expect(constructor.getThrows()).andStubAnswer(new IAnswer<JClassType[]>() {
        public JClassType[] answer() throws Throwable {
            Class<?>[] realThrows = realConstructor.getExceptionTypes();
            JClassType[] gwtThrows = new JClassType[realThrows.length];
            for (int i = 0; i < realThrows.length; i++) {
                gwtThrows[i] = (JClassType) adaptType(realThrows[i]);
            }
            return gwtThrows;
        }
    });

    EasyMock.replay(constructor);
    return constructor;
}

From source file:fr.putnami.pwt.core.mvp.rebind.ProxyViewCreator.java

License:Open Source License

private void generateInternalTokenizer(TreeLogger logger, SourceWriter srcWriter) {
    boolean hasTokeConstructor = false;
    for (JConstructor constructor : this.placeType.getConstructors()) {
        if (constructor.getParameters().length == 1 && constructor.getParameters()[0].getType()
                .getSimpleSourceName().equals(String.class.getSimpleName())) {
            hasTokeConstructor = true;//  ww w  .jav  a  2s .c  o m
        }
    }
    srcWriter.println("@Override");
    srcWriter.println("public %s getPlace(String token) {", this.placeType.getSimpleSourceName());
    srcWriter.indent();
    if (hasTokeConstructor) {
        srcWriter.println("return new %s(token);", this.placeType.getSimpleSourceName());
    } else {
        srcWriter.println("%s place = new %s();", this.placeType.getSimpleSourceName(),
                this.placeType.getSimpleSourceName());
        srcWriter.println("place.setToken(token);");
        srcWriter.println("return place;");
    }
    srcWriter.outdent();
    srcWriter.println("}");
    srcWriter.println("@Override");
    srcWriter.println("public String getToken(%s place) {", this.placeType.getSimpleSourceName());
    srcWriter.indent();
    srcWriter.println("if(place instanceof ViewPlace){");
    srcWriter.indent();
    srcWriter.println("return ((ViewPlace)place).getToken();");
    srcWriter.outdent();
    srcWriter.println("}");
    srcWriter.println("return null;");
    srcWriter.outdent();
    srcWriter.println("}");
}

From source file:org.jboss.errai.ioc.rebind.ioc.codegen.meta.impl.gwt.GWTConstructor.java

License:Apache License

public GWTConstructor(JConstructor c) {
    this.constructor = c;
    this.declaringClass = MetaClassFactory.get(c.getEnclosingType());

    try {/*from  w ww .j a va  2 s .com*/
        Class<?> cls = Class.forName(c.getEnclosingType().getQualifiedSourceName(), false,
                Thread.currentThread().getContextClassLoader());

        Constructor constr = cls.getConstructor(InjectUtil.jParmToClass(c.getParameters()));

        annotations = constr.getAnnotations();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}