List of usage examples for com.google.gwt.core.ext.typeinfo JConstructor getParameterTypes
JType[] getParameterTypes();
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.//from w w w.j av a2s . 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:fr.onevu.gwt.uibinder.rebind.TypeOracleUtils.java
License:Apache License
/** * Check for a constructor which is compatible with the supplied argument * types./*from w w w . ja v a2 s. c o m*/ * * @param type * @param argTypes * @return true if a constructor compatible with the supplied arguments exists */ public static boolean hasCompatibleConstructor(JClassType type, JType... argTypes) { // Note that this does not return the constructor, since that is a more // complicated decision about finding the best matching arguments where // more than one are compatible. for (JConstructor ctor : type.getConstructors()) { if (typesAreCompatible(ctor.getParameterTypes(), argTypes, ctor.isVarArgs())) { return true; } } return false; }
From source file:org.eclipse.che.util.ExtensionRegistryGenerator.java
License:Open Source License
/** * Writes dependency gathering code, like: * <p/>/*from w w w. j ava2 s . c o m*/ * Array<DependencyDescription> deps = Collections.<DependencyDescription> createArray(); * deps.add(new DependencyDescription("ide.api.ui.menu", "")); * deps.add(new DependencyDescription("extension.demo", "1.0.0-alpha")); * * @param sw * @param extension * @throws UnableToCompleteException */ private void generateDependenciesForExtension(SourceWriter sw, JClassType extension) throws UnableToCompleteException { // expected code /* Array<DependencyDescription> deps = Collections.<DependencyDescription> createArray(); deps.add(new DependencyDescription("ide.api.ui.menu", "")); */ if (extension.getConstructors().length == 0) { throw new UnableToCompleteException(); } sw.println("List<DependencyDescription> deps = new ArrayList<>();"); JConstructor jConstructor = extension.getConstructors()[0]; JType[] parameterTypes = jConstructor.getParameterTypes(); for (JType jType : parameterTypes) { JClassType argType = jType.isClassOrInterface(); if (argType != null && (argType.isAnnotationPresent(SDK.class) || argType.isAnnotationPresent(Extension.class))) { String id = ""; String version = ""; if (argType.isAnnotationPresent(SDK.class)) { id = argType.getAnnotation(SDK.class).title(); } else if (argType.isAnnotationPresent(Extension.class)) { id = argType.getQualifiedSourceName(); version = argType.getAnnotation(Extension.class).version(); } sw.println("deps.add(new DependencyDescription(\"%s\", \"%s\"));", escape(id), escape(version)); } } }
From source file:org.gwt.json.serialization.SerializerGenerator.java
License:Apache License
protected String constructorArgsForType(String className, TypeOracle typeOracle) { if (JsonDateSerializer.class.getName().equals(className)) { return "\"" + dateFormat + "\""; } else {/* w ww . j a va 2 s. c o m*/ JClassType classType = typeOracle.findType(className); if (classType != null) { for (JConstructor constructor : classType.getConstructors()) { JType[] types = constructor.getParameterTypes(); if (types != null && types.length == 1 && Serializer.class.getName().equals(types[0].getQualifiedSourceName())) { return "this"; } } } } return ""; }
From source file:org.lirazs.gbackbone.generator.ReflectionGenerator.java
License:Apache License
private ModelConstructorType getModelConstructorType(JConstructor constructor) { ModelConstructorType modelConstructorType = ModelConstructorType.EMPTY; JType[] parameterTypes = constructor.getParameterTypes(); int optionsParameterCount = 0; int jsonObjectParameterCount = 0; for (JType parameterType : parameterTypes) { if (parameterType.getQualifiedSourceName().equals("org.lirazs.gbackbone.client.core.data.Options")) optionsParameterCount++;//from ww w. j ava 2s . c o m if (parameterType.getQualifiedSourceName().equals("com.google.gwt.json.client.JSONObject")) jsonObjectParameterCount++; } if (optionsParameterCount == 2) { modelConstructorType = ModelConstructorType.ATTRIBUTES_AND_OPTIONS; } else if (optionsParameterCount == 1) { if (jsonObjectParameterCount == 1) { modelConstructorType = ModelConstructorType.JSON_OBJECT_AND_OPTIONS; } else { modelConstructorType = ModelConstructorType.ATTRIBUTES; } } else if (jsonObjectParameterCount == 1) { modelConstructorType = ModelConstructorType.JSON_OBJECT; } return modelConstructorType; }