补充一个读取模板文件生成类的方法过程。
项目结构,模板放置位置
模板文件内容:
package %package%;
public class %classname% {
private String name;
private int data;
private void setName(String name){
this.name = name;
}
private String getName(){
return name;
}
private void setData(){
data = %data%;
}
private int getData(){
return data;
}
}
读取模板文件的工具:
//文件名写法 /temp/template
public static String getTempFileString( String file ) {
BufferedReader reader = new BufferedReader( new InputStreamReader( Utils.class.getResourceAsStream( file ) ) );
StringBuilder builder = new StringBuilder();
try {
String line;
while ( ( line = reader.readLine() ) != null ) {
builder.append( line ).append( "\n" );
}
reader.close();
} catch ( IOException e ) {
try {
reader.close();
} catch ( IOException e1 ) {
}
}
return builder.toString();
}
processor文件写法:
@SupportedOptions({"CLASSNAME"})
@SupportedAnnotationTypes("com.ldx.annotationlib.Template")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@AutoService(Processor.class)
public class AptProcessor5 extends AbstractProcessor {
private Filer mFilerUtils;
private Types mTypesUtils;
private Elements mElementsUtils;
private Messager mMessager;
private Map<String,String> mOptionMap;
private ArrayList<VariableElement> mElementList;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
mFilerUtils = processingEnv.getFiler();
mTypesUtils = processingEnv.getTypeUtils();
mElementsUtils = processingEnv.getElementUtils();
mMessager = processingEnv.getMessager();
mOptionMap = processingEnv.getOptions();
mElementList = new ArrayList<>();
}
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
if (set != null && set.size() > 0){
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(Template.class);
for (Element element : elements) {
VariableElement variableElement = (VariableElement) element;
mElementList.add(variableElement);
}
createFile(mElementList);
return true;
}
return false;
}
private void createFile(ArrayList<VariableElement> mElementList) {
TypeElement enclosingElement = (TypeElement) mElementList.get(0).getEnclosingElement();
String pkName = mElementsUtils.getPackageOf(enclosingElement).getQualifiedName().toString();
String packName = mElementsUtils.getPackageOf(enclosingElement).asType().toString();
String className = enclosingElement.getSimpleName().toString();
try {
JavaFileObject jfo = mFilerUtils.createSourceFile(pkName + "."+className+ "Template", new Element[]{});
Writer writer = jfo.openWriter();
String data = Utils.getTempFileString("/temp/template");
if (data != null){
data = data.replace("%classname%", className+ "Template");
data = data.replace("%package%", pkName);
data = data.replace("%data%", "2");
}
writer.write(data);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最终生成的文件
package com.ldx.canvasdrawdemo.activity;
public class BindViewActivityTemplate {
private String name;
private int data;
private void setName(String name){
this.name = name;
}
private String getName(){
return name;
}
private void setData(){
data = 2;
}
private int getData(){
return data;
}
}
网友评论