com.squareup.javapoet.TypeName Java Examples
The following examples show how to use
com.squareup.javapoet.TypeName.
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: Tuple2CasesGenerator.java From motif with Apache License 2.0 | 6 votes |
public static void main(String[] args) { TypeName A = TypeVariableName.get("A"); TypeName B = TypeVariableName.get("B"); TypeName t = ParameterizedTypeName.get(ClassName.get(Tuple2.class), A, B); Match2MethodSpec tuple2Match = Match2MethodSpec.builder() .withName("tuple2").withSummaryJavadoc("Matches a tuple of 2 elements.\n") .withMatchExtractor(Tuple2FieldExtractor.class).withParamA(A, "a").withParamB(B, "b") .build(); JavaFile tuple2CasesFile = CasesGenerator.newBuilder( "com.leacox.motif.cases", "Tuple2Cases", t) .addFileComment(Copyright.COPYRIGHT_NOTICE) .addJavadoc("Motif cases for matching a {@link Tuple2}.\n") .addMatch2Method(tuple2Match) .build().generate(); try { tuple2CasesFile.writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } }
Example #2
Source File: ObjectBindTransform.java From kripton with Apache License 2.0 | 6 votes |
@Override public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property) { // TODO QUA // TypeName typeName = resolveTypeName(property.getParent(), // property.getPropertyType().getTypeName()); TypeName typeName = property.getPropertyType().getTypeName(); String bindName = context.getBindMapperName(context, typeName); // @formatter:off if (property.isNullable() && !property.isInCollection()) { methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); } methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, BindProperty.xmlName(property)); methodBuilder.addStatement("$L.serializeOnXml($L, xmlSerializer, $T.$L)", bindName, getter(beanName, beanClass, property), EventType.class, EventType.START_TAG); methodBuilder.addStatement("$L.writeEndElement()", serializerName); if (property.isNullable() && !property.isInCollection()) { methodBuilder.endControlFlow(); } // @formatter:on }
Example #3
Source File: BaseMatchMethodPermutationBuilder.java From motif with Apache License 2.0 | 6 votes |
/** * Returns the statement arguments for the match method returns statement. */ protected List<TypeName> getReturnStatementArgs(MatchType matchType, TypeName paramType) { List<TypeName> extractA; if (matchType == MatchType.DECOMPOSE) { TypeName u = match(paramType) .when(typeOf(TypeVariableName.class)).get( x -> (TypeName) TypeVariableName.get("E" + x.name, x)) .orElse(x -> x) .getMatch(); extractA = ImmutableList.of(u); } else if (matchType == MatchType.ANY) { extractA = ImmutableList.of(paramType); } else { extractA = ImmutableList.of(); } return extractA; }
Example #4
Source File: FitProcessor.java From fit with Apache License 2.0 | 6 votes |
private String genPutMethod(TypeName fieldTypeName) { TypeName unboxFieldTypeName = unbox(fieldTypeName); String putMethod = ""; if (stringTypeName.equals(unboxFieldTypeName)) { putMethod = "putString"; } else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) { putMethod = "putBoolean"; } else if (TypeName.FLOAT.equals(unboxFieldTypeName)) { putMethod = "putFloat"; } else if (TypeName.INT.equals(unboxFieldTypeName) || TypeName.BYTE.equals(unboxFieldTypeName) || TypeName.SHORT.equals(unboxFieldTypeName) || TypeName.CHAR.equals(unboxFieldTypeName)) { putMethod = "putInt"; } else if (TypeName.LONG.equals(unboxFieldTypeName)) { putMethod = "putLong"; } else if (TypeName.DOUBLE.equals(unboxFieldTypeName)) { putMethod = "putLong"; } else if (setOfHoverboards.equals(unboxFieldTypeName) || hashSetOfHoverboards.equals( unboxFieldTypeName)) { putMethod = "putStringSet"; } return putMethod; }
Example #5
Source File: JTypeName.java From gwt-jackson with Apache License 2.0 | 6 votes |
private TypeName primitiveName( JPrimitiveType type, boolean boxed ) { if ( "boolean".equals( type.getSimpleSourceName() ) ) { return boxed ? BOOLEAN_NAME : TypeName.BOOLEAN; } else if ( "byte".equals( type.getSimpleSourceName() ) ) { return boxed ? BYTE_NAME : TypeName.BYTE; } else if ( "short".equals( type.getSimpleSourceName() ) ) { return boxed ? SHORT_NAME : TypeName.SHORT; } else if ( "int".equals( type.getSimpleSourceName() ) ) { return boxed ? INTEGER_NAME : TypeName.INT; } else if ( "long".equals( type.getSimpleSourceName() ) ) { return boxed ? LONG_NAME : TypeName.LONG; } else if ( "char".equals( type.getSimpleSourceName() ) ) { return boxed ? CHARACTER_NAME : TypeName.CHAR; } else if ( "float".equals( type.getSimpleSourceName() ) ) { return boxed ? FLOAT_NAME : TypeName.FLOAT; } else if ( "double".equals( type.getSimpleSourceName() ) ) { return boxed ? DOUBLE_NAME : TypeName.DOUBLE; } else { return boxed ? VOID_NAME : TypeName.VOID; } }
Example #6
Source File: GsonDeserializer.java From auto-value-bundle with MIT License | 6 votes |
private String deserializeParameterizedObject(TypeName typeName) { if (typeName instanceof ParameterizedTypeName) { ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName; StringBuilder typeToken = new StringBuilder("<") .append(parameterizedTypeName.rawType.simpleName()); for (TypeName internalTypeName : parameterizedTypeName.typeArguments) { typeToken.append(deserializeParameterizedObject(internalTypeName)) .append(", "); } return typeToken.deleteCharAt(typeToken.length() - 1) .deleteCharAt(typeToken.length() - 1) .append(">") .toString(); } else { return ((ClassName) typeName).simpleName() + ">"; } }
Example #7
Source File: TypeVariablesExtractor.java From litho with Apache License 2.0 | 6 votes |
/** Get the type variables from the given {@link TypeElement}. */ public static List<TypeVariableName> getTypeVariables(TypeElement typeElement) { final List<? extends TypeParameterElement> typeParameters = typeElement.getTypeParameters(); final int typeParameterCount = typeParameters.size(); final List<TypeVariableName> typeVariables = new ArrayList<>(typeParameterCount); for (TypeParameterElement typeParameterElement : typeParameters) { final int boundTypesCount = typeParameterElement.getBounds().size(); final TypeName[] boundsTypeNames = new TypeName[boundTypesCount]; for (int i = 0; i < boundTypesCount; i++) { boundsTypeNames[i] = TypeName.get(typeParameterElement.getBounds().get(i)); } final TypeVariableName typeVariable = TypeVariableName.get(typeParameterElement.getSimpleName().toString(), boundsTypeNames); typeVariables.add(typeVariable); } return typeVariables; }
Example #8
Source File: SQLiteDAOClass.java From HighLite with Apache License 2.0 | 6 votes |
private MethodSpec buildGetSingleByRawQueryMethod() { final String cursorVarName = "cursor"; return MethodSpec.methodBuilder("getSingle") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .returns(getClassNameOfElement()) .addParameter(CONTEXT, "context", Modifier.FINAL) .addParameter(STRING, "rawQueryClause", Modifier.FINAL) .addParameter(ArrayTypeName.of(STRING), "rawQueryArgs", Modifier.FINAL) .addParameter(TypeName.BOOLEAN, "fetchForeignKeys", Modifier.FINAL) .addParameter(TypeName.BOOLEAN, "fetchRelationships", Modifier.FINAL) .addParameter(TypeName.BOOLEAN, "fromCache", Modifier.FINAL) .addStatement("final $T $L = getReadableDatabase($L)" + ".rawQuery(rawQueryClause, rawQueryArgs)", CURSOR, cursorVarName, "context") .beginControlFlow("if (!$L.moveToFirst())", cursorVarName) .addStatement("$L.close()", cursorVarName) .addStatement("return null") .endControlFlow() .addStatement("$T ret = instantiateObject(cursor, context, " + "fetchForeignKeys, fetchRelationships, fromCache)", getClassNameOfElement()) .addStatement("$L.close()", cursorVarName) .addStatement("return ret") .build(); }
Example #9
Source File: JavaWriter.java From lazythreetenbp with Apache License 2.0 | 6 votes |
private FieldSpec regionId(Set<String> allRegionIds) { CodeBlock.Builder builder = CodeBlock.builder() .add("$T.asList(\n$>$>", Arrays.class); Iterator<String> iterator = allRegionIds.iterator(); while (iterator.hasNext()) { builder.add("$S", iterator.next()); if (iterator.hasNext()) { builder.add(",\n"); } } builder.add("$<$<)"); TypeName listType = ParameterizedTypeName.get(List.class, String.class); return FieldSpec.builder(listType, "REGION_IDS", STATIC, FINAL) .initializer(builder.build()) .build(); }
Example #10
Source File: BeanJsonDeserializerCreator.java From gwt-jackson with Apache License 2.0 | 6 votes |
private MethodSpec buildInitDeserializersMethod( Map<PropertyInfo, JDeserializerType> properties ) throws UnableToCompleteException { TypeName resultType = ParameterizedTypeName.get( ClassName.get( SimpleStringMap.class ), ParameterizedTypeName.get( ClassName.get( BeanPropertyDeserializer.class ), typeName( beanInfo.getType() ), DEFAULT_WILDCARD ) ); MethodSpec.Builder builder = MethodSpec.methodBuilder( "initDeserializers" ) .addModifiers( Modifier.PROTECTED ) .addAnnotation( Override.class ) .returns( resultType ) .addStatement( "$T map = $T.createObject().cast()", resultType, SimpleStringMap.class ); for ( Entry<PropertyInfo, JDeserializerType> entry : properties.entrySet() ) { PropertyInfo property = entry.getKey(); JDeserializerType deserializerType = entry.getValue(); builder.addStatement( "map.put($S, $L)", property.getPropertyName(), buildDeserializer( property, property.getType(), deserializerType ) ); } builder.addStatement( "return map" ); return builder.build(); }
Example #11
Source File: GeneratedBuilderClass.java From FastAdapter with MIT License | 6 votes |
private MethodSpec buildSetValuesMethod() { final MethodSpec.Builder lBuilder = MethodSpec.methodBuilder("setValues") .returns(TypeName.VOID) .addModifiers(Modifier.PUBLIC, Modifier.FINAL); for (int i = 0, size = mFields.size(); i < size; i++) { AnnotatedField annotatedField = mFields.get(i); FastAttribute fastAttribute = annotatedField.getFastAttribute(); lBuilder.addCode("putValue(" + fastAttribute.bindViewId() + ", " + annotatedField.name); } return lBuilder.build(); }
Example #12
Source File: JsonProtocolSpec.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Override public CodeBlock responseHandler(IntermediateModel model, OperationModel opModel) { TypeName pojoResponseType = getPojoResponseType(opModel, poetExtensions); String protocolFactory = protocolFactoryLiteral(model, opModel); CodeBlock.Builder builder = CodeBlock.builder(); builder.add("$T operationMetadata = $T.builder()\n" + ".hasStreamingSuccessResponse($L)\n" + ".isPayloadJson($L)\n" + ".build();", JsonOperationMetadata.class, JsonOperationMetadata.class, opModel.hasStreamingOutput(), !opModel.getHasBlobMemberAsPayload()); if (opModel.hasEventStreamOutput()) { responseHandlersForEventStreaming(opModel, pojoResponseType, protocolFactory, builder); } else { builder.add("\n\n$T<$T> responseHandler = $L.createResponseHandler(operationMetadata, $T::builder);", HttpResponseHandler.class, pojoResponseType, protocolFactory, pojoResponseType); } return builder.build(); }
Example #13
Source File: MemberInjectorGenerator.java From toothpick with Apache License 2.0 | 6 votes |
private void emitSuperMemberInjectorFieldIfNeeded(TypeSpec.Builder scopeMemberTypeSpec) { if (superClassThatNeedsInjection != null) { FieldSpec.Builder superMemberInjectorField = FieldSpec.builder( ParameterizedTypeName.get( ClassName.get(MemberInjector.class), TypeName.get(typeUtil.erasure(superClassThatNeedsInjection.asType()))), "superMemberInjector", Modifier.PRIVATE) // TODO use proper typing here .initializer( "new $L__MemberInjector()", getGeneratedFQNClassName(superClassThatNeedsInjection)); scopeMemberTypeSpec.addField(superMemberInjectorField.build()); } }
Example #14
Source File: BundleExtension.java From auto-value-bundle with MIT License | 6 votes |
private static String unbundleElement( Context context, VariableElement bundleElement, VariableElement deserializerElement, String key, ExecutableElement method) { TypeName type = ClassName.get(method.getReturnType()); boolean isEnum = method.getReturnType().getKind().equals(ElementKind.ENUM); String defaultValue = ""; GCMBundle gcmBundle = context.autoValueClass().getAnnotation(GCMBundle.class); if (gcmBundle != null) { return bundlables.parseFromString(bundleElement, deserializerElement, type, key, isEnum); } else { return bundlables.readType(bundleElement, type, key, deserializerElement, isEnum); } }
Example #15
Source File: EnumsProviderClassBuilder.java From aircon with MIT License | 6 votes |
private MethodSpec createRemoteValueGetter(final TypeElement enumClass, final List<VariableElement> consts) { final String methodName = NamingUtils.ENUMS_PROVIDER_REMOTE_VALUE_GETTER_METHOD; final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(getRemoteValueType(consts)); builder.addParameter(TypeName.get(enumClass.asType()), PARAMETER_VALUE, Modifier.FINAL); final CodeBlockBuilder bodyCodeBuilder = new CodeBlockBuilder(); bodyCodeBuilder.beginSwitch(PARAMETER_VALUE); for (VariableElement variableElement : consts) { Object remoteValue = getRemoteValue(variableElement); if (consts.indexOf(variableElement) == consts.size() - 1) { bodyCodeBuilder.addDefaultSwitchCase(null); } bodyCodeBuilder.addSwitchCase(variableElement.getSimpleName(), new CodeBlockBuilder().addReturn(remoteValue) .build()); } bodyCodeBuilder.endSwitch(); return builder.addCode(bodyCodeBuilder.build()) .build(); }
Example #16
Source File: SQLTransformer.java From kripton with Apache License 2.0 | 6 votes |
/** * Get java.util type Transformable * * @param type * the type * @return the util transform */ static SQLTransform getUtilTransform(TypeName type) { String typeName = type.toString(); // Integer.class.getCanonicalName().equals(typeName) if (Date.class.getCanonicalName().equals(typeName)) { return new DateSQLTransform(); } if (Locale.class.getCanonicalName().equals(typeName)) { return new LocaleSQLTransform(); } if (Currency.class.getCanonicalName().equals(typeName)) { return new CurrencySQLTransform(); } if (Calendar.class.getCanonicalName().equals(typeName)) { return new CalendarSQLTransform(); } if (TimeZone.class.getCanonicalName().equals(typeName)) { return new TimeZoneSQLTransform(); } return null; }
Example #17
Source File: ViewMethod.java From Moxy with MIT License | 5 votes |
ViewMethod(ExecutableElement methodElement, TypeElement strategy, String tag) { this.element = methodElement; this.name = methodElement.getSimpleName().toString(); this.strategy = strategy; this.tag = tag; this.parameterSpecs = methodElement.getParameters() .stream() .map(ParameterSpec::get) .collect(Collectors.toList()); this.exceptions = methodElement.getThrownTypes().stream() .map(TypeName::get) .collect(Collectors.toList()); this.typeVariables = methodElement.getTypeParameters() .stream() .map(TypeVariableName::get) .collect(Collectors.toList()); this.argumentsString = parameterSpecs.stream() .map(parameterSpec -> parameterSpec.name) .collect(Collectors.joining(", ")); this.uniqueSuffix = ""; }
Example #18
Source File: IntentBuilderGenerator.java From dart with Apache License 2.0 | 5 votes |
private TypeName getInitialStateGeneric(boolean resolved) { if (resolved) { return get(target.classPackage, builderClassName(), RESOLVED_OPTIONAL_SEQUENCE_CLASS); } final ClassName optionalSequence = get(target.classPackage, builderClassName(), OPTIONAL_SEQUENCE_CLASS); return TypeVariableName.get(OPTIONAL_SEQUENCE_GENERIC, optionalSequence); }
Example #19
Source File: QueryCompilerWriter.java From sqlitemagic with Apache License 2.0 | 5 votes |
public static QueryCompilerWriter create(EntityEnvironment entityEnvironment) { final TypeName tableElementTypeName = entityEnvironment.getTableElementTypeName(); final TableElement tableElement = entityEnvironment.getTableElement(); return builder() .entityEnvironment(entityEnvironment) .tableElement(tableElement) .tableElementTypeName(tableElementTypeName) .tableStructureConstant(tableStructureConstant(tableElement)) .tableType(TABLE) .build(); }
Example #20
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 #21
Source File: ModelBuilderSpecs.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private TypeName builderImplSuperClass() { if (isRequest()) { return new AwsServiceBaseRequestSpec(intermediateModel).className().nestedClass("BuilderImpl"); } if (isResponse()) { return new AwsServiceBaseResponseSpec(intermediateModel).className().nestedClass("BuilderImpl"); } return ClassName.OBJECT; }
Example #22
Source File: FitProcessor.java From fit with Apache License 2.0 | 5 votes |
private MethodSpec createPreferenceGetMethod(TypeName targetType, Set<Element> fieldElements, Set<fit.compiler.PropertyDescriptor> setterPropertyDescriptors) { MethodSpec.Builder result = MethodSpec.methodBuilder("get") .addAnnotation(Override.class) .addModifiers(PUBLIC) .addParameter(CONTEXT, "context") .addParameter(STRING, "name"); result.addStatement( "$T sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)", SHARED_PREFERENCES); result.addStatement("$T obj = new $T()", targetType, targetType); for (Element element : fieldElements) { genGetCode(false, result, element.asType(), element.getSimpleName().toString(), "obj.$N"); } //setter for (fit.compiler.PropertyDescriptor propertyDescriptor : setterPropertyDescriptors) { Element method = propertyDescriptor.getSetter(); TypeMirror typeMirror = ((ExecutableType) method.asType()).getParameterTypes().get(0); genGetCode(true, result, typeMirror, propertyDescriptor.getField().getSimpleName().toString(), "obj." + method.getSimpleName() + "("); } result.addStatement("return obj").returns(targetType); return result.build(); }
Example #23
Source File: SolidityFunctionWrapper.java From web3j with Apache License 2.0 | 5 votes |
static TypeName getEventNativeType(TypeName typeName) { if (typeName instanceof ParameterizedTypeName) { return TypeName.get(byte[].class); } String simpleName = ((ClassName) typeName).simpleName(); if (simpleName.equals(Utf8String.class.getSimpleName())) { return TypeName.get(byte[].class); } else { return getNativeType(typeName); } }
Example #24
Source File: AnnotationCreatorClassPoolVisitor.java From reflection-no-reflection with Apache License 2.0 | 5 votes |
private JavaFile buildAnnotationImpl(Class<? extends Annotation> annotationClass) { String aClassName = annotationClass.getName(); MethodSpec annotationTypeMethod = MethodSpec.methodBuilder("annotationType") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .returns(ClassName.get(java.lang.Class.class)) .addStatement("return $L.class", aClassName) .build(); TypeSpec.Builder annotationImplType = TypeSpec.classBuilder(annotationClass.getSimpleName() + "$$Impl") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addSuperinterface(util.getClassName(annotationClass)) .addMethod(annotationTypeMethod); System.out.println("annotation methods " + annotationClass.getMethods().size()); System.out.println("annotation fields " + annotationClass.getFields().length); System.out.println("annotation " + annotationClass.toString()); for (Method method : annotationClass.getMethods()) { TypeName type; if (method.getReturnType().isArray()) { //important to use the component here as there is no method get(TypeName) //we fail to be detected as an array (see ArrayTypeName.get implementation) type = ArrayTypeName.of(util.getClassName(method.getReturnType().getComponentType())); } else { type = TypeName.get(method.getReturnType()); } FieldSpec field = FieldSpec.builder(type, method.getName(), Modifier.PRIVATE).build(); annotationImplType.addField(field); MethodSpec setterMethod = createSetterMethod(type, method.getName()); annotationImplType.addMethod(setterMethod); MethodSpec getterMethod = createGetterMethod(type, method.getName()); annotationImplType.addMethod(getterMethod); } return JavaFile.builder(targetPackageName, annotationImplType.build()).build(); }
Example #25
Source File: CodeGenerator.java From toothpick with Apache License 2.0 | 5 votes |
protected TypeName getParamType(ParamInjectionTarget paramInjectionTarget) { if (paramInjectionTarget.kind == ParamInjectionTarget.Kind.INSTANCE) { return TypeName.get(typeUtil.erasure(paramInjectionTarget.memberClass.asType())); } else { return ParameterizedTypeName.get( ClassName.get(paramInjectionTarget.memberClass), ClassName.get(typeUtil.erasure(paramInjectionTarget.kindParamClass.asType()))); } }
Example #26
Source File: StringBindTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateParseOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { XmlType xmlType = property.xmlInfo.xmlType; if (property.hasTypeAdapter()) { // there's an type adapter methodBuilder.addCode("// using type adapter $L\n", property.typeAdapter.adapterClazz); switch (xmlType) { case ATTRIBUTE: methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA+"$T.unescapeXml($L.getAttributeValue(attributeIndex))"+POST_TYPE_ADAPTER), TypeAdapterUtils.class, typeName(property.typeAdapter.adapterClazz), StringEscapeUtils.class, parserName); break; case TAG: methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA+"$T.unescapeXml($L.getElementText())"+POST_TYPE_ADAPTER), TypeAdapterUtils.class, typeName(property.typeAdapter.adapterClazz), StringEscapeUtils.class, parserName); break; case VALUE: case VALUE_CDATA: methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA+"$T.unescapeXml($L.getText())"+POST_TYPE_ADAPTER), TypeAdapterUtils.class, typeName(property.typeAdapter.adapterClazz), StringEscapeUtils.class, parserName); break; } } else { // there's no type adapter switch (xmlType) { case ATTRIBUTE: methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.unescapeXml($L.getAttributeValue(attributeIndex))"), StringEscapeUtils.class, parserName); break; case TAG: methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.unescapeXml($L.getElementText())"), StringEscapeUtils.class, parserName); break; case VALUE: case VALUE_CDATA: methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.unescapeXml($L.getText())"), StringEscapeUtils.class, parserName); break; } } }
Example #27
Source File: TypeProvider.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private TypeName fieldType(MemberModel memberModel, boolean preserveEnumType) { if (memberModel.isSimple()) { boolean isEnumMember = memberModel.getEnumType() != null; return preserveEnumType && isEnumMember ? poetExtensions.getModelClass(memberModel.getEnumType()) : getTypeNameForSimpleType(memberModel.getVariable().getVariableType()); } else if (memberModel.isList()) { TypeName elementType = fieldType(memberModel.getListModel().getListMemberModel(), preserveEnumType); return ParameterizedTypeName.get(ClassName.get(List.class), elementType); } else if (memberModel.isMap()) { TypeName keyType = fieldType(memberModel.getMapModel().getKeyModel(), preserveEnumType); TypeName valueType = fieldType(memberModel.getMapModel().getValueModel(), preserveEnumType); return ParameterizedTypeName.get(ClassName.get(Map.class), keyType, valueType); } return poetExtensions.getModelClass(memberModel.getC2jShape()); }
Example #28
Source File: JsonNumberTest.java From json2java4idea with Apache License 2.0 | 5 votes |
@Test public void getType() throws Exception { // exercise final TypeName actual = underTest.getType(); // verify assertThat(actual) .isEqualTo(TypeName.INT); }
Example #29
Source File: Match1MethodPermutationBuilder.java From motif with Apache License 2.0 | 5 votes |
private List<MethodSpec> getMethodPermutations( TypeName inputType, List<TypeNameWithArity> paramTypesA, String methodName) { return paramTypesA.stream() .filter(a -> a.arity <= maxArity) .map( a -> { MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .addJavadoc(summaryJavadoc) .addJavadoc(getJavadoc(a)) .returns(getReturnType(inputType, a)) .addTypeVariables(getTypeVariables(inputType, a)); if (nonMatchParams != null && !nonMatchParams.isEmpty()) { nonMatchParams.stream().forEach( p -> methodBuilder.addParameter(p.type, p.name)); } return methodBuilder.addParameter(a.typeName, paramA.name) .addStatement(getMatcherStatement(a), getMatcherStatementArgs(1)) .addStatement( getReturnStatement(a), getReturnStatementArgs(inputType, a)) .build(); } ) .collect(Collectors.toList()); }
Example #30
Source File: OutputValue.java From dataenum with Apache License 2.0 | 5 votes |
public TypeName parameterizedOutputClass() { if (!hasTypeVariables()) { return outputClass(); } TypeName[] typeNames = Iterables.toArray(typeVariables(), TypeVariableName.class); return ParameterizedTypeName.get(outputClass(), typeNames); }