本文整理汇总了Java中com.squareup.javapoet.JavaFile.writeTo方法的典型用法代码示例。如果您正苦于以下问题:Java JavaFile.writeTo方法的具体用法?Java JavaFile.writeTo怎么用?Java JavaFile.writeTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.javapoet.JavaFile
的用法示例。
在下文中一共展示了JavaFile.writeTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) {
if (typeElements.size() == 0) {
return true;
}
for (TypeElement typeElement : typeElements) {
for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) {
T entity = element.getAnnotation(targetAnnotation());
SourceGenerator generator = createGenerator(element, entity, roundEnv);
Filer filer = super.processingEnv.getFiler();
try {
List<JavaFile> javaFile = generator.createJavaFile(element);
for (JavaFile file : javaFile) {
file.writeTo(filer);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
示例2: 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();
}
}
示例3: 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;
}
示例4: 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
}
}
}
}
示例5: 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());
}
示例6: generateSharedProperties
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
public static boolean generateSharedProperties(Collection<FieldData> fields,
Elements elements, Filer filer, ProcessorPrinter pp){
final ClassName cn_sp = ClassName.get(PKG_DM_INTERNAL, SIMPLE_NAME_SHARED_PROP);
CodeBlock.Builder staticBuilder = CodeBlock.builder();
for(FieldData fd : fields){
staticBuilder.add("$T.putToCache($S, $S, $L);\n", cn_sp, fd.getTypeCompat().toString(),
fd.getPropertyName(), fd.getComplexType());
}
String classSimpleName = SIMPLE_NAME_SHARED_PROP + "_" + findBestIndex(elements);
TypeSpec typeSpec = TypeSpec.classBuilder(classSimpleName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStaticBlock(staticBuilder.build())
.addJavadoc(CodeBlock.of(DOC))
.build();
try {
JavaFile javaFile = JavaFile.builder(PKG_DM_INTERNAL, typeSpec)
.build();
// System.out.println(javaFile.toString());
javaFile.writeTo(filer);
} catch (IOException e) {
pp.error(TAG, "generateSharedProperties", Util.toString(e));
return false;
}
return true;
}
示例7: generateCode
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
private void generateCode(ClassInfo classInfo, List<PropertyInfo> properties)
throws IOException, ReactPropertyException {
MethodSpec getMethods = MethodSpec.methodBuilder("getProperties")
.addModifiers(PUBLIC)
.addAnnotation(Override.class)
.addParameter(PROPERTY_MAP_TYPE, "props")
.returns(TypeName.VOID)
.addCode(generateGetProperties(properties))
.build();
TypeName superType = getSuperType(classInfo);
ClassName className = classInfo.mClassName;
String holderClassName =
getClassName((TypeElement) classInfo.mElement, className.packageName()) + "$$PropsSetter";
TypeSpec holderClass = TypeSpec.classBuilder(holderClassName)
.addSuperinterface(superType)
.addModifiers(PUBLIC)
.addMethod(generateSetPropertySpec(classInfo, properties))
.addMethod(getMethods)
.build();
JavaFile javaFile = JavaFile.builder(className.packageName(), holderClass)
.addFileComment("Generated by " + getClass().getName())
.build();
javaFile.writeTo(mFiler);
}
示例8: generateJava
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
private void generateJava(String packageName, String pluginName, File target) {
List<JavaFile> collector = new ArrayList<JavaFile>();
TypeGenerator.generate(packageName, model, collector);
IndexGenerator.generate(packageName, model, collector);
ConstantGenerator.generate(packageName, model, collector);
PluginGenerator.generate(packageName, pluginName, model, collector);
for (JavaFile file : collector) {
try {
file.writeTo(target);
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例9: generateClass
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
/**
* Generates the code for the Barricade configuration based on the annotations found.
*
* @param processingEnv Processing environment
* @param configs Configuration detected by annotation processing
* @param messager Messager to print logs
* @throws IOException
*/
static void generateClass(ProcessingEnvironment processingEnv,
HashMap<String, BarricadeResponseSet> configs, Messager messager) throws IOException {
messager.printMessage(Diagnostic.Kind.NOTE, "Generating configuration code...");
TypeSpec.Builder classBuilder = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL);
FieldSpec valuesField = FieldSpec.builder(TYPE_CONFIG, "configs").addModifiers(PRIVATE).build();
FieldSpec instanceField =
FieldSpec.builder(ClassName.get(PACKAGE_NAME, CLASS_NAME), "barricadeConfig")
.addModifiers(PRIVATE, STATIC)
.build();
MethodSpec.Builder instanceMethodBuilder = generateGetInstanceMethodBuilder();
MethodSpec.Builder constructorMethodBuilder = generateConstructorBuilder(configs, messager);
MethodSpec.Builder valuesMethod = generateGetConfigsMethodBuilder();
MethodSpec.Builder getResponseMethodBuilder = generateGetResponseMethodBuilder();
classBuilder.addType(generateEndpointsInnerClass(configs.keySet()));
classBuilder.addType(generateResponsesInnerClass(configs));
classBuilder.addField(instanceField);
classBuilder.addField(valuesField);
classBuilder.addMethod(instanceMethodBuilder.build());
classBuilder.addMethod(constructorMethodBuilder.build());
classBuilder.addMethod(valuesMethod.build());
classBuilder.addMethod(getResponseMethodBuilder.build());
classBuilder.addSuperinterface(IBarricadeConfig.class);
JavaFile.Builder javaFileBuilder = builder(PACKAGE_NAME, classBuilder.build());
JavaFile javaFile = javaFileBuilder.build();
javaFile.writeTo(processingEnv.getFiler());
messager.printMessage(Diagnostic.Kind.NOTE, "Code generation complete!");
}
示例10: writeRxObserve
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
private void writeRxObserve() {
final TypeSpec.Builder builder = TypeSpec.classBuilder(Constants.CLASS)
.addModifiers(Modifier.PUBLIC);
for (ClassName className : observeHolders.keySet()) {
final ObserveHolder observeHolder = observeHolders.get(className);
final String simpleName = className.simpleName();
final TypeName returnType = ClassName.bestGuess(className.packageName() + "." + simpleName + Constants.OBSERVE_CLASS);
if (processUtils.allMethodsAreStatic(observeHolder.methods)) {
builder.addMethod(MethodSpec.methodBuilder(Constants.METHOD_OF + simpleName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(returnType)
.addStatement("return new $T()", returnType)
.build());
} else {
builder.addMethod(MethodSpec.methodBuilder(Constants.METHOD_OF)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(className, Constants.TARGET)
.returns(returnType)
.addStatement("return new $T($L)", returnType, Constants.TARGET)
.build());
}
}
final TypeSpec newClass = builder.build();
final JavaFile javaFile = JavaFile.builder(Constants.PACKAGE, newClass).build();
try {
javaFile.writeTo(System.out);
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例11: process
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
List<JavaFile> files = findAndParseTargets(env);
for (JavaFile javaFile : files) {
try {
javaFile.writeTo(filer);
} catch (IOException e) {
error("Unable to write same name %s: %s", javaFile.packageName, e.getMessage());
}
}
return false;
}
示例12: createJavaFile
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
private void createJavaFile(String packageName, TypeSpec typeSpec) {
try {
JavaFile javaFile = JavaFile.builder(packageName, typeSpec)
.build();
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException ex) {
processingEnv.getMessager()
.printMessage(Diagnostic.Kind.ERROR, "It was not possible to generate java files due to an error: \n" + ex.getMessage());
}
}
示例13: writeSource
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
void writeSource(JavaFile file) {
try {
file.writeTo(env.getFiler());
} catch (IOException e) {
env.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
}
}
示例14: process
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<TypeElement> factories = roundEnv.getElementsAnnotatedWith(InspectorFactory.class)
.stream()
.map((Function<Element, TypeElement>) element -> (TypeElement) element)
.collect(Collectors.toSet());
for (TypeElement factory : factories) {
if (!implementsValidatorFactory(factory)) {
error(factory, "Must implement Validator.Factory!");
}
List<TypeElement> validationTargets =
getTargetClasses(factory.getAnnotation(InspectorFactory.class)).flatMap(targetClass ->
roundEnv.getElementsAnnotatedWith(
targetClass)
.stream())
.map((Function<Element, TypeElement>) element -> {
if (!(element instanceof TypeElement)) {
throw new UnsupportedOperationException(
"InspectorFactories can only operate on annotated types.");
}
return (TypeElement) element;
})
.collect(Collectors.toList());
String adapterName = classNameOf(factory);
String packageName = packageNameOf(factory);
TypeSpec validatorFactory =
createValidatorFactory(validationTargets, packageName, adapterName);
JavaFile file = JavaFile.builder(packageName, validatorFactory)
.build();
try {
file.writeTo(processingEnv.getFiler());
} catch (IOException e) {
processingEnv.getMessager()
.printMessage(ERROR, "Failed to write ValidatorFactory: " + e.getLocalizedMessage());
}
}
return false;
}
示例15: process
import com.squareup.javapoet.JavaFile; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> reactModuleListElements = roundEnv.getElementsAnnotatedWith(
ReactModuleList.class);
for (Element reactModuleListElement : reactModuleListElements) {
if (!(reactModuleListElement instanceof TypeElement)) {
continue;
}
TypeElement typeElement = (TypeElement) reactModuleListElement;
ReactModuleList reactModuleList = typeElement.getAnnotation(ReactModuleList.class);
if (reactModuleList == null) {
continue;
}
ClassName className = ClassName.get(typeElement);
String packageName = ClassName.get(typeElement).packageName();
String fileName = className.simpleName();
List<String> nativeModules = new ArrayList<>();
try {
reactModuleList.nativeModules(); // throws MirroredTypesException
} catch (MirroredTypesException mirroredTypesException) {
List<? extends TypeMirror> typeMirrors = mirroredTypesException.getTypeMirrors();
for (TypeMirror typeMirror : typeMirrors) {
nativeModules.add(typeMirror.toString());
}
}
MethodSpec getReactModuleInfosMethod;
try {
getReactModuleInfosMethod = MethodSpec.methodBuilder("getReactModuleInfos")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addCode(getCodeBlockForReactModuleInfos(nativeModules))
.returns(MAP_TYPE)
.build();
} catch (ReactModuleSpecException reactModuleSpecException) {
mMessager.printMessage(ERROR, reactModuleSpecException.mMessage);
return false;
}
TypeSpec reactModulesInfosTypeSpec = TypeSpec.classBuilder(
fileName + "$$ReactModuleInfoProvider")
.addModifiers(Modifier.PUBLIC)
.addMethod(getReactModuleInfosMethod)
.addSuperinterface(ReactModuleInfoProvider.class)
.build();
JavaFile javaFile = JavaFile.builder(packageName, reactModulesInfosTypeSpec)
.addFileComment("Generated by " + getClass().getName())
.build();
try {
javaFile.writeTo(mFiler);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}