use of javax.lang.model.element.ExecutableElement in project LoganSquare by bluelinelabs.
the class MethodProcessor method isCallbackMethodAnnotationValid.
public boolean isCallbackMethodAnnotationValid(Element element, String annotationName) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
if (enclosingElement.getAnnotation(JsonObject.class) == null) {
error(enclosingElement, "%s: @%s methods can only be in classes annotated with @%s.", enclosingElement.getQualifiedName(), annotationName, JsonObject.class.getSimpleName());
return false;
}
ExecutableElement executableElement = (ExecutableElement) element;
if (executableElement.getParameters().size() > 0) {
error(element, "%s: @%s methods must not take any parameters.", enclosingElement.getQualifiedName(), annotationName);
return false;
}
List<? extends Element> allElements = enclosingElement.getEnclosedElements();
int methodInstances = 0;
for (Element enclosedElement : allElements) {
for (AnnotationMirror am : enclosedElement.getAnnotationMirrors()) {
if (am.getAnnotationType().asElement().getSimpleName().toString().equals(annotationName)) {
methodInstances++;
}
}
}
if (methodInstances != 1) {
error(element, "%s: There can only be one @%s method per class.", enclosingElement.getQualifiedName(), annotationName);
return false;
}
return true;
}
use of javax.lang.model.element.ExecutableElement in project LoganSquare by bluelinelabs.
the class OnJsonParseCompleteProcessor method processOnCompleteMethodAnnotation.
private void processOnCompleteMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
if (!isCallbackMethodAnnotationValid(element, OnJsonParseComplete.class.getSimpleName())) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
ExecutableElement executableElement = (ExecutableElement) element;
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
objectHolder.onCompleteCallback = executableElement.getSimpleName().toString();
}
use of javax.lang.model.element.ExecutableElement in project LoganSquare by bluelinelabs.
the class OnPreSerializeProcessor method processOnPreJsonSerializeMethodAnnotation.
private void processOnPreJsonSerializeMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
if (!isCallbackMethodAnnotationValid(element, OnPreJsonSerialize.class.getSimpleName())) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
ExecutableElement executableElement = (ExecutableElement) element;
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
objectHolder.preSerializeCallback = executableElement.getSimpleName().toString();
}
use of javax.lang.model.element.ExecutableElement in project LoganSquare by bluelinelabs.
the class JsonFieldHolder method getGetter.
public static String getGetter(Element element, Elements elements) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
TypeKind elementTypeKind = element.asType().getKind();
String elementName = element.getSimpleName().toString();
String elementNameLowerCase = elementName.toLowerCase();
List<String> possibleMethodNames = new ArrayList<>();
possibleMethodNames.add("get" + elementNameLowerCase);
if (elementTypeKind == TypeKind.BOOLEAN) {
possibleMethodNames.add("is" + elementNameLowerCase);
possibleMethodNames.add("has" + elementNameLowerCase);
possibleMethodNames.add(elementNameLowerCase);
}
// Handle the case where variables are named in the form mVariableName instead of just variableName
if (elementName.length() > 1 && elementName.charAt(0) == 'm' && (elementName.charAt(1) >= 'A' && elementName.charAt(1) <= 'Z')) {
possibleMethodNames.add("get" + elementNameLowerCase.substring(1));
if (elementTypeKind == TypeKind.BOOLEAN) {
possibleMethodNames.add("is" + elementNameLowerCase.substring(1));
possibleMethodNames.add("has" + elementNameLowerCase.substring(1));
possibleMethodNames.add(elementNameLowerCase.substring(1));
}
}
List<? extends Element> elementMembers = elements.getAllMembers(enclosingElement);
List<ExecutableElement> elementMethods = ElementFilter.methodsIn(elementMembers);
for (ExecutableElement methodElement : elementMethods) {
if (methodElement.getParameters().size() == 0) {
String methodNameString = methodElement.getSimpleName().toString();
String methodNameLowerCase = methodNameString.toLowerCase();
if (possibleMethodNames.contains(methodNameLowerCase)) {
if (methodElement.getParameters().size() == 0) {
if (methodElement.getReturnType().toString().equals(element.asType().toString())) {
return methodNameString;
}
}
}
}
}
return null;
}
use of javax.lang.model.element.ExecutableElement in project EventBus by greenrobot.
the class EventBusAnnotationProcessor method writeCreateSubscriberMethods.
private void writeCreateSubscriberMethods(BufferedWriter writer, List<ExecutableElement> methods, String callPrefix, String myPackage) throws IOException {
for (ExecutableElement method : methods) {
List<? extends VariableElement> parameters = method.getParameters();
TypeMirror paramType = getParamTypeMirror(parameters.get(0), null);
TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType);
String methodName = method.getSimpleName().toString();
String eventClass = getClassString(paramElement, myPackage) + ".class";
Subscribe subscribe = method.getAnnotation(Subscribe.class);
List<String> parts = new ArrayList<>();
parts.add(callPrefix + "(\"" + methodName + "\",");
String lineEnd = "),";
if (subscribe.priority() == 0 && !subscribe.sticky()) {
if (subscribe.threadMode() == ThreadMode.POSTING) {
parts.add(eventClass + lineEnd);
} else {
parts.add(eventClass + ",");
parts.add("ThreadMode." + subscribe.threadMode().name() + lineEnd);
}
} else {
parts.add(eventClass + ",");
parts.add("ThreadMode." + subscribe.threadMode().name() + ",");
parts.add(subscribe.priority() + ",");
parts.add(subscribe.sticky() + lineEnd);
}
writeLine(writer, 3, parts.toArray(new String[parts.size()]));
if (verbose) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Indexed @Subscribe at " + method.getEnclosingElement().getSimpleName() + "." + methodName + "(" + paramElement.getSimpleName() + ")");
}
}
}
Aggregations