com.zhangyue.we.anoprocesser.XmlProcessor Maven / Gradle / Ivy
The newest version!
package com.zhangyue.we.anoprocesser;
import com.zhangyue.we.anoprocesser.xml.Constants;
import com.zhangyue.we.anoprocesser.xml.LayoutManager;
import com.zhangyue.we.x2c.ano.Xml;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
/**
* @author chengwei 2018/8/7
*/
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@SupportedAnnotationTypes("com.zhangyue.we.x2c.ano.Xml")
public class XmlProcessor extends AbstractProcessor {
private LayoutManager mLayoutMgr;
private boolean isEnable = false;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
Map options = processingEnvironment.getOptions();
isEnable = isEnable(options.get(Constants.KEY_ENABLE_SWITCH));
if (!isEnable) {
Log.w("XmlProcessor is disable");
return;
}
Log.w("11111");
Log.init(processingEnvironment.getMessager());
mLayoutMgr = LayoutManager.instance();
mLayoutMgr.setFiler(processingEnvironment.getFiler());
}
private boolean isEnable(String s) {
if (s == null || s.length() == 0) {
return false;
}
try {
return Boolean.parseBoolean(s);
} catch (Exception e) {
return false;
}
}
@Override
public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {
if (!isEnable) {
return true;
}
Set extends Element> elements = roundEnvironment.getElementsAnnotatedWith(Xml.class);
TreeSet layouts = new TreeSet<>();
for (Element element : elements) {
Xml xml = element.getAnnotation(Xml.class);
String[] names = xml.layouts();
for (String name : names) {
layouts.add(name.substring(name.lastIndexOf(".") + 1));
}
}
for (String name : layouts) {
mLayoutMgr.setGroupId(Math.abs(name.hashCode()));
mLayoutMgr.translate(name);
}
mLayoutMgr.printTranslate();
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy