Java Code Examples for com.squareup.javapoet.TypeName#isPrimitive()
The following examples show how to use
com.squareup.javapoet.TypeName#isPrimitive() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeRule.java From caffeine with Apache License 2.0 | 6 votes |
/** Creates a mutator to the variable. */ protected final MethodSpec newSetter(TypeName varType, String varName, Visibility visibility) { String methodName = "set" + Character.toUpperCase(varName.charAt(0)) + varName.substring(1); String type; if (varType.isPrimitive()) { type = varType.equals(TypeName.INT) ? "Int" : "Long"; } else { type = "Object"; } MethodSpec.Builder setter = MethodSpec.methodBuilder(methodName) .addModifiers(context.publicFinalModifiers()) .addParameter(varType, varName); if (visibility.isRelaxed) { setter.addStatement("$T.UNSAFE.put$L(this, $N, $N)", UNSAFE_ACCESS, type, offsetName(varName), varName); } else { setter.addStatement("this.$N = $N", varName, varName); } return setter.build(); }
Example 2
Source File: SQLTransformer.java From kripton with Apache License 2.0 | 5 votes |
/** * Checks if is supported JDK type. * * @param typeName * the type name * @return true, if is supported JDK type */ public static boolean isSupportedJDKType(TypeName typeName) { if (typeName.isPrimitive()) { return getPrimitiveTransform(typeName) != null; } String name = typeName.toString(); if (name.startsWith("java.lang")) { return getLanguageTransform(typeName) != null; } if (name.startsWith("java.util")) { return getUtilTransform(typeName) != null; } if (name.startsWith("java.math")) { return getMathTransform(typeName) != null; } if (name.startsWith("java.net")) { return getNetTransform(typeName) != null; } if (name.startsWith("java.sql")) { return getSqlTransform(typeName) != null; } return false; }
Example 3
Source File: AutoBundleWriter.java From AutoBundle with Apache License 2.0 | 5 votes |
private static MethodSpec createBuilderConstructor(AutoBundleBindingClass target, String fieldName) { MethodSpec.Builder builder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addStatement("this.$N = new $T()", fieldName, CLASS_BUNDLE); for (AutoBundleBindingField arg : target.getRequiredArgs()) { String key = arg.getArgKey(); TypeName type = arg.getArgType(); // parameter for constructor // support annotation ParameterSpec.Builder paramBuilder = ParameterSpec.builder(type, key); if (arg.hasAnnotations()) { arg.getAnnotations().forEach(paramBuilder::addAnnotation); } // nonnull if (!type.isPrimitive()) { paramBuilder.addAnnotation(ANNOTATION_NONNULL); } builder.addParameter(paramBuilder.build()); // statement String operationName = arg.getOperationName("put"); if (arg.hasCustomConverter()) { TypeName converter = arg.getConverter(); builder.addStatement("$T $NConverter = new $T()", converter, key, converter) .addStatement("this.$N.$N($S, $NConverter.convert($N) )", fieldName, operationName, key, key, key); } else { builder.addStatement("this.$N.$N($S, $N)", fieldName, operationName, key, key); } } return builder.build(); }
Example 4
Source File: PaperParcelWriter.java From paperparcel with Apache License 2.0 | 5 votes |
@SuppressWarnings("ConstantConditions") private void writeField( MethodSpec.Builder builder, FieldDescriptor field, CodeBlock accessorBlock, ParameterSpec dest, ParameterSpec flags) { TypeName fieldTypeName = TypeName.get(field.type().get()); if (fieldTypeName.isPrimitive()) { if (TypeName.BOOLEAN.equals(fieldTypeName)) { builder.addStatement("$N.writeInt($L ? 1 : 0)", dest, accessorBlock); } else if (TypeName.INT.equals(fieldTypeName)) { builder.addStatement("$N.writeInt($L)", dest, accessorBlock); } else if (TypeName.LONG.equals(fieldTypeName)) { builder.addStatement("$N.writeLong($L)", dest, accessorBlock); } else if (TypeName.DOUBLE.equals(fieldTypeName)) { builder.addStatement("$N.writeDouble($L)", dest, accessorBlock); } else if (TypeName.FLOAT.equals(fieldTypeName)) { builder.addStatement("$N.writeFloat($L)", dest, accessorBlock); } else if (TypeName.CHAR.equals(fieldTypeName)) { builder.addStatement("$N.writeInt($L)", dest, accessorBlock); } else if (TypeName.BYTE.equals(fieldTypeName)) { builder.addStatement("$N.writeByte($L)", dest, accessorBlock); } else if (TypeName.SHORT.equals(fieldTypeName)) { builder.addStatement("$N.writeInt($L)", dest, accessorBlock); } else { throw new IllegalArgumentException("Unknown primitive type: " + fieldTypeName); } } else { AdapterDescriptor adapter = descriptor.adapters().get(field); CodeBlock adapterInstance = adapterInstance(adapter); if (field.isNullable() && !adapter.nullSafe()) { builder.addStatement("$T.writeNullable($L, $N, $N, $L)", UTILS, accessorBlock, dest, flags, adapterInstance); } else { builder.addStatement("$L.writeToParcel($L, $N, $N)", adapterInstance, accessorBlock, dest, flags); } } }
Example 5
Source File: PaperParcelWriter.java From paperparcel with Apache License 2.0 | 5 votes |
@SuppressWarnings("ConstantConditions") private FieldSpec readField(String fieldName, FieldDescriptor field, ParameterSpec in) { TypeName fieldTypeName = TypeName.get(field.type().get()); FieldSpec.Builder builder = FieldSpec.builder(fieldTypeName, fieldName); if (fieldTypeName.isPrimitive()) { if (TypeName.BOOLEAN.equals(fieldTypeName)) { builder.initializer("$N.readInt() == 1", in); } else if (TypeName.INT.equals(fieldTypeName)) { builder.initializer("$N.readInt()", in); } else if (TypeName.LONG.equals(fieldTypeName)) { builder.initializer("$N.readLong()", in); } else if (TypeName.DOUBLE.equals(fieldTypeName)) { builder.initializer("$N.readDouble()", in); } else if (TypeName.FLOAT.equals(fieldTypeName)) { builder.initializer("$N.readFloat()", in); } else if (TypeName.CHAR.equals(fieldTypeName)) { builder.initializer("(char) $N.readInt()", in); } else if (TypeName.BYTE.equals(fieldTypeName)) { builder.initializer("$N.readByte()", in); } else if (TypeName.SHORT.equals(fieldTypeName)) { builder.initializer("(short) $N.readInt()", in); } else { throw new IllegalArgumentException("Unknown primitive type: " + fieldTypeName); } } else { AdapterDescriptor adapter = descriptor.adapters().get(field); CodeBlock adapterInstance = adapterInstance(adapter); if (field.isNullable() && !adapter.nullSafe()) { builder.initializer("$T.readNullable($N, $L)", UTILS, in, adapterInstance); } else { builder.initializer("$L.readFromParcel($N)", adapterInstance, in); } } return builder.build(); }
Example 6
Source File: AdapterNameGenerator.java From paperparcel with Apache License 2.0 | 5 votes |
private String getNameInternal(TypeName typeName) { String adapterName = null; if (typeName instanceof WildcardTypeName) { WildcardTypeName wildcardTypeName = (WildcardTypeName) typeName; String upperBoundsPart = ""; String lowerBoundsPart = ""; for (TypeName upperBound : wildcardTypeName.upperBounds) { upperBoundsPart += getNameInternal(upperBound); } for (TypeName lowerBound : wildcardTypeName.lowerBounds) { lowerBoundsPart += getNameInternal(lowerBound); } adapterName = upperBoundsPart + lowerBoundsPart; } if (typeName instanceof ArrayTypeName) { ArrayTypeName arrayTypeName = (ArrayTypeName) typeName; adapterName = getNameInternal(arrayTypeName.componentType) + "Array"; } if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; String paramPart = ""; for (TypeName param : parameterizedTypeName.typeArguments) { paramPart += getNameInternal(param); } adapterName = paramPart + parameterizedTypeName.rawType.simpleName(); } if (typeName instanceof ClassName) { ClassName className = (ClassName) typeName; adapterName = Joiner.on("_").join(className.simpleNames()); } if (typeName.isPrimitive()) { adapterName = typeName.toString(); } if (adapterName == null) { throw new AssertionError(); } return adapterName; }
Example 7
Source File: FitProcessor.java From fit with Apache License 2.0 | 5 votes |
private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName typeName, String putMethod, String valueL, String propertyName) { TypeName unboxFieldTypeName = unbox(typeName); if (TypeName.DOUBLE.equals(typeName)) { valueL = "Double.doubleToLongBits( obj." + valueL + ")"; builder.addStatement("editor.$L($S, " + valueL + ")", putMethod, propertyName); return builder; } else if (setOfHoverboards.equals(unboxFieldTypeName) || hashSetOfHoverboards.equals( unboxFieldTypeName)) { builder.addStatement("$T.$L($L, $S, obj." + valueL + ")", UTILS, putMethod, "editor", propertyName); return builder; } if (typeName.isBoxedPrimitive()) { if (TypeName.DOUBLE.equals(unboxFieldTypeName)) { builder.addStatement( "editor.$L($S, Double.doubleToLongBits($T.checkNonNull(obj.$L) ? obj.$L : 0))", putMethod, propertyName, UTILS, propertyName, propertyName); } else if (TypeName.CHAR.equals(unboxFieldTypeName) || TypeName.BYTE.equals( unboxFieldTypeName) || TypeName.SHORT.equals(unboxFieldTypeName) || TypeName.INT.equals( unboxFieldTypeName) || TypeName.LONG.equals(unboxFieldTypeName) || TypeName.FLOAT.equals( unboxFieldTypeName)) { builder.addStatement( "editor.$L($S, $T.checkNonNull( obj." + valueL + ") ? obj." + valueL + " : 0)", putMethod, propertyName, UTILS); } else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) { builder.addStatement("editor.$L($S, $T.checkNonNull(obj.$L) ? obj.$L : false)", putMethod, propertyName, UTILS, propertyName, propertyName); } } else { if (stringTypeName.equals(unboxFieldTypeName) || typeName.isPrimitive()) { builder.addStatement("editor.$L($S, obj." + valueL + ")", putMethod, propertyName); } else { builder.addStatement("$T.writeObject(context, name + $S, obj.$L)", FILE_OBJECT_UTIL, "." + propertyName, valueL); } } return builder; }
Example 8
Source File: FitProcessor.java From fit with Apache License 2.0 | 5 votes |
private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName typeName, String putMethod, String valueL, String propertyName) { TypeName unboxFieldTypeName = unbox(typeName); if (TypeName.DOUBLE.equals(typeName)) { valueL = "Double.doubleToLongBits( obj." + valueL + ")"; builder.addStatement("editor.$L($S, " + valueL + ")", putMethod, propertyName); return builder; } else if (setOfHoverboards.equals(unboxFieldTypeName) || hashSetOfHoverboards.equals( unboxFieldTypeName)) { builder.addStatement("$T.$L($L, $S, obj." + valueL + ")", UTILS, putMethod, "editor", propertyName); return builder; } if (typeName.isBoxedPrimitive()) { if (TypeName.DOUBLE.equals(unboxFieldTypeName)) { builder.addStatement( "editor.$L($S, Double.doubleToLongBits($T.checkNonNull(obj.$L) ? obj.$L : 0))", putMethod, propertyName, UTILS, propertyName, propertyName); } else if (TypeName.CHAR.equals(unboxFieldTypeName) || TypeName.BYTE.equals( unboxFieldTypeName) || TypeName.SHORT.equals(unboxFieldTypeName) || TypeName.INT.equals( unboxFieldTypeName) || TypeName.LONG.equals(unboxFieldTypeName) || TypeName.FLOAT.equals( unboxFieldTypeName)) { builder.addStatement( "editor.$L($S, $T.checkNonNull( obj." + valueL + ") ? obj." + valueL + " : 0)", putMethod, propertyName, UTILS); } else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) { builder.addStatement("editor.$L($S, $T.checkNonNull(obj.$L) ? obj.$L : false)", putMethod, propertyName, UTILS, propertyName, propertyName); } } else { if (stringTypeName.equals(unboxFieldTypeName) || typeName.isPrimitive()) { builder.addStatement("editor.$L($S, obj." + valueL + ")", putMethod, propertyName); } else { builder.addStatement("$T.writeObject(context, name + $S, obj.$L)", FILE_OBJECT_UTIL, "." + propertyName, valueL); } } return builder; }
Example 9
Source File: StateProperty.java From reductor with Apache License 2.0 | 5 votes |
public TypeName getReducerInterfaceTypeName() { TypeName stateType = TypeName.get(this.stateType); if (stateType.isPrimitive()) { stateType = stateType.box(); } return ParameterizedTypeName.get(ClassName.get(Reducer.class), stateType); }
Example 10
Source File: JniProcessor.java From cronet with BSD 3-Clause "New" or "Revised" License | 5 votes |
boolean shouldDowncastToObjectForJni(TypeName t) { if (t.isPrimitive()) { return false; } // There are some non-primitives that should not be downcasted. return !JNI_OBJECT_TYPE_EXCEPTIONS.contains(t.toString()); }
Example 11
Source File: WriterUtil.java From sqlitemagic with Apache License 2.0 | 4 votes |
public static String getDefaultValue(@NonNull TypeName typeName) { if (!typeName.isPrimitive()) { return "null"; } return PRIMITIVES_DEFAULT_VALUE_MAP.get(typeName); }
Example 12
Source File: BindTransformer.java From kripton with Apache License 2.0 | 4 votes |
/** * Gets the transform. * * @param typeName * the type name * @return the transform */ static BindTransform getTransform(TypeName typeName) { if (typeName.isPrimitive()) { return getPrimitiveTransform(typeName); } if (typeName instanceof ArrayTypeName) { ArrayTypeName typeNameArray = (ArrayTypeName) typeName; if (TypeUtility.isEquals(typeNameArray.componentType, Byte.TYPE.toString())) { return new ByteArrayBindTransform(); } else { return new ArrayBindTransform(typeNameArray.componentType, typeNameArray.componentType.isPrimitive()); } } else if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; if (TypeUtility.isList(parameterizedTypeName.rawType)) { return new ListBindTransformation(parameterizedTypeName); } else if (TypeUtility.isSet(parameterizedTypeName.rawType)) { return new SetBindTransformation(parameterizedTypeName); } else if (TypeUtility.isMap(parameterizedTypeName.rawType)) { return new MapBindTransformation(parameterizedTypeName); } } String name = typeName.toString(); if (name.startsWith("java.lang")) { return getLanguageTransform(typeName); } if (name.startsWith("java.util")) { return getUtilTransform(typeName); } if (name.startsWith("java.math")) { return getMathTransform(typeName); } if (name.startsWith("java.net")) { return getNetTransform(typeName); } if (name.startsWith("java.sql")) { return getSqlTransform(typeName); } if (TypeUtility.isEnum(typeName)) { return new EnumBindTransform(typeName); } // for default is treated as object return new ObjectBindTransform(); }
Example 13
Source File: TypeVariableResolver.java From kripton with Apache License 2.0 | 4 votes |
/** * Resolve. * * @param inputType the input type * @return the type name */ public TypeName resolve(TypeName inputType) { if (!hasTypeVariables()) return inputType; if (inputType.isPrimitive() || inputType.isBoxedPrimitive() || inputType.equals(TypeName.get(String.class))) { return inputType; } if (TypeName.VOID.equals(inputType)) return inputType; TypeName resolved=null; if (inputType instanceof ParameterizedTypeName) { ParameterizedTypeName inputParametrizedTypeName=(ParameterizedTypeName)inputType; TypeName[] typeArguments=new TypeName[inputParametrizedTypeName.typeArguments.size()]; int i=0; for (TypeName item: inputParametrizedTypeName.typeArguments) { typeArguments[i]=resolve(item); i++; } resolved=ParameterizedTypeName.get((ClassName)resolve(inputParametrizedTypeName.rawType), typeArguments); return resolved; } else if (inputType instanceof ArrayTypeName) { ArrayTypeName inputTypeName=(ArrayTypeName)inputType; resolved=ArrayTypeName.of(resolve(inputTypeName.componentType)); return resolved; } else { // it's not a type variable if (inputType.toString().contains(".")) { return inputType; } // ASSERT: simple typeName if (typeVariableMap.containsKey(inputType.toString())) { TypeName type = typeVariableMap.get(inputType.toString()); return type; } else if (declaredTypeArgumentList.size()==1){ resolved = declaredTypeArgumentList.get(0); // if we found an unique type variable, we use it. //typeVariableMap = new HashMap<>(); //typeVariableMap.put(inputTypeName.toString(), resolved); return resolved; } AssertKripton.assertTrue(false, "In type hierarchy of %s '%s' there is a unresolved type variable named '%s'. Define it with @BindTypeVariables", element.getKind()==ElementKind.CLASS ? "class" :"interface", element.getQualifiedName(), inputType.toString()); return resolved; } }
Example 14
Source File: ProcessUtils.java From Freezer with Apache License 2.0 | 4 votes |
public static boolean isPrimitive(TypeName typeName) { return typeName.isPrimitive() || unbox(typeName).isPrimitive() || (ClassName.get(String.class).equals(typeName)) || isDate(typeName); }
Example 15
Source File: ProcessUtils.java From RxAndroidOrm with Apache License 2.0 | 4 votes |
public static boolean isPrimitive(TypeName typeName) { return typeName.isPrimitive() || unbox(typeName).isPrimitive() || (ClassName.get(String.class).equals(typeName)) || isDate(typeName); }
Example 16
Source File: ComponentBodyGenerator.java From litho with Apache License 2.0 | 4 votes |
private static @Comparable.Type int getComparableType( TypeName typeName, com.facebook.litho.specmodels.model.TypeSpec typeSpec, EnumSet<RunMode> runMode) { if (typeName.equals(TypeName.FLOAT)) { return Comparable.FLOAT; } else if (typeName.equals(TypeName.DOUBLE)) { return Comparable.DOUBLE; } else if (typeName instanceof ArrayTypeName) { return Comparable.ARRAY; } else if (typeName.isPrimitive()) { return Comparable.PRIMITIVE; } else if (typeName.equals(ClassNames.COMPARABLE_DRAWABLE)) { return Comparable.COMPARABLE_DRAWABLE; } else if (!runMode.contains(RunMode.ABI) && typeSpec.isSubInterface(ClassNames.COLLECTION)) { final int level = calculateLevelOfComponentInCollections((DeclaredTypeSpec) typeSpec); switch (level) { case 0: return Comparable.COLLECTION_COMPLEVEL_0; case 1: return Comparable.COLLECTION_COMPLEVEL_1; case 2: return Comparable.COLLECTION_COMPLEVEL_2; case 3: return Comparable.COLLECTION_COMPLEVEL_3; case 4: return Comparable.COLLECTION_COMPLEVEL_4; default: throw new IllegalStateException("Collection Component level not supported."); } } else if (typeName.equals(ClassNames.COMPONENT)) { return Comparable.COMPONENT; } else if (typeName.equals(ClassNames.SECTION)) { return Comparable.SECTION; } else if (typeName.equals(ClassNames.EVENT_HANDLER)) { return Comparable.EVENT_HANDLER; } else if (typeName instanceof ParameterizedTypeName && ((ParameterizedTypeName) typeName).rawType.equals(ClassNames.EVENT_HANDLER)) { return Comparable.EVENT_HANDLER_IN_PARAMETERIZED_TYPE; } return Comparable.OTHER; }
Example 17
Source File: EnumsProviderClassBuilder.java From aircon with MIT License | 4 votes |
private MethodSpec createEnumProviderMethod(final TypeElement enumClass, final List<VariableElement> consts) { final String methodName = Consts.GETTER_METHOD_PREFIX + enumClass.getSimpleName(); final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(TypeName.get(enumClass.asType())); final TypeName parameterType = getRemoteValueType(consts); builder.addParameter(parameterType, PARAMETER_VALUE, Modifier.FINAL); builder.addParameter(parameterType, PARAMETER_DEFAULT_VALUE, Modifier.FINAL); final CodeBlockBuilder bodyCodeBuilder = new CodeBlockBuilder(); final boolean checkNull = !parameterType.isPrimitive(); if (checkNull) { bodyCodeBuilder.beginIf(new CodeBlockBuilder().addBinaryOperator(CodeBlockBuilder.OPERATOR_UNEQUALITY, PARAMETER_VALUE, null) .build()); } bodyCodeBuilder.beginSwitch(PARAMETER_VALUE); for (VariableElement variableElement : consts) { final CodeBlock enumConst = new CodeBlockBuilder().addClassQualifier(TypeName.get(enumClass.asType())) .add(variableElement.getSimpleName()) .build(); bodyCodeBuilder.addSwitchCase(getRemoteValue(variableElement), new CodeBlockBuilder().addReturn(enumConst) .build()); } bodyCodeBuilder.endSwitch(); if (checkNull) { bodyCodeBuilder.endIf(); } final CodeBlock condition = new CodeBlockBuilder().addBinaryOperator(CodeBlockBuilder.OPERATOR_UNEQUALITY, PARAMETER_VALUE, PARAMETER_DEFAULT_VALUE) .build(); final CodeBlock recursiveCall = new CodeBlockBuilder().addMethodCall(methodName, PARAMETER_DEFAULT_VALUE, PARAMETER_DEFAULT_VALUE) .build(); bodyCodeBuilder.addReturn(new CodeBlockBuilder().addConditionalExpression(condition, recursiveCall, null) .build()); return builder.addCode(bodyCodeBuilder.build()) .build(); }
Example 18
Source File: PrefsTransformer.java From kripton with Apache License 2.0 | 4 votes |
/** * Gets the transform. * * @param typeName the type name * @return the transform */ private static PrefsTransform getTransform(TypeName typeName) { if (typeName.isPrimitive()) { return getPrimitiveTransform(typeName); } if (typeName instanceof ArrayTypeName) { ArrayTypeName typeNameArray = (ArrayTypeName) typeName; TypeName componentTypeName = typeNameArray.componentType; if (TypeUtility.isEquals(componentTypeName, Byte.TYPE.toString())) { return new ByteArrayPrefsTransform(); } else { return new ArrayPrefsTransform(); } } else if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; if (TypeUtility.isList(parameterizedTypeName)) { return new ListPrefsTransformation(); } else if (TypeUtility.isSet(parameterizedTypeName)) { return new SetPrefsTransformation(); } else if (TypeUtility.isMap(parameterizedTypeName)) { return new MapPrefsTransformation(); } } PrefsTransform transform; transform = getLanguageTransform(typeName); if (transform != null) { return transform; } transform = getUtilTransform(typeName); if (transform != null) { return transform; } transform = getMathTransform(typeName); if (transform != null) { return transform; } transform = getNetTransform(typeName); if (transform != null) { return transform; } transform = getSqlTransform(typeName); if (transform != null) { return transform; } if (TypeUtility.isEnum(typeName)) { return new EnumPrefsTransform(typeName); } return new ObjectPrefsTransform(); }
Example 19
Source File: SQLTransformer.java From kripton with Apache License 2.0 | 4 votes |
/** * Gets the transform. * * @param typeName * the type name * @return the transform */ private static SQLTransform getTransform(TypeName typeName) { if (typeName.isPrimitive()) { return getPrimitiveTransform(typeName); } if (typeName instanceof ArrayTypeName) { ArrayTypeName typeNameArray = (ArrayTypeName) typeName; TypeName componentTypeName = typeNameArray.componentType; if (TypeUtility.isEquals(componentTypeName, Byte.TYPE.toString())) { return new ByteArraySQLTransform(); } else { return new ArraySQLTransform(); } } else if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; if (TypeUtility.isList(parameterizedTypeName)) { return new ListSQLTransformation(); } else if (TypeUtility.isSet(parameterizedTypeName)) { return new SetSQLTransformation(); } else if (TypeUtility.isMap(parameterizedTypeName)) { return new MapSQLTransformation(); } } String name = typeName.toString(); if (name.startsWith("java.lang")) { return getLanguageTransform(typeName); } if (name.startsWith("java.util")) { return getUtilTransform(typeName); } if (name.startsWith("java.math")) { return getMathTransform(typeName); } if (name.startsWith("java.net")) { return getNetTransform(typeName); } if (name.startsWith("java.sql")) { return getSqlTransform(typeName); } if (TypeUtility.isEnum(typeName)) { return new EnumSQLTransform(typeName); } return new ObjectSQLTransform(); }
Example 20
Source File: RpcRegisterGenerator.java From fastjgame with Apache License 2.0 | 4 votes |
/** * 生成方法调用代码,没有分号和换行符。 * {@code Object result = instance.rpcMethod(a, b, c)} * {@code instance.rpcMethod(a, b, c)} */ private RpcRegisterGenerator.InvokeStatement genInvokeStatement(ExecutableElement method) { // 缩进 final StringBuilder format = new StringBuilder(" "); final List<Object> params = new ArrayList<>(method.getParameters().size()); if (method.getReturnType().getKind() != TypeKind.VOID) { // 声明返回值 final TypeName returnTypeName = ParameterizedTypeName.get(method.getReturnType()); format.append("$T result = "); params.add(returnTypeName); } // 调用方法 format.append("$L.$L("); params.add(instance); params.add(method.getSimpleName().toString()); boolean needDelimiter = false; int index = 0; for (VariableElement variableElement : method.getParameters()) { if (needDelimiter) { format.append(", "); } else { needDelimiter = true; } if (processor.isContext(variableElement)) { format.append(context); continue; } final TypeName parameterTypeName = ParameterizedTypeName.get(variableElement.asType()); if (parameterTypeName.isPrimitive()) { // 基本类型需要两次转换,否则可能导致重载问题 // (int)((Integer)methodParams.get(index)) // eg: // getName(int age); // getName(Integer age); format.append("($T)(($T)$L.get($L))"); params.add(parameterTypeName); params.add(parameterTypeName.box()); } else { format.append("($T)$L.get($L)"); params.add(parameterTypeName); } params.add(methodParams); params.add(index); index++; } format.append(")"); return new RpcRegisterGenerator.InvokeStatement(format.toString(), params); }