use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.
the class TreeBuilder method buildIteratorMethodAccess.
/**
* Builds an AST Tree to access the iterator() method of some iterable
* expression.
*
* @param iterableExpr an expression whose type is a subtype of Iterable
* @return a MemberSelectTree that accesses the iterator() method of
* the expression
*/
public MemberSelectTree buildIteratorMethodAccess(ExpressionTree iterableExpr) {
DeclaredType exprType = (DeclaredType) TypesUtils.upperBound(InternalUtils.typeOf(iterableExpr));
assert exprType != null : "expression must be of declared type Iterable<>";
TypeElement exprElement = (TypeElement) exprType.asElement();
// Find the iterator() method of the iterable type
Symbol.MethodSymbol iteratorMethod = null;
for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(exprElement))) {
Name methodName = method.getSimpleName();
if (method.getParameters().size() == 0) {
if (methodName.contentEquals("iterator")) {
iteratorMethod = (Symbol.MethodSymbol) method;
}
}
}
assert iteratorMethod != null : "no iterator method declared for expression type";
Type.MethodType methodType = (Type.MethodType) iteratorMethod.asType();
Symbol.TypeSymbol methodClass = methodType.asElement();
DeclaredType iteratorType = (DeclaredType) methodType.getReturnType();
TypeMirror elementType;
if (iteratorType.getTypeArguments().size() > 0) {
elementType = iteratorType.getTypeArguments().get(0);
// Remove captured type from a wildcard.
if (elementType instanceof Type.CapturedType) {
elementType = ((Type.CapturedType) elementType).wildcard;
}
iteratorType = modelTypes.getDeclaredType((TypeElement) modelTypes.asElement(iteratorType), elementType);
}
// Replace the iterator method's generic return type with
// the actual element type of the expression.
Type.MethodType updatedMethodType = new Type.MethodType(com.sun.tools.javac.util.List.<Type>nil(), (Type) iteratorType, com.sun.tools.javac.util.List.<Type>nil(), methodClass);
JCTree.JCFieldAccess iteratorAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) iterableExpr, iteratorMethod);
iteratorAccess.setType(updatedMethodType);
return iteratorAccess;
}
use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.
the class AnnotationUtils method fromName.
/**
* Creates an {@link AnnotationMirror} given by a particular
* fully-qualified name. getElementValues on the result returns an
* empty map.
*
* @param elements the element utilities to use
* @param name the name of the annotation to create
* @return an {@link AnnotationMirror} of type {@code} name
*/
public static AnnotationMirror fromName(Elements elements, CharSequence name) {
if (annotationsFromNames.containsKey(name))
return annotationsFromNames.get(name);
final DeclaredType annoType = typeFromName(elements, name);
if (annoType == null)
return null;
if (annoType.asElement().getKind() != ElementKind.ANNOTATION_TYPE) {
ErrorReporter.errorAbort(annoType + " is not an annotation");
// dead code
return null;
}
AnnotationMirror result = new AnnotationMirror() {
String toString = "@" + annoType;
@Override
public DeclaredType getAnnotationType() {
return annoType;
}
@Override
public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValues() {
return Collections.emptyMap();
}
/*@SideEffectFree*/
@Override
public String toString() {
return toString;
}
};
annotationsFromNames.put(name, result);
return result;
}
use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.
the class TreeBuilder method buildHasNextMethodAccess.
/**
* Builds an AST Tree to access the hasNext() method of an iterator.
*
* @param iteratorExpr an expression whose type is a subtype of Iterator
* @return a MemberSelectTree that accesses the hasNext() method of
* the expression
*/
public MemberSelectTree buildHasNextMethodAccess(ExpressionTree iteratorExpr) {
DeclaredType exprType = (DeclaredType) InternalUtils.typeOf(iteratorExpr);
assert exprType != null : "expression must be of declared type Iterator<>";
TypeElement exprElement = (TypeElement) exprType.asElement();
// Find the hasNext() method of the iterator type
Symbol.MethodSymbol hasNextMethod = null;
for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(exprElement))) {
Name methodName = method.getSimpleName();
if (method.getParameters().size() == 0) {
if (methodName.contentEquals("hasNext")) {
hasNextMethod = (Symbol.MethodSymbol) method;
}
}
}
assert hasNextMethod != null : "no hasNext method declared for expression type";
JCTree.JCFieldAccess hasNextAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) iteratorExpr, hasNextMethod);
hasNextAccess.setType(hasNextMethod.asType());
return hasNextAccess;
}
use of javax.lang.model.type.DeclaredType in project bazel by bazelbuild.
the class TreeBuilder method buildPrimValueMethodAccess.
/**
* Builds an AST Tree to access the *Value() method of a
* boxed type such as Short or Float, where * is the corresponding
* primitive type (i.e. shortValue or floatValue).
*
* @param expr an expression whose type is a boxed type
* @return a MemberSelectTree that accesses the *Value() method of
* the expression
*/
public MemberSelectTree buildPrimValueMethodAccess(Tree expr) {
TypeMirror boxedType = InternalUtils.typeOf(expr);
TypeElement boxedElement = (TypeElement) ((DeclaredType) boxedType).asElement();
assert TypesUtils.isBoxedPrimitive(boxedType);
TypeMirror unboxedType = modelTypes.unboxedType(boxedType);
// Find the *Value() method of the boxed type
String primValueName = unboxedType.toString() + "Value";
Symbol.MethodSymbol primValueMethod = null;
for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers(boxedElement))) {
Name methodName = method.getSimpleName();
if (methodName.contentEquals(primValueName) && method.getParameters().size() == 0) {
primValueMethod = (Symbol.MethodSymbol) method;
}
}
assert primValueMethod != null : "no *Value method declared for boxed type";
Type.MethodType methodType = (Type.MethodType) primValueMethod.asType();
JCTree.JCFieldAccess primValueAccess = (JCTree.JCFieldAccess) maker.Select((JCTree.JCExpression) expr, primValueMethod);
primValueAccess.setType(methodType);
return primValueAccess;
}
use of javax.lang.model.type.DeclaredType in project buck by facebook.
the class AccessFlags method isDeprecated.
private static boolean isDeprecated(Element element) {
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
DeclaredType annotationType = annotationMirror.getAnnotationType();
TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
// compilation.
if (annotationTypeElement.getQualifiedName().contentEquals("java.lang.Deprecated")) {
return true;
}
}
return false;
}
Aggregations