use of javax.lang.model.element.VariableElement in project buck by facebook.
the class InterfaceValidator method validate.
public void validate(List<? extends CompilationUnitTree> compilationUnits) {
try (BuckTracing.TraceSection trace = BUCK_TRACING.traceSection("buck.abi.validate")) {
new InterfaceTypeAndConstantReferenceFinder(trees, new InterfaceTypeAndConstantReferenceFinder.Listener() {
private final Set<Element> importedTypes = new HashSet<>();
@Override
public void onTypeImported(TypeElement type) {
importedTypes.add(type);
}
@Override
public void onTypeReferenceFound(TypeElement referencedType, TreePath path, Element enclosingElement) {
PackageElement enclosingPackage = getPackageElement(enclosingElement);
if (typeWillBeAvailable(referencedType) || referenceIsLegalForMissingTypes(path, enclosingPackage, referencedType)) {
// All good!
return;
}
String minimalQualifiedName = findMinimalQualifiedName(path, enclosingPackage, referencedType);
// TODO(jkeljo): Clearer message
trees.printMessage(messageKind, String.format("Must qualify the name: %s", minimalQualifiedName), path.getLeaf(), path.getCompilationUnit());
}
@Override
public void onConstantReferenceFound(VariableElement constant, TreePath path, Element enclosingElement) {
TypeElement constantEnclosingType = (TypeElement) constant.getEnclosingElement();
if (typeWillBeAvailable(constantEnclosingType)) {
// All good!
return;
}
// TODO(jkeljo): Clearer message
trees.printMessage(messageKind, String.format("Must inline the constant value: %s", constant.getConstantValue()), path.getLeaf(), path.getCompilationUnit());
}
private boolean typeWillBeAvailable(TypeElement type) {
return isCompiledInCurrentRun(type) || isOnBootClasspath(type);
}
private boolean isCompiledInCurrentRun(TypeElement typeElement) {
return trees.getPath(typeElement) != null;
}
private boolean isOnBootClasspath(TypeElement typeElement) {
return bootClasspathOracle.isOnBootClasspath(elements.getBinaryName(typeElement).toString());
}
private boolean referenceIsLegalForMissingTypes(TreePath path, PackageElement enclosingPackage, TypeElement referencedTypeElement) {
return isImported(referencedTypeElement) || isTopLevelTypeInPackage(referencedTypeElement, enclosingPackage) || isFullyQualified(path, referencedTypeElement);
}
private boolean isImported(TypeElement referencedTypeElement) {
return importedTypes.contains(referencedTypeElement);
}
private boolean isTopLevelTypeInPackage(TypeElement referencedTypeElement, PackageElement enclosingPackage) {
return enclosingPackage == referencedTypeElement.getEnclosingElement();
}
private boolean isFullyQualified(TreePath path, TypeElement referencedTypeElement) {
return referencedTypeElement.getQualifiedName().contentEquals(TreeBackedTrees.treeToName(path.getLeaf()));
}
private String findMinimalQualifiedName(TreePath path, PackageElement enclosingPackage, TypeElement typeElement) {
List<QualifiedNameable> enclosingElements = new ArrayList<>();
QualifiedNameable walker = typeElement;
while (walker.getKind() != ElementKind.PACKAGE && !referenceIsLegalForMissingTypes(path, enclosingPackage, (TypeElement) walker)) {
enclosingElements.add(walker);
walker = (QualifiedNameable) walker.getEnclosingElement();
}
enclosingElements.add(walker);
StringBuilder resultBuilder = new StringBuilder();
for (int i = enclosingElements.size() - 1; i >= 0; i--) {
QualifiedNameable element = enclosingElements.get(i);
if (element.getKind() == ElementKind.PACKAGE) {
resultBuilder.append(element.getQualifiedName());
} else {
resultBuilder.append(element.getSimpleName());
}
if (i > 0) {
resultBuilder.append(".");
}
}
return resultBuilder.toString();
}
}).findReferences(compilationUnits);
}
}
use of javax.lang.model.element.VariableElement in project buck by facebook.
the class MoreElements method isSourceRetention.
public static boolean isSourceRetention(AnnotationMirror annotation) {
DeclaredType annotationType = annotation.getAnnotationType();
TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
AnnotationMirror retentionAnnotation = findAnnotation("java.lang.annotation.Retention", annotationTypeElement);
if (retentionAnnotation == null) {
return false;
}
VariableElement retentionPolicy = (VariableElement) Preconditions.checkNotNull(findAnnotationValue(retentionAnnotation, "value"));
return retentionPolicy.getSimpleName().contentEquals("SOURCE");
}
use of javax.lang.model.element.VariableElement in project buck by facebook.
the class TreeBackedExecutableElementTest method testGetParameters.
@Test
public void testGetParameters() throws IOException {
compile(Joiner.on('\n').join("abstract class Foo {", " public abstract void foo(String s, int i);", "}"));
VariableElement sParameter = findParameter("s", findMethod("foo", elements.getTypeElement("Foo")));
VariableElement iParameter = findParameter("i", findMethod("foo", elements.getTypeElement("Foo")));
assertSameType(elements.getTypeElement("java.lang.String").asType(), sParameter.asType());
assertSameType(types.getPrimitiveType(TypeKind.INT), iParameter.asType());
}
use of javax.lang.model.element.VariableElement in project buck by facebook.
the class TreeBackedVariableElementTest method testGetConstantValue.
@Test
public void testGetConstantValue() throws IOException {
compile(Joiner.on('\n').join("public class Foo {", " public static final int CONSTANT = 42;", "}"));
VariableElement variable = findField("CONSTANT", elements.getTypeElement("Foo"));
assertEquals(42, variable.getConstantValue());
}
use of javax.lang.model.element.VariableElement 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 };
}
Aggregations