use of javax.lang.model.util.Elements in project auto by google.
the class ExtensionTest method testCantConsumeNonExistentMethod.
@Test
public void testCantConsumeNonExistentMethod() {
class ConsumeBogusMethod extends NonFinalExtension {
@Override
public Set<ExecutableElement> consumeMethods(Context context) {
// Find Integer.intValue() and try to consume that.
Elements elementUtils = context.processingEnvironment().getElementUtils();
TypeElement javaLangInteger = elementUtils.getTypeElement(Integer.class.getName());
for (ExecutableElement method : ElementFilter.methodsIn(javaLangInteger.getEnclosedElements())) {
if (method.getSimpleName().contentEquals("intValue")) {
return ImmutableSet.of(method);
}
}
throw new AssertionError("Could not find Integer.intValue()");
}
}
JavaFileObject impl = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "import com.google.auto.value.AutoValue;", "@AutoValue public abstract class Baz {", " abstract String foo();", "}");
Compilation compilation = javac().withProcessors(new AutoValueProcessor(ImmutableList.of(new ConsumeBogusMethod()))).compile(impl);
assertThat(compilation).hadErrorContainingMatch("wants to consume a method that is not one of the abstract methods in this class" + ".*intValue\\(\\)").inFile(impl).onLineContaining("@AutoValue public abstract class Baz");
}
use of javax.lang.model.util.Elements in project dubbo by alibaba.
the class AbstractServiceRestMetadataResolver method resolve.
@Override
public final ServiceRestMetadata resolve(ProcessingEnvironment processingEnv, TypeElement serviceType, Set<? extends TypeElement> annotations) {
info("%s is processing the service type[%s] with annotations[%s]", processorName, serviceType, annotations.stream().map(t -> "@" + t.toString()).collect(Collectors.joining(",")));
ServiceRestMetadata serviceRestMetadata = new ServiceRestMetadata();
Elements elements = processingEnv.getElementUtils();
try {
AnnotationMirror serviceAnnotation = getAnnotation(serviceType);
String serviceInterfaceName = resolveServiceInterfaceName(serviceType, serviceAnnotation);
serviceRestMetadata.setServiceInterface(serviceInterfaceName);
serviceRestMetadata.setGroup(getGroup(serviceAnnotation));
serviceRestMetadata.setVersion(getVersion(serviceAnnotation));
TypeElement serviceInterfaceType = elements.getTypeElement(serviceInterfaceName);
List<? extends ExecutableElement> serviceMethods = new LinkedList<>(getPublicNonStaticMethods(serviceInterfaceType, Object.class));
// Sorts
sort(serviceMethods, ExecutableElementComparator.INSTANCE);
serviceMethods.forEach(serviceMethod -> {
resolveRestMethodMetadata(processingEnv, serviceType, serviceInterfaceType, serviceMethod).ifPresent(serviceRestMetadata.getMeta()::add);
});
} finally {
clearCache();
}
info("The %s's process result : %s", processorName, serviceRestMetadata);
return serviceRestMetadata;
}
use of javax.lang.model.util.Elements in project dubbo by alibaba.
the class CollectionTypeDefinitionBuilder method accept.
@Override
public boolean accept(ProcessingEnvironment processingEnv, DeclaredType type) {
Elements elements = processingEnv.getElementUtils();
TypeElement collectionTypeElement = elements.getTypeElement(Collection.class.getTypeName());
TypeMirror collectionType = collectionTypeElement.asType();
Types types = processingEnv.getTypeUtils();
TypeMirror erasedType = types.erasure(type);
return types.isAssignable(erasedType, collectionType);
}
use of javax.lang.model.util.Elements in project dagger by square.
the class ModuleAdapterProcessor method providerMethodsByClass.
/**
* Returns a map containing all {@code @Provides} methods, indexed by class.
*/
private Map<String, List<ExecutableElement>> providerMethodsByClass(RoundEnvironment env) {
Elements elementUtils = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Map<String, List<ExecutableElement>> result = new HashMap<String, List<ExecutableElement>>();
provides: for (Element providerMethod : findProvidesMethods(env)) {
switch(providerMethod.getEnclosingElement().getKind()) {
case CLASS:
// valid, move along
break;
default:
// TODO(tbroyer): pass annotation information
error("Unexpected @Provides on " + elementToString(providerMethod), providerMethod);
continue;
}
TypeElement type = (TypeElement) providerMethod.getEnclosingElement();
Set<Modifier> typeModifiers = type.getModifiers();
if (typeModifiers.contains(PRIVATE) || typeModifiers.contains(ABSTRACT)) {
error("Classes declaring @Provides methods must not be private or abstract: " + type.getQualifiedName(), type);
continue;
}
Set<Modifier> methodModifiers = providerMethod.getModifiers();
if (methodModifiers.contains(PRIVATE) || methodModifiers.contains(ABSTRACT) || methodModifiers.contains(STATIC)) {
error("@Provides methods must not be private, abstract or static: " + type.getQualifiedName() + "." + providerMethod, providerMethod);
continue;
}
ExecutableElement providerMethodAsExecutable = (ExecutableElement) providerMethod;
if (!providerMethodAsExecutable.getThrownTypes().isEmpty()) {
error("@Provides methods must not have a throws clause: " + type.getQualifiedName() + "." + providerMethod, providerMethod);
continue;
}
// Invalidate return types.
TypeMirror returnType = types.erasure(providerMethodAsExecutable.getReturnType());
if (!returnType.getKind().equals(TypeKind.ERROR)) {
// processors is not "invalid" in this way, so ignore).
for (String invalidTypeName : INVALID_RETURN_TYPES) {
TypeElement invalidTypeElement = elementUtils.getTypeElement(invalidTypeName);
if (invalidTypeElement != null && types.isSameType(returnType, types.erasure(invalidTypeElement.asType()))) {
error(String.format("@Provides method must not return %s directly: %s.%s", invalidTypeElement, type.getQualifiedName(), providerMethod), providerMethod);
// Skip to next provides method.
continue provides;
}
}
}
List<ExecutableElement> methods = result.get(type.getQualifiedName().toString());
if (methods == null) {
methods = new ArrayList<ExecutableElement>();
result.put(type.getQualifiedName().toString(), methods);
}
methods.add(providerMethodAsExecutable);
}
TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType();
// should still be registered and a ModuleAdapter should still be written.
for (Element module : env.getElementsAnnotatedWith(Module.class)) {
if (!module.getKind().equals(ElementKind.CLASS)) {
error("Modules must be classes: " + elementToString(module), module);
continue;
}
TypeElement moduleType = (TypeElement) module;
// Verify that all modules do not extend from non-Object types.
if (!types.isSameType(moduleType.getSuperclass(), objectType)) {
error("Modules must not extend from other classes: " + elementToString(module), module);
}
String moduleName = moduleType.getQualifiedName().toString();
if (result.containsKey(moduleName))
continue;
result.put(moduleName, new ArrayList<ExecutableElement>());
}
return result;
}
use of javax.lang.model.util.Elements in project ceylon-compiler by ceylon.
the class T6358786 method main.
public static void main(String... args) throws IOException {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
String srcdir = System.getProperty("test.src");
File file = new File(srcdir, args[0]);
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, fm, null, null, null, fm.getJavaFileObjectsFromFiles(Arrays.asList(file)));
Elements elements = task.getElements();
for (TypeElement clazz : task.enter(task.parse())) {
String doc = elements.getDocComment(clazz);
if (doc == null)
throw new AssertionError(clazz.getSimpleName() + ": no doc comment");
System.out.format("%s: %s%n", clazz.getSimpleName(), doc);
}
}
Aggregations