use of javax.lang.model.element.PackageElement in project requery by requery.
the class AndroidObservableExtension method addToSetter.
@Override
public void addToSetter(AttributeDescriptor member, MethodSpec.Builder builder) {
if (!observable || !isBindable(member)) {
return;
}
Elements elements = processingEnvironment.getElementUtils();
// data binding compiler will create a useful set of classes in /data-binding-info
String bindingInfo = BINDING_PACKAGE + ".layouts.DataBindingInfo";
TypeElement dataBindingType = elements.getTypeElement(bindingInfo);
ClassName BRclass = null;
if (!Names.isEmpty(modulePackage)) {
BRclass = ClassName.get(modulePackage, "BR");
} else if (dataBindingType != null) {
// fallback for pre Android gradle plugin 2.3.0
Optional<String> modulePackage = Mirrors.findAnnotationMirror(dataBindingType, BINDING_PACKAGE + ".BindingBuildInfo").map(mirror -> Mirrors.findAnnotationValue(mirror, "modulePackage")).filter(Optional::isPresent).map(Optional::get).map(AnnotationValue::toString);
if (modulePackage.isPresent()) {
// not actually checking the BR class exists since it maybe generated later
BRclass = ClassName.get(modulePackage.get().replaceAll("\"", ""), "BR");
}
}
if (BRclass == null) {
PackageElement packageElement = elements.getPackageOf(entity.element());
String packageName = packageElement.getQualifiedName().toString();
BRclass = ClassName.get(packageName, "BR");
}
// matching the way the BR property names are generated
// https://code.google.com/p/android/issues/detail?id=199436
String propertyName = member.setterName().replaceFirst("set", "");
/*
if (Names.isAllUpper(propertyName)) {
propertyName = propertyName.toLowerCase();
} else {
propertyName = Names.lowerCaseFirst(propertyName);
}*/
propertyName = Names.lowerCaseFirst(propertyName);
builder.addStatement("notifyPropertyChanged($L.$L)", BRclass, propertyName);
}
use of javax.lang.model.element.PackageElement in project EventBus by greenrobot.
the class EventBusAnnotationProcessor method getClassString.
private String getClassString(TypeElement typeElement, String myPackage) {
PackageElement packageElement = getPackageElement(typeElement);
String packageString = packageElement.getQualifiedName().toString();
String className = typeElement.getQualifiedName().toString();
if (packageString != null && !packageString.isEmpty()) {
if (packageString.equals(myPackage)) {
className = cutPackage(myPackage, className);
} else if (packageString.equals("java.lang")) {
className = typeElement.getSimpleName().toString();
}
}
return className;
}
use of javax.lang.model.element.PackageElement in project neo4j by neo4j.
the class FunctionVisitor method validateName.
public Stream<CompilationMessage> validateName(ExecutableElement method) {
Optional<String> customName = customNameExtractor.apply(method.getAnnotation(annotationType));
if (customName.isPresent()) {
if (isInRootNamespace(customName.get())) {
return Stream.of(rootNamespaceError(method, customName.get()));
}
return Stream.empty();
}
PackageElement namespace = elements.getPackageOf(method);
if (namespace == null) {
return Stream.of(rootNamespaceError(method));
}
return Stream.empty();
}
use of javax.lang.model.element.PackageElement in project maven-plugins by apache.
the class SimpleAnnotationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (annotations.isEmpty()) {
return true;
}
// assert that commons-lang3 is on the classpath
try {
getClass().getClassLoader().loadClass("org.apache.commons.lang3.StringUtils");
} catch (ClassNotFoundException expected) {
throw new RuntimeException("Expected org.apache.commons.lang3.StringUtils to be on the processorpath," + "because it is a declared dependency of the annotation processor.");
}
// assert that commons-io is NOT on the classpath, as it is only a project dependency in "annotation-user"
try {
getClass().getClassLoader().loadClass("org.apache.commons.io.IOUtils");
throw new RuntimeException("Expected a ClassNotFoundException because " + "org.apache.commons.io.IOUtils is not supposed to be on the processorpath.");
} catch (ClassNotFoundException expected) {
// expected.
}
Filer filer = processingEnv.getFiler();
Elements elementUtils = processingEnv.getElementUtils();
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotations.iterator().next());
for (Element element : elements) {
Name name = element.getSimpleName();
PackageElement packageElement = elementUtils.getPackageOf(element);
try {
Name packageName = packageElement.getQualifiedName();
FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
Writer writer = resource.openWriter();
writer.write(name.toString());
writer.close();
String className = name + "Companion";
JavaFileObject javaFile = filer.createSourceFile(packageName + "." + className, element);
Writer javaWriter = javaFile.openWriter();
javaWriter.append("package ").append(packageName).append(";\n\n");
javaWriter.append("public class ").append(className).append(" {\n");
javaWriter.append(" public ").append(className).append("() {\n");
javaWriter.append(" System.out.println(\"Hey there!\");\n");
javaWriter.append(" }\n}\n");
javaWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return !elements.isEmpty();
}
use of javax.lang.model.element.PackageElement in project dukescript-presenters by dukescript.
the class JUnitTestMethodsProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element e : roundEnv.getElementsAnnotatedWith(JUnitTestMethods.class)) {
JUnitTestMethods ju = e.getAnnotation(JUnitTestMethods.class);
if (ju == null) {
continue;
}
PackageElement pkg = processingEnv.getElementUtils().getPackageOf(e);
try {
TypeMirror sn;
try {
ju.superclass().getName();
throw new IllegalStateException();
} catch (MirroredTypeException ex) {
sn = ex.getTypeMirror();
}
JavaFileObject sf = processingEnv.getFiler().createSourceFile(pkg.toString() + "." + ju.name(), e);
Writer w = sf.openWriter();
w.append("package ").append(pkg.toString()).append(";\n");
w.append("abstract class ").append(ju.name()).append(" extends ").append(sn.toString()).append(ju.generics()).append(" {\n");
w.append(" int success;\n");
w.append(" int failure;\n");
w.append(" final java.io.StringWriter output = new java.io.StringWriter();\n");
w.append(" private final java.io.PrintWriter printer= new java.io.PrintWriter(output);\n");
w.append("\n");
Element esn = processingEnv.getTypeUtils().asElement(sn);
for (Element ee : esn.getEnclosedElements()) {
if (ee.getKind() != ElementKind.CONSTRUCTOR) {
continue;
}
ExecutableElement constructor = (ExecutableElement) ee;
w.append(" ").append(ju.name()).append("(");
String sep = "";
for (VariableElement ve : constructor.getParameters()) {
final TypeMirror vet = processingEnv.getTypeUtils().erasure(ve.asType());
w.append(sep).append(vet.toString()).append(" ").append(ve.getSimpleName());
sep = ", ";
}
w.append(") {\n");
w.append(" super(");
sep = "";
for (VariableElement ve : constructor.getParameters()) {
w.append(sep).append(ve.getSimpleName());
sep = ", ";
}
w.append(");\n");
w.append(" }\n");
}
final TypeMirror ko;
try {
ju.annotatedBy().getName();
throw new IllegalStateException();
} catch (MirroredTypeException ex) {
ko = ex.getTypeMirror();
}
final List<? extends TypeMirror> tests;
try {
ju.tests();
throw new IllegalStateException();
} catch (MirroredTypesException ex) {
tests = ex.getTypeMirrors();
}
w.append(" protected abstract void runMethod(String name) throws Throwable;\n");
StringBuilder testAll = new StringBuilder();
testAll.append(" public void test").append(ju.name()).append("() {\n");
for (TypeMirror test : tests) {
Element et = processingEnv.getTypeUtils().asElement(test);
for (Element ee : et.getEnclosedElements()) {
if (ee.getKind() != ElementKind.METHOD) {
continue;
}
boolean found = false;
for (AnnotationMirror am : ee.getAnnotationMirrors()) {
if (processingEnv.getTypeUtils().isSameType(am.getAnnotationType(), ko)) {
found = true;
break;
}
}
if (!found) {
continue;
}
String name = ee.getSimpleName().toString();
;
if (!name.startsWith("test")) {
name = "test" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
w.append(" public void run" + name + "() {\n");
w.append(" try {\n");
w.append(" runMethod(\"" + name + "\");\n");
w.append(" success++;\n");
w.append(" } catch (Throwable t) {\n");
w.append(" printer.println();\n");
w.append(" printer.println(\"===== " + name + " =====\");\n");
w.append(" t.printStackTrace(printer);\n");
w.append(" failure++;\n");
w.append(" } finally {\n");
w.append(" printer.flush();\n");
w.append(" }\n");
w.append(" }\n");
testAll.append(" run").append(name).append("();\n");
}
}
testAll.append(" if (failure > 0) {\n");
testAll.append(" output.append(\"Success: \" + success + \" Failure: \" + failure + \"\\n\");\n");
testAll.append(" throw new AssertionError(output.toString());\n");
testAll.append(" }\n");
testAll.append(" }\n");
w.append(testAll.toString());
w.append("}\n");
w.close();
} catch (IOException ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ex.getMessage(), e);
}
}
return true;
}
Aggregations