use of javax.lang.model.element.ExecutableElement 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.ExecutableElement 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.ExecutableElement in project querydsl by querydsl.
the class TypeElementHandler method handleEntityType.
public EntityType handleEntityType(TypeElement element) {
EntityType entityType = typeFactory.getEntityType(element.asType(), true);
List<? extends Element> elements = element.getEnclosedElements();
VisitorConfig config = configuration.getConfig(element, elements);
Set<String> blockedProperties = new HashSet<String>();
Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
// constructors
if (config.visitConstructors()) {
handleConstructors(entityType, elements);
}
// fields
if (config.visitFieldProperties()) {
for (VariableElement field : ElementFilter.fieldsIn(elements)) {
String name = field.getSimpleName().toString();
if (configuration.isBlockedField(field)) {
blockedProperties.add(name);
} else if (configuration.isValidField(field)) {
Annotations annotations = new Annotations();
configuration.inspect(field, annotations);
annotations.addAnnotation(field.getAnnotation(QueryType.class));
annotations.addAnnotation(field.getAnnotation(QueryInit.class));
propertyAnnotations.put(name, annotations);
propertyTypes.put(name, field.asType());
TypeMirror fixedType = configuration.getRealType(field);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
// methods
if (config.visitMethodProperties()) {
for (ExecutableElement method : ElementFilter.methodsIn(elements)) {
String name = method.getSimpleName().toString();
if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) {
name = BeanUtils.uncapitalize(name.substring(3));
} else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) {
name = BeanUtils.uncapitalize(name.substring(2));
} else {
continue;
}
if (configuration.isBlockedGetter(method)) {
blockedProperties.add(name);
} else if (configuration.isValidGetter(method) && !blockedProperties.contains(name)) {
Annotations annotations = propertyAnnotations.get(name);
if (annotations == null) {
annotations = new Annotations();
propertyAnnotations.put(name, annotations);
}
configuration.inspect(method, annotations);
annotations.addAnnotation(method.getAnnotation(QueryType.class));
annotations.addAnnotation(method.getAnnotation(QueryInit.class));
propertyTypes.put(name, method.getReturnType());
TypeMirror fixedType = configuration.getRealType(method);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
// fixed types override property types
propertyTypes.putAll(fixedTypes);
for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
if (property != null) {
entityType.addProperty(property);
}
}
return entityType;
}
use of javax.lang.model.element.ExecutableElement in project querydsl by querydsl.
the class TypeElementHandler method handleConstructors.
private void handleConstructors(EntityType entityType, List<? extends Element> elements) {
for (ExecutableElement constructor : ElementFilter.constructorsIn(elements)) {
if (configuration.isValidConstructor(constructor)) {
List<Parameter> parameters = transformParams(constructor.getParameters());
entityType.addConstructor(new Constructor(parameters));
}
}
}
use of javax.lang.model.element.ExecutableElement in project requery by requery.
the class AttributeMember method setterName.
@Override
public String setterName() {
if (element().getKind().isField()) {
String name = annotationOf(Naming.class).map(Naming::setter).orElse("");
return getMethodName(name, useBeanStyleProperties() ? "set" : "");
} else {
// if an interface try to find a matching setter to implement
for (ExecutableElement element : ElementFilter.methodsIn(entity.element().getEnclosedElements())) {
List<? extends VariableElement> parameters = element.getParameters();
if (parameters.size() == 1) {
String property = Names.removeMethodPrefixes(element.getSimpleName().toString());
if (property.toLowerCase(Locale.ROOT).equalsIgnoreCase(name())) {
return element.getSimpleName().toString();
}
}
}
// otherwise create one
ExecutableElement executableElement = (ExecutableElement) element();
String elementName = element().getSimpleName().toString();
AccessorNamePrefix prefix = AccessorNamePrefix.fromElement(executableElement);
switch(prefix) {
case GET:
return elementName.replaceFirst("get", "set");
case IS:
return elementName.replaceFirst("is", "set");
case NONE:
default:
return elementName;
}
}
}
Aggregations