本文整理汇总了Java中com.squareup.javapoet.JavaFile类的典型用法代码示例。如果您正苦于以下问题:Java JavaFile类的具体用法?Java JavaFile怎么用?Java JavaFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavaFile类属于com.squareup.javapoet包,在下文中一共展示了JavaFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import com.squareup.javapoet.JavaFile; //导入依赖的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;
}
示例2: generateClass
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private void generateClass(TypeElement typeElement, List<VariableElement> listOfVariables) {
final ClassName pojoType = ClassName.get(typeElement);
TypeSpec.Builder classBuilder = TypeSpec.
classBuilder(typeElement.getSimpleName() + "Converter")
.addModifiers(Modifier.PUBLIC);
classBuilder.addMethod(generateSingleRowParseMethod(typeElement, listOfVariables, "toSingleRow", Modifier.PUBLIC));
classBuilder.addMethod(generateSingleRowParseMethod(typeElement, listOfVariables, "parseToSingleRow", Modifier.PUBLIC, Modifier.STATIC));
classBuilder.addMethod(generateListParseMethod(pojoType, "toList", "toSingleRow", Modifier.PUBLIC));
classBuilder.addMethod(generateListParseMethod(pojoType, "parseToList", "parseToSingleRow", Modifier.PUBLIC, Modifier.STATIC));
classBuilder.addMethod(generateToContentValuesMethod(typeElement, listOfVariables, "toContentValues", Modifier.PUBLIC));
classBuilder.addMethod(generateToContentValuesMethod(typeElement, listOfVariables, "parseToContentValues", Modifier.PUBLIC, Modifier.STATIC));
try {
JavaFile.builder(pojoType.packageName(), classBuilder.build())
.build()
.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例3: process
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
@Override public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
Map<TypeElement, BindingSet> bindingMap = findAndParseTargets(env);
for (Map.Entry<TypeElement, BindingSet> entry : bindingMap.entrySet()) {
TypeElement typeElement = entry.getKey();
BindingSet binding = entry.getValue();
JavaFile javaFile = binding.brewJava(sdk);
try {
javaFile.writeTo(filer);
} catch (IOException e) {
error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage());
}
}
return false;
}
示例4: generate
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
public JavaFile generate(List<RegistrationAnnotatedClass> annotatedClasses) throws ProcessingException {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC);
for (RegistrationAnnotatedClass annotatedClass : annotatedClasses) {
ClassName annotatedClassName = ClassName.get(annotatedClass.getClassElement());
ClassName screenClassName = ClassName.bestGuess(annotatedClass.getScreenClassName());
if (annotatedClass.getScreenResultClassName() == null) {
String registrationMethod = getRegistrationMethod(annotatedClass.getScreenType());
constructorBuilder.addStatement(registrationMethod, screenClassName, annotatedClassName);
} else {
String registrationForResultMethod = getRegistrationForResultMethod(annotatedClass.getScreenType());
ClassName screenResultClassName = ClassName.bestGuess(annotatedClass.getScreenResultClassName());
constructorBuilder.addStatement(registrationForResultMethod, screenClassName, annotatedClassName, screenResultClassName);
}
}
TypeSpec navigationFactory = TypeSpec.classBuilder(CLASS_NAME)
.addModifiers(Modifier.PUBLIC)
.superclass(ClassName.get(PACKAGE, SUPERCLASS_NAME))
.addMethod(constructorBuilder.build())
.build();
return JavaFile.builder(PACKAGE, navigationFactory).build();
}
示例5: constructActivityModule
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private void constructActivityModule() {
final TypeSpec.Builder builder = TypeSpec.classBuilder(Constants.ACTIVITY_MODULE)
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addAnnotation(Constants.DAGGER_MODULE);
for (ActivityHolder activityHolder : activityHolders.values()) {
builder.addMethod(MethodSpec.methodBuilder(Constants.METHOD_CONTRIBUTE + activityHolder.className)
.addAnnotation(Constants.DAGGER_ANDROID_ANNOTATION)
.addModifiers(Modifier.ABSTRACT)
.returns(activityHolder.classNameComplete)
.build()
);
}
final TypeSpec newClass = builder.build();
final JavaFile javaFile = JavaFile.builder(Constants.PACKAGE_NAME, newClass).build();
try {
javaFile.writeTo(System.out);
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例6: construct
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
public void construct(JsoupModelHolder modelHolder) {
final TypeSpec.Builder builder = TypeSpec.classBuilder(modelHolder.className + Constants.PARSER)
.addModifiers(Modifier.PUBLIC);
for (JsoupModelFieldHolder field : modelHolder.fields) {
builder.addMethod(MethodSpec.methodBuilder(field.name.replace("-","_").replace(" ","_"))
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.VOID)
.addParameter(modelHolder.classNameComplete, "item")
.addParameter(TypeName.get(String.class), "value")
.addStatement("item.$L = value", field.name)
.build());
}
final TypeSpec newClass = builder.build();
final JavaFile javaFile = JavaFile.builder(modelHolder.classNameComplete.packageName(), newClass).build();
try {
javaFile.writeTo(System.out);
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: generate
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
public static void generate(Domain ontology, Path outputDirectory) throws IOException {
JavaFile tags = TagGenerator.tag(ontology);
JavaFile marker = MarkerGenerator.marker(ontology);
JavaFile builder = MarkerGenerator.markerBuilder(ontology);
JavaFile statement = LoggerGenerator.statement(ontology);
JavaFile logger = LoggerGenerator.logger(ontology);
JavaFile mdc = MdcGenerator.statement(ontology);
Files.createDirectories(outputDirectory);
for(JavaFile file : new JavaFile[]{tags, marker, builder, statement, logger, mdc}) {
if(file != null) {
if(changed(file, outputDirectory)) {
file.writeTo(outputDirectory);
} else {
// do not write this file
}
}
}
}
示例8: logger
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
public static JavaFile logger(Domain ontology) {
ClassName name = ClassName.get(ontology.getTargetPackage(), ontology.getName() + LOGGER);
ClassName statementName = ClassName.get(ontology.getTargetPackage(), ontology.getName() + STATEMENT);
ParameterSpec loggerParameter = ParameterSpec.builder(org.slf4j.Logger.class, "logger").build();
return JavaFile.builder(name.packageName(), TypeSpec.classBuilder(name)
.superclass(ParameterizedTypeName.get(ClassName.get(AbstractDomainLogger.class), statementName))
.addJavadoc(composeJavadoc(ontology, name))
.addModifiers(Modifier.PUBLIC)
.addMethod(MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(loggerParameter)
.addStatement("super($N)", loggerParameter)
.build())
.addMethod(getCreateLogStatementMethod(statementName)).build()).build();
}
示例9: writeBinding
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private void writeBinding(String targetClassName, Set<Element> annotationFields)
throws IOException {
String packageName = null;
int lastDot = targetClassName.lastIndexOf('.');
if (lastDot > 0) {
packageName = targetClassName.substring(0, lastDot);
}
String targetSimpleClassName = targetClassName.substring(lastDot + 1);
String bindingClassName = targetClassName + "_PrefBinding";
String bindingSimpleClassName = bindingClassName.substring(lastDot + 1);
ClassName targetClass = ClassName.get(packageName, targetSimpleClassName);
TypeSpec binding = TypeSpec.classBuilder(bindingSimpleClassName)
.addModifiers(Modifier.PUBLIC)
.addMethod(buildConstructor(targetClass, annotationFields))
.build();
JavaFile javaFile = JavaFile.builder(packageName, binding).build();
javaFile.writeTo(processingEnv.getFiler());
}
示例10: writeConsumer
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private static ClassName writeConsumer(int count) throws IOException {
String packageName = "info.dourok.esactivity.function";
MethodSpec.Builder method = MethodSpec.methodBuilder("accept")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
TypeSpec.Builder type = TypeSpec.interfaceBuilder("Consumer" + count)
.addModifiers(Modifier.PUBLIC);
for (int i = 0; i < count; i++) {
type.addTypeVariable(TypeVariableName.get("T" + i));
method.addParameter(
TypeVariableName.get("T" + i), "t" + i);
}
type.addMethod(method.build());
JavaFile.builder(packageName, type.build())
.build()
.writeTo(EasyUtils.getFiler());
return ClassName.get(packageName, "Consumer" + count);
}
示例11: writeInto
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
public void writeInto(final Filer pFiler, final Messager pMessager) {
// prepare generated class
final GeneratedClass lGeneratedClass = new GeneratedClass(mEnclosingClass, mMethods);
final TypeSpec lTypeSpecGeneratedClass = lGeneratedClass.buildTypeSpec();
final String lPackageName = lGeneratedClass.packageName();
// create generated class to a file
try {
JavaFile.builder(lPackageName, lTypeSpecGeneratedClass)
.build()
.writeTo(pFiler);
} catch (IOException pE) {
logError(pMessager, mEnclosingClass, "error while writing generated class");
}
}
示例12: process
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
Map<TypeElement, StitchGenerator> stitchGeneratorMap = processAnnotations(env);
for (Entry<TypeElement, StitchGenerator> typeElementCodeGeneratorEntry : stitchGeneratorMap
.entrySet()) {
TypeElement typeElement = typeElementCodeGeneratorEntry.getKey();
StitchGenerator stitchGenerator = typeElementCodeGeneratorEntry.getValue();
JavaFile javaFile = stitchGenerator.makeFile();
try {
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
mMessager.printMessage(Kind.ERROR, String
.format("Unable to write binding for type %s: %s",
typeElement.getSimpleName(),
e.getMessage()));
}
}
return false;
}
示例13: innerGenerateRouterInit
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private void innerGenerateRouterInit(MethodSpec.Builder methodBuilder)
{
TypeSpec routerInit = TypeSpec.classBuilder(ROUTER_INIT_CLASS_NAME)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(ClassName.get(ROUTER_API_PACKAGE_NAME, ROUTER_INIT_INTERFACE_NAME))
.addMethod(methodBuilder.build())
.build();
try
{
JavaFile.builder(ROUTER_API_PACKAGE_NAME, routerInit)
.build()
.writeTo(mFiler);
}
catch (Exception e)
{
error("Failed to generate file %s", routerInit.name);
}
}
示例14: process
import com.squareup.javapoet.JavaFile; //导入依赖的package包/类
private JavaFile process(EnrichedTypeElement element) throws AbortProcessingException {
TypeSpec.Builder builder = TypeSpec.classBuilder(element.getClassName(DECORATOR_SUFFIX))
.addModifiers(element.getAccessModifier())
.addTypeVariables(element.getTypeVariables())
.addField(createDelegateFieldSpec(element))
.addMethod(createConstructorMethod(element))
.addMethods(createOverridingMethods(element));
if (element.isInterface()) {
builder.addSuperinterface(element.getTypeName());
} else if (element.isAbstract()) {
builder.superclass(element.getTypeName());
} else {
throw new AbortProcessingException("Cannot generate implementation for final class");
}
return JavaFile.builder(element.getPackageName(), builder.build())
.build();
}
示例15: generateMappingTable
import com.squareup.javapoet.JavaFile; //导入依赖的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);
}