List of usage examples for com.google.gwt.core.ext.typeinfo JType isRawType
JRawType isRawType();
null. From source file:com.github.nmorel.gwtjackson.rebind.AbstractCreator.java
License:Apache License
/** * Build a {@link JSerializerType} that instantiate a {@link JsonSerializer} for the given type. If the type is a bean, * the implementation of {@link AbstractBeanJsonSerializer} will be created. * * @param type type//ww w .ja v a 2 s . c o m * @param subtype true if the serializer is for a subtype * * @return the {@link JSerializerType}. Examples: * <ul> * <li>ctx.getIntegerSerializer()</li> * <li>new org.PersonBeanJsonSerializer()</li> * </ul> */ protected final JSerializerType getJsonSerializerFromType(JType type, boolean subtype) throws UnableToCompleteException, UnsupportedTypeException { JSerializerType.Builder builder = new JSerializerType.Builder().type(type); if (null != type.isWildcard()) { // For wildcard type, we use the base type to find the serializer. type = type.isWildcard().getBaseType(); } if (null != type.isRawType()) { // For raw type, we use the base type to find the serializer. type = type.isRawType().getBaseType(); } JTypeParameter typeParameter = type.isTypeParameter(); if (null != typeParameter) { // It's a type parameter like T in 'MyClass<T>' if (!subtype || typeParameter.getDeclaringClass() == getMapperInfo().get().getType()) { // The serializer is created for the main type so we use the serializer field declared for this type. return builder.instance(CodeBlock.builder() .add(String.format(TYPE_PARAMETER_SERIALIZER_FIELD_NAME, typeParameter.getOrdinal())) .add(".json()").build()).build(); } else { // There is no declared serializer so we use the base type to find a serializer. type = typeParameter.getBaseType(); } } Optional<MapperInstance> configuredSerializer = configuration.getSerializer(type); if (configuredSerializer.isPresent()) { // The type is configured in AbstractConfiguration. if (null != type.isParameterized() || null != type.isGenericType()) { JClassType[] typeArgs; if (null != type.isGenericType()) { typeArgs = type.isGenericType().asParameterizedByWildcards().getTypeArgs(); } else { typeArgs = type.isParameterized().getTypeArgs(); } ImmutableList.Builder<JSerializerType> parametersSerializerBuilder = ImmutableList.builder(); for (int i = 0; i < typeArgs.length; i++) { JSerializerType parameterSerializerType; if (configuredSerializer.get().getParameters().length <= i) { break; } if (MapperType.KEY_SERIALIZER == configuredSerializer.get().getParameters()[i]) { parameterSerializerType = getKeySerializerFromType(typeArgs[i]); } else { parameterSerializerType = getJsonSerializerFromType(typeArgs[i], subtype); } parametersSerializerBuilder.add(parameterSerializerType); } ImmutableList<JSerializerType> parametersSerializer = parametersSerializerBuilder.build(); builder.parameters(parametersSerializer); builder.instance( methodCallCodeWithJMapperTypeParameters(configuredSerializer.get(), parametersSerializer)); } else { // The serializer has no parameters. builder.instance(methodCallCode(configuredSerializer.get())); } return builder.build(); } if (typeOracle.isJavaScriptObject(type)) { // It's a JSO and the user didn't give a custom serializer. We use the default one. configuredSerializer = configuration.getSerializer(typeOracle.getJavaScriptObject()); return builder.instance(methodCallCode(configuredSerializer.get())).build(); } if (typeOracle.isEnum(type) || typeOracle.isEnumSupertype(type)) { configuredSerializer = configuration.getSerializer(typeOracle.getEnum()); return builder.instance(methodCallCode(configuredSerializer.get())).build(); } JArrayType arrayType = type.isArray(); if (null != arrayType) { Class arraySerializer; if (arrayType.getRank() == 1) { // One dimension array arraySerializer = ArrayJsonSerializer.class; } else if (arrayType.getRank() == 2) { // Two dimension array arraySerializer = Array2dJsonSerializer.class; } else if (arrayType.getRank() == 3) { // Three dimension array arraySerializer = Array3dJsonSerializer.class; } else { // More dimensions are not supported String message = "Arrays with 4 or more dimensions are not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } JSerializerType parameterSerializerType = getJsonSerializerFromType(arrayType.getLeafType(), subtype); builder.parameters(ImmutableList.of(parameterSerializerType)); builder.instance(CodeBlock.builder() .add("$T.newInstance($L)", arraySerializer, parameterSerializerType.getInstance()).build()); return builder.build(); } if (null != type.isAnnotation()) { String message = "Annotations are not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } JClassType classType = type.isClassOrInterface(); if (null != classType) { // The type is a class or interface and has no default serializer. We generate one. JClassType baseClassType = classType; JParameterizedType parameterizedType = classType.isParameterized(); if (null != parameterizedType) { // It's a bean with generics, we create a serializer based on generic type. baseClassType = parameterizedType.getBaseType(); } BeanJsonSerializerCreator beanJsonSerializerCreator = new BeanJsonSerializerCreator( logger.branch(Type.DEBUG, "Creating serializer for " + baseClassType.getQualifiedSourceName()), context, configuration, typeOracle, baseClassType); BeanJsonMapperInfo mapperInfo = beanJsonSerializerCreator.create(); // Generics and parameterized types serializers have no default constructor. They need serializers for each parameter. ImmutableList<? extends JType> typeParameters = getTypeParameters(classType, subtype); ImmutableList.Builder<JParameterizedSerializer> parametersSerializerBuilder = ImmutableList.builder(); ImmutableList.Builder<JSerializerType> parametersJsonSerializerBuilder = ImmutableList.builder(); for (JType argType : typeParameters) { JSerializerType jsonSerializer = getJsonSerializerFromType(argType, subtype); parametersSerializerBuilder.add(new JParameterizedSerializer( getKeySerializerFromType(argType, subtype, true), jsonSerializer)); parametersJsonSerializerBuilder.add(jsonSerializer); } builder.parameters(parametersJsonSerializerBuilder.build()); builder.beanMapper(true); builder.instance(constructorCallCode( ClassName.get(mapperInfo.getPackageName(), mapperInfo.getSimpleSerializerClassName()), parametersSerializerBuilder.build())); return builder.build(); } String message = "Type '" + type.getQualifiedSourceName() + "' is not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); }
From source file:com.github.nmorel.gwtjackson.rebind.AbstractCreator.java
License:Apache License
/** * Build the {@link JSerializerType} that instantiate a {@link KeySerializer} for the given type. * * @param type type//w ww .j ava2 s .c o m * @param subtype true if the serializer is for a subtype * @param useDefault true if it should return {@link ObjectKeySerializer} if the type is not supported * * @return the {@link JSerializerType}. */ protected final JSerializerType getKeySerializerFromType(JType type, boolean subtype, boolean useDefault) throws UnsupportedTypeException { JSerializerType.Builder builder = new JSerializerType.Builder().type(type); if (null != type.isWildcard()) { // For wildcard type, we use the base type to find the serializer. type = type.isWildcard().getBaseType(); } if (null != type.isRawType()) { // For raw type, we use the base type to find the serializer. type = type.isRawType().getBaseType(); } JTypeParameter typeParameter = type.isTypeParameter(); if (null != typeParameter) { // It's a type parameter like T in 'MyClass<T>' if (!subtype || typeParameter.getDeclaringClass() == getMapperInfo().get().getType()) { // The serializer is created for the main type so we use the serializer field declared for this type. return builder.instance(CodeBlock.builder() .add(String.format(TYPE_PARAMETER_SERIALIZER_FIELD_NAME, typeParameter.getOrdinal())) .add(".key()").build()).build(); } else { // There is no declared serializer so we use the base type to find a serializer. type = typeParameter.getBaseType(); } } Optional<MapperInstance> keySerializer = configuration.getKeySerializer(type); if (keySerializer.isPresent()) { builder.instance(methodCallCode(keySerializer.get())); return builder.build(); } if (typeOracle.isEnum(type) || typeOracle.isEnumSupertype(type)) { keySerializer = configuration.getKeySerializer(typeOracle.getEnum()); return builder.instance(methodCallCode(keySerializer.get())).build(); } if (useDefault) { keySerializer = configuration.getKeySerializer(typeOracle.getJavaLangObject()); if (keySerializer.isPresent()) { builder.instance(methodCallCode(keySerializer.get())); return builder.build(); } } String message = "Type '" + type.getQualifiedSourceName() + "' is not supported as map's key"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); }
From source file:com.github.nmorel.gwtjackson.rebind.AbstractCreator.java
License:Apache License
/** * Build a {@link JDeserializerType} that instantiate a {@link JsonSerializer} for the given type. If the type is a bean, * the implementation of {@link AbstractBeanJsonSerializer} will be created. * * @param type type/* w w w . j a va 2s . c o m*/ * @param subtype true if the deserializer is for a subtype * * @return the {@link JDeserializerType}. Examples: * <ul> * <li>ctx.getIntegerDeserializer()</li> * <li>new org .PersonBeanJsonDeserializer()</li> * </ul> */ protected final JDeserializerType getJsonDeserializerFromType(JType type, boolean subtype) throws UnableToCompleteException, UnsupportedTypeException { JDeserializerType.Builder builder = new JDeserializerType.Builder().type(type); if (null != type.isWildcard()) { // For wildcard type, we use the base type to find the deserializer. type = type.isWildcard().getBaseType(); } if (null != type.isRawType()) { // For raw type, we use the base type to find the deserializer. type = type.isRawType().getBaseType(); } JTypeParameter typeParameter = type.isTypeParameter(); if (null != typeParameter) { // It's a type parameter like T in 'MyClass<T>' if (!subtype || typeParameter.getDeclaringClass() == getMapperInfo().get().getType()) { // The deserializer is created for the main type so we use the deserializer field declared for this type. return builder.instance(CodeBlock.builder() .add(String.format(TYPE_PARAMETER_DESERIALIZER_FIELD_NAME, typeParameter.getOrdinal())) .add(".json()").build()).build(); } else { // There is no declared deserializer so we use the base type to find a deserializer. type = typeParameter.getBaseType(); } } if (typeOracle.isEnumSupertype(type)) { String message = "Type java.lang.Enum is not supported by deserialization"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } Optional<MapperInstance> configuredDeserializer = configuration.getDeserializer(type); if (configuredDeserializer.isPresent()) { // The type is configured in AbstractConfiguration. if (null != type.isParameterized() || null != type.isGenericType()) { JClassType[] typeArgs; if (null != type.isGenericType()) { typeArgs = type.isGenericType().asParameterizedByWildcards().getTypeArgs(); } else { typeArgs = type.isParameterized().getTypeArgs(); } ImmutableList.Builder<JDeserializerType> parametersDeserializerBuilder = ImmutableList.builder(); for (int i = 0; i < typeArgs.length; i++) { JDeserializerType parameterDeserializerType; if (MapperType.KEY_DESERIALIZER == configuredDeserializer.get().getParameters()[i]) { parameterDeserializerType = getKeyDeserializerFromType(typeArgs[i]); } else { parameterDeserializerType = getJsonDeserializerFromType(typeArgs[i], subtype); } parametersDeserializerBuilder.add(parameterDeserializerType); } ImmutableList<JDeserializerType> parametersDeserializer = parametersDeserializerBuilder.build(); builder.parameters(parametersDeserializer); builder.instance(methodCallCodeWithJMapperTypeParameters(configuredDeserializer.get(), parametersDeserializer)); } else { // The deserializer has no parameters. builder.instance(methodCallCode(configuredDeserializer.get())); } return builder.build(); } if (typeOracle.isJavaScriptObject(type)) { // It's a JSO and the user didn't give a custom deserializer. We use the default one. configuredDeserializer = configuration.getDeserializer(typeOracle.getJavaScriptObject()); return builder.instance(methodCallCode(configuredDeserializer.get())).build(); } if (typeOracle.isEnum(type)) { configuredDeserializer = configuration.getDeserializer(typeOracle.getEnum()); return builder .instance( methodCallCodeWithClassParameters(configuredDeserializer.get(), ImmutableList.of(type))) .build(); } JArrayType arrayType = type.isArray(); if (null != arrayType) { TypeSpec arrayCreator; Class<?> arrayDeserializer; JType leafType = arrayType.getLeafType(); int arrayRank = arrayType.getRank(); if (arrayRank < 10) { arrayCreator = ArrayCreator.build(arrayType, arrayRank, leafType); arrayDeserializer = ArrayJsonDeserializer.class; } else { // More dimensions are not supported String message = "Arrays with 4 or more dimensions are not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } JDeserializerType parameterDeserializerType = getJsonDeserializerFromType(leafType, subtype); builder.parameters(ImmutableList.of(parameterDeserializerType)); builder.instance(CodeBlock.builder().add("$T.newInstance($L, $L)", arrayDeserializer, parameterDeserializerType.getInstance(), arrayCreator).build()); return builder.build(); } if (null != type.isAnnotation()) { String message = "Annotations are not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } JClassType classType = type.isClassOrInterface(); if (null != classType) { // The type is a class or interface and has no default deserializer. We generate one. JClassType baseClassType = classType; JParameterizedType parameterizedType = classType.isParameterized(); if (null != parameterizedType) { // It's a bean with generics, we create a deserializer based on generic type. baseClassType = parameterizedType.getBaseType(); } BeanJsonDeserializerCreator beanJsonDeserializerCreator = new BeanJsonDeserializerCreator( logger.branch(Type.DEBUG, "Creating deserializer for " + baseClassType.getQualifiedSourceName()), context, configuration, typeOracle, baseClassType); BeanJsonMapperInfo mapperInfo = beanJsonDeserializerCreator.create(); // Generics and parameterized types deserializers have no default constructor. They need deserializers for each parameter. ImmutableList<? extends JType> typeParameters = getTypeParameters(classType, subtype); ImmutableList.Builder<JParameterizedDeserializer> parametersDeserializerBuilder = ImmutableList .builder(); ImmutableList.Builder<JDeserializerType> parametersJsonDeserializerBuilder = ImmutableList.builder(); for (JType argType : typeParameters) { JDeserializerType jsonDeserializer = getJsonDeserializerFromType(argType, subtype); parametersDeserializerBuilder.add(new JParameterizedDeserializer( getKeyDeserializerFromType(argType, subtype, true), jsonDeserializer)); parametersJsonDeserializerBuilder.add(jsonDeserializer); } builder.parameters(parametersJsonDeserializerBuilder.build()); builder.beanMapper(true); builder.instance(constructorCallCode( ClassName.get(mapperInfo.getPackageName(), mapperInfo.getSimpleDeserializerClassName()), parametersDeserializerBuilder.build())); return builder.build(); } String message = "Type '" + type.getQualifiedSourceName() + "' is not supported"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); }
From source file:com.github.nmorel.gwtjackson.rebind.AbstractCreator.java
License:Apache License
/** * Build the {@link JDeserializerType} that instantiate a {@link KeyDeserializer} for the given type. * * @param type type/*from w w w .ja v a 2 s. c om*/ * @param subtype true if the deserializer is for a subtype * @param useDefault true if it should return a default deserializer in case the type is not supported * * @return the {@link JDeserializerType}. */ protected final JDeserializerType getKeyDeserializerFromType(JType type, boolean subtype, boolean useDefault) throws UnsupportedTypeException { JDeserializerType.Builder builder = new JDeserializerType.Builder().type(type); if (null != type.isWildcard()) { // For wildcard type, we use the base type to find the deserializer. type = type.isWildcard().getBaseType(); } if (null != type.isRawType()) { // For raw type, we use the base type to find the deserializer. type = type.isRawType().getBaseType(); } JTypeParameter typeParameter = type.isTypeParameter(); if (null != typeParameter) { // It's a type parameter like T in 'MyClass<T>' if (!subtype || typeParameter.getDeclaringClass() == getMapperInfo().get().getType()) { // The deserializer is created for the main type so we use the deserializer field declared for this type. return builder.instance(CodeBlock.builder() .add(String.format(TYPE_PARAMETER_DESERIALIZER_FIELD_NAME, typeParameter.getOrdinal())) .add(".key()").build()).build(); } else { // There is no declared deserializer so we use the base type to find a deserializer. type = typeParameter.getBaseType(); } } if (typeOracle.isEnumSupertype(type)) { String message = "Type java.lang.Enum is not supported by deserialization"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); } Optional<MapperInstance> keyDeserializer = configuration.getKeyDeserializer(type); if (keyDeserializer.isPresent()) { builder.instance(methodCallCode(keyDeserializer.get())); return builder.build(); } if (typeOracle.isEnum(type)) { keyDeserializer = configuration.getKeyDeserializer(typeOracle.getEnum()); return builder .instance(methodCallCodeWithClassParameters(keyDeserializer.get(), ImmutableList.of(type))) .build(); } if (useDefault) { keyDeserializer = configuration.getKeyDeserializer(typeOracle.getString()); if (keyDeserializer.isPresent()) { builder.instance(methodCallCode(keyDeserializer.get())); return builder.build(); } } String message = "Type '" + type.getQualifiedSourceName() + "' is not supported as map's key"; logger.log(TreeLogger.Type.WARN, message); throw new UnsupportedTypeException(message); }
From source file:com.github.nmorel.gwtjackson.rebind.property.PropertyProcessor.java
License:Apache License
/** * Extract the bean type from the type given in parameter. For {@link java.util.Collection}, it gives the bounded type. For {@link * java.util.Map}, it gives the second bounded type. Otherwise, it gives the type given in parameter. * * @param type type to extract the bean type * @param propertyName name of the property * * @return the extracted type//from w w w . j a va2 s . c o m */ private static Optional<JClassType> extractBeanType(TreeLogger logger, JacksonTypeOracle typeOracle, JType type, String propertyName) { if (null != type.isWildcard()) { // we use the base type to find the serializer to use type = type.isWildcard().getBaseType(); } if (null != type.isRawType()) { type = type.isRawType().getBaseType(); } JArrayType arrayType = type.isArray(); if (null != arrayType) { return extractBeanType(logger, typeOracle, arrayType.getComponentType(), propertyName); } JClassType classType = type.isClassOrInterface(); if (null == classType) { return Optional.absent(); } else if (typeOracle.isIterable(classType)) { if ((null == classType.isParameterized() || classType.isParameterized().getTypeArgs().length != 1) && (null == classType.isGenericType() || classType.isGenericType().getTypeParameters().length != 1)) { logger.log(Type.INFO, "Expected one argument for the java.lang.Iterable '" + propertyName + "'. Applying annotations to " + "type " + classType.getParameterizedQualifiedSourceName()); return Optional.of(classType); } if (null != classType.isParameterized()) { return extractBeanType(logger, typeOracle, classType.isParameterized().getTypeArgs()[0], propertyName); } else { return extractBeanType(logger, typeOracle, classType.isGenericType().getTypeParameters()[0].getBaseType(), propertyName); } } else if (typeOracle.isMap(classType)) { if ((null == classType.isParameterized() || classType.isParameterized().getTypeArgs().length != 2) && (null == classType.isGenericType() || classType.isGenericType().getTypeParameters().length != 2)) { logger.log(Type.INFO, "Expected two arguments for the java.util.Map '" + propertyName + "'. Applying annotations to " + "type " + classType.getParameterizedQualifiedSourceName()); return Optional.of(classType); } if (null != classType.isParameterized()) { return extractBeanType(logger, typeOracle, classType.isParameterized().getTypeArgs()[1], propertyName); } else { return extractBeanType(logger, typeOracle, classType.isGenericType().getTypeParameters()[1].getBaseType(), propertyName); } } else { return Optional.of(classType); } }
From source file:de.knightsoftnet.validators.rebind.BeanHelper.java
License:Apache License
/** * get association type./*from ww w .ja v a 2s. co m*/ * * @param ppropertyDescriptor property description * @param puseField use field * @return JClassType */ public JClassType getAssociationType(final PropertyDescriptor ppropertyDescriptor, final boolean puseField) { final JType type = this.getElementType(ppropertyDescriptor, puseField); final JArrayType jarray = type.isArray(); if (jarray != null) { return jarray.getComponentType().isClassOrInterface(); } final JParameterizedType jptype = type.isParameterized(); JClassType[] typeArgs; if (jptype == null) { final JRawType jrtype = type.isRawType(); typeArgs = jrtype.getGenericType().getTypeParameters(); } else { typeArgs = jptype.getTypeArgs(); } // it is either a Iterable or a Map use the last type arg. return typeArgs[typeArgs.length - 1].isClassOrInterface(); }
From source file:org.jboss.errai.codegen.meta.impl.gwt.GWTUtil.java
License:Apache License
public static MetaType fromType(final TypeOracle oracle, final JType t) { if (t.isTypeParameter() != null) { return new GWTTypeVariable(oracle, t.isTypeParameter()); } else if (t.isArray() != null && (t.isArray().getComponentType().isTypeParameter() != null || t.isArray().getComponentType().isWildcard() != null)) { return new GWTGenericArrayType(oracle, t.isArray()); } else if (t.isParameterized() != null) { return new GWTParameterizedType(oracle, t.isParameterized()); } else if (t.isWildcard() != null) { return new GWTWildcardType(oracle, t.isWildcard()); } else if (t.isClassOrInterface() != null || t.isEnum() != null || t.isPrimitive() != null || t.isRawType() != null || t.isArray() != null) { return GWTClass.newInstance(oracle, t); } else {// www . ja v a 2s .c om throw new RuntimeException("Don't know how to make a MetaType from given JType " + t + " (which is a " + (t == null ? null : t.getClass()) + ")"); } }