List of usage examples for com.google.gwt.core.ext.typeinfo JType isAnnotation
JAnnotationType isAnnotation();
null if it is not. From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);//from w w w. jav a 2 s. c o m String varName = "t"; if (t instanceof JPrimitiveType) varName = "p"; int id = nextId(); typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id); pb("Type " + varName + " = new Type();"); pb(varName + ".name = \"" + t.getErasedType().getQualifiedSourceName() + "\";"); pb(varName + ".id = " + id + ";"); pb(varName + ".clazz = " + t.getErasedType().getQualifiedSourceName() + ".class;"); if (t instanceof JClassType) { JClassType c = (JClassType) t; if (isVisible(c.getSuperclass())) pb(varName + ".superClass = " + c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class;"); if (c.getFlattenedSupertypeHierarchy() != null) { pb("Set<Class> " + varName + "Assignables = new HashSet<Class>();"); for (JType i : c.getFlattenedSupertypeHierarchy()) { if (!isVisible(i)) continue; pb(varName + "Assignables.add(" + i.getErasedType().getQualifiedSourceName() + ".class);"); } pb(varName + ".assignables = " + varName + "Assignables;"); } if (c.isInterface() != null) pb(varName + ".isInterface = true;"); if (c.isEnum() != null) pb(varName + ".isEnum = true;"); if (c.isArray() != null) pb(varName + ".isArray = true;"); if (c.isMemberType()) pb(varName + ".isMemberClass = true;"); pb(varName + ".isStatic = " + c.isStatic() + ";"); pb(varName + ".isAbstract = " + c.isAbstract() + ";"); if (c.getFields() != null) { pb(varName + ".fields = new Field[] {"); for (JField f : c.getFields()) { String enclosingType = getType(c); String fieldType = getType(f.getType()); int setter = nextId(); int getter = nextId(); String elementType = getElementTypes(f); String annotations = getAnnotations(f.getDeclaredAnnotations()); pb("new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile() + ", " + getter + ", " + setter + ", " + elementType + ", " + annotations + "), "); SetterGetterStub stub = new SetterGetterStub(); stub.name = f.getName(); stub.enclosingType = enclosingType; stub.type = fieldType; stub.isStatic = f.isStatic(); stub.isFinal = f.isFinal(); if (enclosingType != null && fieldType != null) { stub.getter = getter; stub.setter = setter; } setterGetterStubs.add(stub); } pb("};"); } printMethods(c, varName, "Method", c.getMethods()); if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) { printMethods(c, varName, "Constructor", c.getConstructors()); } else { logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated"); } if (c.isArray() != null) { pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";"); } if (c.isEnum() != null) { JEnumConstant[] enumConstants = c.isEnum().getEnumConstants(); if (enumConstants != null) { pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];"); for (int i = 0; i < enumConstants.length; i++) { pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName() + "." + enumConstants[i].getName() + ";"); } } } Annotation[] annotations = c.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { pb(varName + ".annotations = " + getAnnotations(annotations) + ";"); } } else if (t.isAnnotation() != null) { pb(varName + ".isAnnotation = true;"); } else { pb(varName + ".isPrimitive = true;"); } pb("types.put(\"" + t.getErasedType().getQualifiedSourceName() + "\", " + varName + ");"); return buffer.toString(); }
From source file:com.badlogic.gwtref.gen.ReflectionCacheSourceCreator.java
License:Apache License
private String createTypeGenerator(JType t) { buffer.setLength(0);//from w w w . j av a 2 s. c om int id = nextTypeId++; typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id); JClassType c = t.isClass(); String name = t.getErasedType().getQualifiedSourceName(); String superClass = null; if (c != null && (isVisible(c.getSuperclass()))) superClass = c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class"; String assignables = null; String interfaces = null; if (c != null && c.getFlattenedSupertypeHierarchy() != null) { assignables = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getFlattenedSupertypeHierarchy()) { if (!isVisible(i) || i.equals(t) || "java.lang.Object".equals(i.getErasedType().getQualifiedSourceName())) continue; if (used) assignables += ", "; assignables += i.getErasedType().getQualifiedSourceName() + ".class"; used = true; } if (used) assignables += "))"; else assignables = null; } if (c == null) { // if it's not a class, it may be an interface instead c = t.isInterface(); } if (c != null && c.getImplementedInterfaces() != null) { interfaces = "new HashSet<Class>(Arrays.asList("; boolean used = false; for (JType i : c.getImplementedInterfaces()) { if (!isVisible(i) || i.equals(t)) continue; if (used) interfaces += ", "; interfaces += i.getErasedType().getQualifiedSourceName() + ".class"; used = true; } if (used) interfaces += "))"; else interfaces = null; } String varName = "c" + id; pb("private static Type " + varName + ";"); pb("private static Type " + varName + "() {"); pb("if(" + varName + "!=null) return " + varName + ";"); pb(varName + " = new Type(\"" + name + "\", " + id + ", " + name + ".class, " + superClass + ", " + assignables + ", " + interfaces + ");"); if (c != null) { if (c.isEnum() != null) pb(varName + ".isEnum = true;"); if (c.isArray() != null) pb(varName + ".isArray = true;"); if (c.isMemberType()) pb(varName + ".isMemberClass = true;"); if (c.isInterface() != null) { pb(varName + ".isInterface = true;"); } else { pb(varName + ".isStatic = " + c.isStatic() + ";"); pb(varName + ".isAbstract = " + c.isAbstract() + ";"); } if (c.getFields() != null && c.getFields().length > 0) { pb(varName + ".fields = new Field[] {"); for (JField f : c.getFields()) { String enclosingType = getType(c); String fieldType = getType(f.getType()); int setterGetter = nextSetterGetterId++; String elementType = getElementTypes(f); String annotations = getAnnotations(f.getDeclaredAnnotations()); pb(" new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile() + ", " + setterGetter + ", " + setterGetter + ", " + elementType + ", " + annotations + "), "); SetterGetterStub stub = new SetterGetterStub(); stub.name = f.getName(); stub.enclosingType = enclosingType; stub.type = fieldType; stub.isStatic = f.isStatic(); stub.isFinal = f.isFinal(); if (enclosingType != null && fieldType != null) { stub.getter = setterGetter; stub.setter = setterGetter; } setterGetterStubs.add(stub); } pb("};"); } createTypeInvokables(c, varName, "Method", c.getMethods()); if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) { createTypeInvokables(c, varName, "Constructor", c.getConstructors()); } else { logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated"); } if (c.isArray() != null) { pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";"); } if (c.isEnum() != null) { JEnumConstant[] enumConstants = c.isEnum().getEnumConstants(); if (enumConstants != null) { pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];"); for (int i = 0; i < enumConstants.length; i++) { pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName() + "." + enumConstants[i].getName() + ";"); } } } Annotation[] annotations = c.getDeclaredAnnotations(); if (annotations != null && annotations.length > 0) { pb(varName + ".annotations = " + getAnnotations(annotations) + ";"); } } else if (t.isAnnotation() != null) { pb(varName + ".isAnnotation = true;"); } else { pb(varName + ".isPrimitive = true;"); } pb("return " + varName + ";"); pb("}"); return buffer.toString(); }
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// w w w . j a va2s . 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 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//from w ww .j a v a 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.gwtent.gen.reflection.ReflectAllInOneCreator.java
License:Apache License
private void processJType(JType type) { JClassType classType = null;//from w ww. ja v a2 s .c o m if (type.isClassOrInterface() != null) { classType = type.isClassOrInterface(); } else if (type.isArray() != null) { processJType(type.isArray().getComponentType()); } else if (type.isAnnotation() != null) { classType = type.isAnnotation(); } if (classType != null) processClass(classType, getNearestSetting(classType, getFullSettings())); }