當前位置: 首頁>>代碼示例>>Java>>正文


Java FieldSpec類代碼示例

本文整理匯總了Java中com.squareup.javapoet.FieldSpec的典型用法代碼示例。如果您正苦於以下問題:Java FieldSpec類的具體用法?Java FieldSpec怎麽用?Java FieldSpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FieldSpec類屬於com.squareup.javapoet包,在下文中一共展示了FieldSpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: builderBuild

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
public static MethodSpec builderBuild(ClassName className, List<MethodSpec> methodSpecList, List<FieldSpec> fieldSpecList) {
    String name = className.simpleName();
    CodeBlock.Builder codeBlock = CodeBlock.builder();

    for (int i = 0; i < methodSpecList.size(); i++) {
        codeBlock.add("$N.$N(this.$N);\n", name.toLowerCase(), methodSpecList.get(i).name, fieldSpecList.get(i).name);
    }

    return MethodSpec
            .methodBuilder("build")
            .addModifiers(Modifier.PUBLIC)
            .addStatement("$N $N = new $N()", name, name.toLowerCase(), name)
            .addCode(codeBlock.build())
            .returns(className)
            .addStatement("return $N", name.toLowerCase())
            .build();
}
 
開發者ID:daisuke-nomura,項目名稱:KiiAnnotation,代碼行數:18,代碼來源:BuilderGenerator.java

示例2: build

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
public TypeSpec.Builder build() {
    TypeSpec.Builder tsb = TypeSpec.classBuilder(name);
    for (Map.Entry<String, Class<?>> entry : nameTypeMap.entrySet()) {
        FieldSpec field = FieldSpec.builder(
                ClassName.get(entry.getValue()), entry.getKey(), Modifier.PRIVATE)
                .build();
        tsb.addField(field);
    }
    if (getter) {
        tsb = GetterSpec.forType(tsb).build();
    }
    if (setter) {
        tsb = SetterSpec.forType(tsb).build();
    }
    if (builder) {
        tsb = BuilderSpec.forType(packageName, tsb).build();
    }
    return tsb;
}
 
開發者ID:harmanpa,項目名稱:javabard,代碼行數:20,代碼來源:ValueClassSpec.java

示例3: addUsedTypeConverterMethods

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private void addUsedTypeConverterMethods(TypeSpec.Builder builder) {
    Set<TypeName> usedTypeConverters = new HashSet<>();

    for (JsonFieldHolder holder : mJsonObjectHolder.fieldMap.values()) {
        usedTypeConverters.addAll(holder.type.getUsedTypeConverters());
    }

    for (TypeName usedTypeConverter : usedTypeConverters) {
        final String variableName = getTypeConverterVariableName(usedTypeConverter);
        builder.addField(FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(TypeConverter.class), usedTypeConverter), variableName)
                .addModifiers(Modifier.PRIVATE, Modifier.STATIC)
                .build()
        );

        builder.addMethod(MethodSpec.methodBuilder(getTypeConverterGetter(usedTypeConverter))
                .addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
                .returns(ParameterizedTypeName.get(ClassName.get(TypeConverter.class), usedTypeConverter))
                .beginControlFlow("if ($L == null)", variableName)
                .addStatement("$L = $T.typeConverterFor($T.class)", variableName, LoganSquare.class, usedTypeConverter)
                .endControlFlow()
                .addStatement("return $L", variableName)
                .build()
        );
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:ObjectMapperInjector.java

示例4: generateGlobalConstant

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private static JavaFile generateGlobalConstant(String packageName, Constant[] globalConstants) {
    TypeSpec.Builder javaClass = TypeSpec.classBuilder(CONSTANT_CLASS_NAME);
    javaClass.addModifiers(PUBLIC, FINAL);
    for (Constant constant : globalConstants) {
        String value = constant.value();
        if (constant.type().equals("Task") && value != null) {
            value = "greycat.Tasks.newTask().parse(\"" + value.replaceAll("\"", "'").trim() + "\",null);";
        } else if (constant.type().equals("String") && value != null) {
            value = "\""+value+"\"";
        }
        FieldSpec.Builder field = FieldSpec.builder(clazz(constant.type()), constant.name())
                .addModifiers(PUBLIC, STATIC);
        if (value != null) {
            field.addModifiers(FINAL).initializer(value);
        }
        javaClass.addField(field.build());
    }
    return JavaFile.builder(packageName, javaClass.build()).build();
}
 
開發者ID:datathings,項目名稱:greycat,代碼行數:20,代碼來源:ConstantGenerator.java

