use of javax.lang.model.util.Elements in project buck by facebook.
the class StubJarGeneratingProcessor method generateStubJar.
private void generateStubJar() {
try {
Elements elementUtils = processingEnv.getElementUtils();
StubJar stubJar = new StubJar(classFileVersion, elementUtils, topLevelTypeNames.stream().map(elementUtils::getTypeElement).collect(Collectors.toList()));
stubJar.writeTo(filesystem, stubJarPath);
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to create stub jar: %s. %s", stubJarPath, e.getMessage()), e);
}
}
use of javax.lang.model.util.Elements in project RoboBinding by RoboBinding.
the class ElementWrapperTest method unsupportedElements.
@DataPoints("unsupportedElements")
public static Element[] unsupportedElements() {
Elements elements = compilation.getElements();
PackageElement packageElement = elements.getPackageElement("java.lang");
TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
VariableElement variableElement = ElementFilter.fieldsIn(typeElement.getEnclosedElements()).get(0);
return new Element[] { packageElement, variableElement };
}
use of javax.lang.model.util.Elements in project RoboBinding by RoboBinding.
the class ElementWrapperTest method supportedElements.
@DataPoints("supportedElements")
public static ElementToWrapped[] supportedElements() {
Elements elements = compilation.getElements();
TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
ExecutableElement methodElement = ElementFilter.methodsIn(typeElement.getEnclosedElements()).get(0);
return new ElementToWrapped[] { a(typeElement).itsWrapped(WrappedTypeElement.class), a(methodElement).itsWrapped(MethodElement.class) };
}
use of javax.lang.model.util.Elements in project bazel by bazelbuild.
the class InternalUtils method leastUpperBound.
/**
* Returns the least upper bound of two {@link TypeMirror}s.
*
* @param processingEnv The {@link ProcessingEnvironment} to use.
* @param tm1 A {@link TypeMirror}.
* @param tm2 A {@link TypeMirror}.
* @return The least upper bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = (Type) tm1;
Type t2 = (Type) tm2;
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually (not done by types.lub).
if (t1.getKind() == TypeKind.NULL) {
return t2;
}
if (t2.getKind() == TypeKind.NULL) {
return t1;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t2;
} else if (types.isAssignable(t2, t1)) {
return t1;
} else {
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
WildcardType wc1 = (WildcardType) t1;
Type bound = (Type) wc1.getExtendsBound();
if (bound == null) {
// Implicit upper bound of java.lang.Object
Elements elements = processingEnv.getElementUtils();
return elements.getTypeElement("java.lang.Object").asType();
}
t1 = bound;
}
if (t2.getKind() == TypeKind.WILDCARD) {
WildcardType wc2 = (WildcardType) t2;
Type bound = (Type) wc2.getExtendsBound();
if (bound == null) {
// Implicit upper bound of java.lang.Object
Elements elements = processingEnv.getElementUtils();
return elements.getTypeElement("java.lang.Object").asType();
}
t2 = bound;
}
return types.lub(t1, t2);
}
use of javax.lang.model.util.Elements in project camel by apache.
the class AnnotationProcessorHelper method findTypeElement.
public static TypeElement findTypeElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, String className) {
if (isNullOrEmpty(className) || "java.lang.Object".equals(className)) {
return null;
}
Set<? extends Element> rootElements = roundEnv.getRootElements();
for (Element rootElement : rootElements) {
if (rootElement instanceof TypeElement) {
TypeElement typeElement = (TypeElement) rootElement;
String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
if (className.equals(aRootName)) {
return typeElement;
}
}
}
// fallback using package name
Elements elementUtils = processingEnv.getElementUtils();
int idx = className.lastIndexOf('.');
if (idx > 0) {
String packageName = className.substring(0, idx);
PackageElement pe = elementUtils.getPackageElement(packageName);
if (pe != null) {
List<? extends Element> enclosedElements = getEnclosedElements(pe);
for (Element rootElement : enclosedElements) {
if (rootElement instanceof TypeElement) {
TypeElement typeElement = (TypeElement) rootElement;
String aRootName = canonicalClassName(typeElement.getQualifiedName().toString());
if (className.equals(aRootName)) {
return typeElement;
}
}
}
}
}
return null;
}
Aggregations