use of javax.lang.model.element.Element in project neo4j by neo4j.
the class PerformsWriteMethodVisitorTest method rejects_conflicted_mode_usage.
@Test
public void rejects_conflicted_mode_usage() {
Element element = elementTestUtils.findMethodElement(PerformsWriteProcedures.class, "conflictingMode");
Stream<CompilationMessage> errors = visitor.visit(element);
assertThat(errors).hasSize(1).extracting(CompilationMessage::getCategory, CompilationMessage::getElement, CompilationMessage::getContents).contains(tuple(Diagnostic.Kind.ERROR, element, "@PerformsWrites usage error: cannot use mode other than Mode.DEFAULT"));
}
use of javax.lang.model.element.Element in project T-MVP by north2016.
the class ApiFactoryProcessor method process.
@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
String CLASS_NAME = "ApiFactory";
String DATA_ARR_CLASS = "DataArr";
TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ API工厂 此类由apt自动生成");
try {
for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(ApiFactory.class))) {
mAbstractProcessor.mMessager.printMessage(Diagnostic.Kind.NOTE, "正在处理: " + element.toString());
for (Element e : element.getEnclosedElements()) {
ExecutableElement executableElement = (ExecutableElement) e;
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(e.getSimpleName().toString()).addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC);
if (TypeName.get(executableElement.getReturnType()).toString().contains(DATA_ARR_CLASS)) {
//返回列表数据
methodBuilder.returns(ClassName.get("rx", "Observable"));
Map<String, Object> params = new HashMap<>();
methodBuilder.addParameter(params.getClass(), "param");
ClassName apiUtil = ClassName.get("com.base.util", "ApiUtil");
ClassName C = ClassName.get("com", "C");
CodeBlock.Builder blockBuilder = CodeBlock.builder();
int len = executableElement.getParameters().size();
for (int i = 0; i < len; i++) {
VariableElement ep = executableElement.getParameters().get(i);
boolean isLast = i == len - 1;
String split = (isLast ? "" : ",");
switch(ep.getSimpleName().toString()) {
case "include":
blockBuilder.add("$L.getInclude(param)" + split, apiUtil);
break;
case "where":
blockBuilder.add("$L.getWhere(param)" + split, apiUtil);
break;
case "skip":
blockBuilder.add("$L.getSkip(param)" + split, apiUtil);
break;
case "limit":
blockBuilder.add("$L.PAGE_COUNT" + split, C);
break;
case "order":
blockBuilder.add("$L._CREATED_AT" + split, C);
break;
}
}
methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), blockBuilder.build().toString(), ClassName.get("com.base.util.helper", "RxSchedulers"));
tb.addMethod(methodBuilder.build());
} else {
//返回普通数据
methodBuilder.returns(TypeName.get(executableElement.getReturnType()));
String paramsString = "";
for (VariableElement ep : executableElement.getParameters()) {
methodBuilder.addParameter(TypeName.get(ep.asType()), ep.getSimpleName().toString());
paramsString += ep.getSimpleName().toString() + ",";
}
methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), paramsString.substring(0, paramsString.length() - 1), ClassName.get("com.base.util.helper", "RxSchedulers"));
tb.addMethod(methodBuilder.build());
}
}
}
// 生成源代码
JavaFile javaFile = JavaFile.builder(Utils.PackageName, tb.build()).build();
// 在 app module/build/generated/source/apt 生成一份源代码
javaFile.writeTo(mAbstractProcessor.mFiler);
} catch (FilerException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.lang.model.element.Element in project T-MVP by north2016.
the class RouterProcessor method process.
@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
String CLASS_NAME = "TRouter";
TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ 全局路由器 此类由apt自动生成");
MethodSpec.Builder methodBuilder1 = MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addParameter(ClassName.get("android.view", "View"), "view");
MethodSpec.Builder methodBuilder2 = MethodSpec.methodBuilder("bind").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(ClassName.get("android.app", "Activity"), "mContext");
List<ClassName> mList = new ArrayList<>();
CodeBlock.Builder blockBuilderGo = CodeBlock.builder();
CodeBlock.Builder blockBuilderBind = CodeBlock.builder();
ClassName appClassName = ClassName.get("com", "App");
blockBuilderGo.addStatement("$T.getAppContext().mCurActivityExtra=extra", appClassName);
blockBuilderGo.addStatement("Activity mContext=$T.getAppContext().getCurActivity()", appClassName);
//括号开始
blockBuilderGo.beginControlFlow(" switch (name)");
//括号开始
blockBuilderBind.beginControlFlow(" switch (mContext.getClass().getSimpleName())");
List<RouterActivityModel> mRouterActivityModels = new ArrayList<>();
try {
for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(Router.class))) {
ClassName currentType = ClassName.get(element);
if (mList.contains(currentType))
continue;
mList.add(currentType);
RouterActivityModel mRouterActivityModel = new RouterActivityModel();
mRouterActivityModel.setElement(element);
mRouterActivityModel.setActionName(element.getAnnotation(Router.class).value());
List<Element> mExtraElements = new ArrayList<>();
List<String> mExtraElementKeys = new ArrayList<>();
for (Element childElement : element.getEnclosedElements()) {
SceneTransition mSceneTransitionAnnotation = childElement.getAnnotation(SceneTransition.class);
if (mSceneTransitionAnnotation != null) {
mRouterActivityModel.setSceneTransitionElementName(mSceneTransitionAnnotation.value());
mRouterActivityModel.setSceneTransitionElement(childElement);
}
Extra mExtraAnnotation = childElement.getAnnotation(Extra.class);
if (mExtraAnnotation != null) {
mExtraElementKeys.add(mExtraAnnotation.value());
mExtraElements.add(childElement);
}
}
mRouterActivityModel.setExtraElementKeys(mExtraElementKeys);
mRouterActivityModel.setExtraElements(mExtraElements);
boolean isNeedBind = (mExtraElementKeys != null && mExtraElementKeys.size() > 0 || mRouterActivityModel.getSceneTransitionElement() != null);
mRouterActivityModel.setNeedBind(isNeedBind);
mRouterActivityModels.add(mRouterActivityModel);
}
ClassName mActivityCompatName = ClassName.get("android.support.v4.app", "ActivityCompat");
ClassName mIntentClassName = ClassName.get("android.content", "Intent");
ClassName mActivityOptionsCompatName = ClassName.get("android.support.v4.app", "ActivityOptionsCompat");
for (RouterActivityModel item : mRouterActivityModels) {
//1
blockBuilderGo.add("case $S: \n", item.getActionName());
if (item.isNeedBind())
//1
blockBuilderBind.add("case $S: \n", item.getElement().getSimpleName());
if (item.getExtraElements() != null && item.getExtraElements().size() > 0) {
for (int i = 0; i < item.getExtraElements().size(); i++) {
Element mFiled = item.getExtraElements().get(i);
blockBuilderBind.add(//1
"(($T)mContext)." + //2
"$L" + //3
"= ($T) " + //4
"$T.getAppContext().mCurActivityExtra.get(" + //5
"$S);\n", //1
item.getElement(), //2
mFiled, //3
mFiled, //4
appClassName, //5
item.getExtraElementKeys().get(i));
//5
}
}
if (item.getSceneTransitionElement() != null) {
blockBuilderGo.add(//2
"$L.startActivity(mContext," + //3
"\nnew $L(mContext," + //4
"\n$L.class)," + //5
"\n$T.makeSceneTransitionAnimation(" + //6
"\nmContext,view," + //7
"\n$S).toBundle());", //2
mActivityCompatName, //3
mIntentClassName, //4
item.getElement(), //5
mActivityOptionsCompatName, //6
item.getSceneTransitionElementName());
blockBuilderBind.add(//2
"$T.setTransitionName(" + //3
"(($T)mContext)." + //4
"$L, " + //5
"$S);\n", //2
ClassName.get("android.support.v4.view", "ViewCompat"), //3
item.getElement(), //4
item.getSceneTransitionElement(), //5
item.getSceneTransitionElementName());
} else {
blockBuilderGo.add(//2
"mContext.startActivity(" + //3
"\nnew $L(mContext," + //7
"\n$L.class));", //3
mIntentClassName, //4
item.getElement());
}
//1
blockBuilderGo.addStatement("\nbreak");
//1
if (item.isNeedBind())
blockBuilderBind.addStatement("break");
}
blockBuilderGo.addStatement("default: break");
blockBuilderGo.endControlFlow();
methodBuilder1.addCode(blockBuilderGo.build());
blockBuilderBind.addStatement("default: break");
blockBuilderBind.endControlFlow();
methodBuilder2.addCode(blockBuilderBind.build());
tb.addMethod(methodBuilder1.build());
tb.addMethod(methodBuilder2.build());
//增加go(action)和go(action,extra):两个重载方法
tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addCode("go(name,extra,null);\n").build());
tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addCode("go(name,null,null);\n").build());
// 生成源代码
JavaFile javaFile = JavaFile.builder(Utils.PackageName, tb.build()).build();
// 在 app module/build/generated/source/apt 生成一份源代码
javaFile.writeTo(mAbstractProcessor.mFiler);
} catch (FilerException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.lang.model.element.Element in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedExecutables.
/**
* Processes factory methods or constructors annotated with {@link StorIOContentResolverCreator}.
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOContentResolverType}
*/
@Override
protected void processAnnotatedExecutables(@NotNull RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOContentResolverCreator = roundEnvironment.getElementsAnnotatedWith(StorIOContentResolverCreator.class);
for (final Element annotatedElement : elementsAnnotatedWithStorIOContentResolverCreator) {
final ExecutableElement annotatedExecutableElement = (ExecutableElement) annotatedElement;
validateAnnotatedExecutable(annotatedExecutableElement);
final StorIOContentResolverCreatorMeta storIOContentResolverCreatorMeta = new StorIOContentResolverCreatorMeta(annotatedExecutableElement.getEnclosingElement(), annotatedExecutableElement, annotatedExecutableElement.getAnnotation(StorIOContentResolverCreator.class));
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedClasses.get(storIOContentResolverCreatorMeta.enclosingElement);
// If class already contains another creator -> throw exception.
if (storIOContentResolverTypeMeta.creator == null) {
storIOContentResolverTypeMeta.creator = annotatedExecutableElement;
} else {
throw new ProcessingException(annotatedExecutableElement, "Only one creator method or constructor is allowed: " + annotatedExecutableElement.getEnclosingElement().getSimpleName());
}
}
}
use of javax.lang.model.element.Element in project querydsl by querydsl.
the class JDOConfiguration method getConfig.
@Override
public VisitorConfig getConfig(TypeElement e, List<? extends Element> elements) {
boolean fields = false, methods = false;
for (Element element : elements) {
if (hasRelevantAnnotation(element)) {
fields |= element.getKind().equals(ElementKind.FIELD);
methods |= element.getKind().equals(ElementKind.METHOD);
}
}
return VisitorConfig.get(fields, methods, VisitorConfig.FIELDS_ONLY);
}
Aggregations