示例5: getTypeAdapters

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(List<JsonProperty> properties,
    NameAllocator nameAllocator) {
  Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
  for (JsonProperty property : properties) {
    if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
      ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
      String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
      name = nameAllocator.newName(name, typeName);

      typeAdapters.put(property.typeAdapter,
          FieldSpec.builder(typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC,
              FINAL).initializer("new $T()", typeName).build());
    }
  }

  return ImmutableMap.copyOf(typeAdapters);
}
 
開發者ID:setheclark,項目名稱:auto-value-json,代碼行數:18,代碼來源:AutoValueJsonExtension.java

示例6: process

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
    parser.scan(roundEnvironment);
    TypeSpec.Builder builder = TypeSpec.classBuilder("Testing")
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    for (Element element : roundEnvironment.getElementsAnnotatedWith(Parse.class))
        builder.addField(FieldSpec.builder(String.class, element.getSimpleName().toString())
                .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
                .initializer("$S", parser.parse("com.example.rparser", element.getAnnotation(Parse.class).value()))
                .build());
    JavaFile javaFile = JavaFile.builder("com.example.rparser", builder.build())
            .build();
    try {
        javaFile.writeTo(filer);
    } catch (IOException ignored) {
    }
    return false;
}
 
開發者ID:hendraanggrian,項目名稱:r-parser,代碼行數:19,代碼來源:ExampleProcessor.java

示例7: buildFields

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
@Nonnull
@Override
protected List<FieldSpec> buildFields() {
    return getProperties().entrySet()
            .stream()
            .map(property -> {
                final String name = property.getKey();
                final TypeName type = property.getValue();

                final String fieldName = fieldNamePolicy.convert(name, type);
                return FieldSpec.builder(type, fieldName)
                        .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
                        .build();
            })
            .collect(Collectors.toList());
}
 
開發者ID:t28hub,項目名稱:json2java4idea,代碼行數:17,代碼來源:JacksonClassBuilder.java

示例8: buildFields

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
@Nonnull
@Override
protected List<FieldSpec> buildFields() {
    return getProperties()
            .entrySet()
            .stream()
            .map(property -> {
                final String name = property.getKey();
                final TypeName type = property.getValue();
                final String fieldName = fieldNamePolicy.convert(name, type);
                return FieldSpec.builder(type, fieldName)
                        .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
                        .build();
            })
            .collect(Collectors.toList());
}
 
開發者ID:t28hub,項目名稱:json2java4idea,代碼行數:17,代碼來源:ModelClassBuilder.java

示例9: getTypeAdapters

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(ImmutableList<Property> properties) {
    Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
    NameAllocator nameAllocator = new NameAllocator();
    nameAllocator.newName("CREATOR");
    for (Property property : properties) {
        if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
            ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
            String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
            name = nameAllocator.newName(name, typeName);

            typeAdapters.put(property.typeAdapter, FieldSpec.builder(
                    typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC, FINAL)
                    .initializer("new $T()", typeName).build());
        }
    }
    return ImmutableMap.copyOf(typeAdapters);
}
 
開發者ID:foodora,項目名稱:android-auto-mapper,代碼行數:18,代碼來源:AutoMappperProcessor.java

示例10: builderGroupBuild

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
public static MethodSpec builderGroupBuild(ClassName className, List<MethodSpec> methodSpecList, List<FieldSpec> fieldSpecList) {
    String name = className.simpleName();
    CodeBlock.Builder codeBlock = CodeBlock.builder();

    for (int i = 0; i < methodSpecList.size(); i++) {
        codeBlock.add("$N.$N(this.$N);\n", name.toLowerCase(), methodSpecList.get(i).name, fieldSpecList.get(i).name);
    }

    return MethodSpec
            .methodBuilder("build")
            .addModifiers(Modifier.PUBLIC)
            .addStatement("$N $N = new $N($N)", name, name.toLowerCase(), name, "this.kiiGroup")
            .addCode(codeBlock.build())
            .returns(className)
            .addStatement("return $N", name.toLowerCase())
            .build();
}
 
開發者ID:daisuke-nomura,項目名稱:KiiAnnotation,代碼行數:18,代碼來源:BuilderGenerator.java

示例11: checkCompiles

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private void checkCompiles(final TypeSpec anonymousTypeSpec) {
	// Anonymous class cannot be top level class, so nest the anonymous class as a field
	final TypeSpec wrapperTypeSpec = TypeSpec
			.classBuilder("Wrapper")
			.addField(FieldSpec.builder(ClassName.get(Data.class), "target").build())
			.addField(FieldSpec.builder(AndroidClassNames.CONTEXT, "context").build())
			.addField(FieldSpec.builder(AndroidClassNames.TYPED_ARRAY, "attrs").build())
			.addField(FieldSpec
					.builder(TypeName.OBJECT, "o")
					.initializer("$L", anonymousTypeSpec)
					.build())
			.build();

	final JavaFile wrapperJavaFile = JavaFile
			.builder("", wrapperTypeSpec)
			.build();

	final Set<JavaFile> filesToCompile = new HashSet<>();

	filesToCompile.add(wrapperJavaFile);
	filesToCompile.add(CallerDef.SRC_FILE);

	CompileChecker.checkCompiles(filesToCompile);
}
 
開發者ID:MatthewTamlin,項目名稱:Spyglass,代碼行數:25,代碼來源:TestCallerGenerator.java

示例12: createGlobalInterface

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private TypeSpec createGlobalInterface() {
    return TypeSpec.classBuilder("Global")
            .addModifiers(Modifier.STATIC, Modifier.FINAL)
            .addAnnotation(AnnotationSpec.builder(ClassNames.JSINTEROP_JSTYPE)
                    .addMember("isNative", "true")
                    .addMember("namespace", "$T.GLOBAL", ClassNames.JSINTEROP_JSPACKAGE)
                    .build())
            .addFields(Stream.of(Object.class,
                    Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE,
                    Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE)
                    .map(type -> FieldSpec.builder(type, "UNDEFINED_" + type.getSimpleName().toUpperCase())
                            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                            .addAnnotation(AnnotationSpec.builder(ClassNames.JSINTEROP_JSPROPERTY)
                                    .addMember("namespace", "$T.GLOBAL", ClassNames.JSINTEROP_JSPACKAGE)
                                    .addMember("name", "$S", "undefined")
                                    .build())
                            .build())
                .collect(Collectors.toList()))
            .build();
}
 
開發者ID:codegen-io,項目名稱:jso-builder,代碼行數:21,代碼來源:JSOBuilderProcessor.java

示例13: generateMappingTable

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
public void generateMappingTable(Filer filer) throws IOException {
    if (routerTable.isEmpty()) {
        return;
    }
    FieldSpec tableFieldSpec = FieldSpec.builder(Map.class, "routerTable")
            .addModifiers(Modifier.PRIVATE)
            .build();
    MethodSpec initTableMethodSpec = getInitTableMethodSpec();
    MethodSpec queryTableMethodSpec = getqueryTableMethodSpec();
    MethodSpec constructorMethodSpec = getconstructorMethodSpec(tableFieldSpec, initTableMethodSpec);
    MethodSpec addRouterMethodSpec = getAddRouterMethodSpec();
    TypeSpec routerTableClassTypeSpec = getRouterTableClassTypeSpec(tableFieldSpec, initTableMethodSpec, queryTableMethodSpec, constructorMethodSpec, addRouterMethodSpec);
    String packageName = Constants.GENERATED_PACKAGE_NAME;
    JavaFile javaFile = JavaFile.builder(packageName, routerTableClassTypeSpec).build();
    javaFile.writeTo(filer);
}
 
開發者ID:bboylin,項目名稱:D-Router,代碼行數:17,代碼來源:RouterTableGenerator.java

示例14: writeValueWithTypeAdapter

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
public static CodeBlock writeValueWithTypeAdapter(FieldSpec adapter, AutoMappperProcessor.Property p, ParameterSpec out) {
    CodeBlock.Builder block = CodeBlock.builder();

    if (p.isNullable()) {
        block.beginControlFlow("if ($N == null)", p.fieldName);
        block.addStatement("$N.writeInt(1)", out);
        block.nextControlFlow("else");
        block.addStatement("$N.writeInt(0)", out);
    }

    block.addStatement("$N.toParcel($N, $N)", adapter, p.fieldName, out);

    if (p.isNullable()) {
        block.endControlFlow();
    }

    return block.build();
}
 
開發者ID:foodora,項目名稱:android-auto-mapper,代碼行數:19,代碼來源:Parcelables.java

示例15: addFields

import com.squareup.javapoet.FieldSpec; //導入依賴的package包/類
private void addFields(final TypeSpec.Builder classBuilder) {
    FieldSpec.Builder viewIdsField = FieldSpec.builder(int[].class, "viewIds", Modifier.PRIVATE, Modifier.STATIC,
            Modifier.FINAL);
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    for (Integer id : viewIds) {
        sb.append(id).append(",");
    }

    if (viewIds.size() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }

    sb.append("}");
    viewIdsField.initializer(CodeBlock.of(sb.toString()));
    classBuilder.addField(viewIdsField.build());

}
 
開發者ID:renard314,項目名稱:auto-adapter,代碼行數:19,代碼來源:ViewHolderFactoryGenerator.java


注:本文中的com.squareup.javapoet.FieldSpec類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。