use of com.sun.tools.javac.code.Type.ClassType in project Rocket by mozilla-tw.
the class ProcessorUtil method findClassValuesFromAnnotationOnClassAsNames.
Set<String> findClassValuesFromAnnotationOnClassAsNames(Element clazz, Class<? extends Annotation> annotationClass) {
String annotationClassName = annotationClass.getName();
AnnotationValue excludedModuleAnnotationValue = null;
for (AnnotationMirror annotationMirror : clazz.getAnnotationMirrors()) {
// instead. This check is necessary because a given class may have multiple Annotations.
if (!annotationClassName.equals(annotationMirror.getAnnotationType().toString())) {
continue;
}
Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values = annotationMirror.getElementValues().entrySet();
// (usually value).
if (values.size() != 1) {
throw new IllegalArgumentException("Expected single value, but found: " + values);
}
excludedModuleAnnotationValue = values.iterator().next().getValue();
if (excludedModuleAnnotationValue == null) {
throw new NullPointerException("Failed to find Excludes#value");
}
}
if (excludedModuleAnnotationValue == null) {
return Collections.emptySet();
}
Object value = excludedModuleAnnotationValue.getValue();
if (value instanceof List) {
List values = (List) value;
Set<String> result = new HashSet<>(values.size());
for (Object current : values) {
Attribute.Class currentClass = (Attribute.Class) current;
result.add(currentClass.getValue().toString());
}
return result;
} else {
ClassType classType = (ClassType) value;
return Collections.singleton(classType.toString());
}
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class MutableMethodReturnType method matchMethod.
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree);
if (methodSymbol.isConstructor()) {
return Description.NO_MATCH;
}
if (isMethodCanBeOverridden(methodSymbol, state)) {
return Description.NO_MATCH;
}
if (ANNOTATED_WITH_PRODUCES_OR_PROVIDES.matches(methodTree, state)) {
return Description.NO_MATCH;
}
Type returnType = methodSymbol.getReturnType();
if (ImmutableCollections.isImmutableType(returnType)) {
return Description.NO_MATCH;
}
ImmutableSet<ClassType> returnStatementsTypes = getMethodReturnTypes(methodTree);
if (returnStatementsTypes.isEmpty()) {
return Description.NO_MATCH;
}
boolean alwaysReturnsImmutableType = returnStatementsTypes.stream().allMatch(ImmutableCollections::isImmutableType);
if (!alwaysReturnsImmutableType) {
return Description.NO_MATCH;
}
Optional<String> immutableReturnType = ImmutableCollections.mutableToImmutable(getTypeQualifiedName(returnType));
if (!immutableReturnType.isPresent()) {
immutableReturnType = getCommonImmutableTypeForAllReturnStatementsTypes(returnStatementsTypes);
}
if (!immutableReturnType.isPresent()) {
return Description.NO_MATCH;
}
Type newReturnType = state.getTypeFromString(immutableReturnType.get());
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
fixBuilder.replace(getTypeTree(methodTree.getReturnType()), SuggestedFixes.qualifyType(state, fixBuilder, newReturnType.asElement()));
SuggestedFix fix = fixBuilder.build();
return describeMatch(methodTree.getReturnType(), fix);
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class MutableMethodReturnType method getImmutableSuperTypesForClassType.
private static ImmutableList<String> getImmutableSuperTypesForClassType(ClassType classType) {
ImmutableList.Builder<String> immutableSuperTypes = ImmutableList.builder();
ClassType superType = classType;
while (superType.supertype_field instanceof ClassType) {
if (ImmutableCollections.isImmutableType(superType)) {
immutableSuperTypes.add(getTypeQualifiedName(superType.asElement().type));
}
superType = (ClassType) superType.supertype_field;
}
return immutableSuperTypes.build();
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class FindIdentifiers method findIdent.
/**
* Finds a declaration with the given name and type that is in scope at the current location.
*/
public static Symbol findIdent(String name, VisitorState state, KindSelector kind) {
ClassType enclosingClass = ASTHelpers.getType(state.findEnclosing(ClassTree.class));
if (enclosingClass == null || enclosingClass.tsym == null) {
return null;
}
Env<AttrContext> env = Enter.instance(state.context).getClassEnv(enclosingClass.tsym);
MethodTree enclosingMethod = state.findEnclosing(MethodTree.class);
if (enclosingMethod != null) {
env = MemberEnter.instance(state.context).getMethodEnv((JCMethodDecl) enclosingMethod, env);
}
try {
Method method = Resolve.class.getDeclaredMethod("findIdent", Env.class, Name.class, KindSelector.class);
method.setAccessible(true);
Symbol result = (Symbol) method.invoke(Resolve.instance(state.context), env, state.getName(name), kind);
return result.exists() ? result : null;
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class FindIdentifiers method findAllIdents.
/**
* Finds the set of all bare variable identifiers in scope at the current location. Identifiers
* are ordered by ascending distance/scope count from the current location to match shadowing
* rules. That is, if two variables with the same simple names appear in the set, the one that
* appears first in iteration order is the one you get if you use the bare name in the source
* code.
*
* <p>We do not report variables that would require a qualfied access. We also do not handle
* wildcard imports.
*/
public static LinkedHashSet<VarSymbol> findAllIdents(VisitorState state) {
ImmutableSet.Builder<VarSymbol> result = new ImmutableSet.Builder<>();
Tree prev = state.getPath().getLeaf();
for (Tree curr : state.getPath().getParentPath()) {
switch(curr.getKind()) {
case BLOCK:
for (StatementTree stmt : ((BlockTree) curr).getStatements()) {
if (stmt.equals(prev)) {
break;
}
addIfVariable(stmt, result);
}
break;
case METHOD:
for (VariableTree param : ((MethodTree) curr).getParameters()) {
result.add(ASTHelpers.getSymbol(param));
}
break;
case CATCH:
result.add(ASTHelpers.getSymbol(((CatchTree) curr).getParameter()));
break;
case CLASS:
case INTERFACE:
case ENUM:
case ANNOTATION_TYPE:
// field is referred to by qualified name, but we don't support that.
for (Tree member : ((ClassTree) curr).getMembers()) {
if (member.equals(prev)) {
break;
}
addIfVariable(member, result);
}
// Collect inherited fields.
Type classType = ASTHelpers.getType(curr);
List<Type> classTypeClosure = state.getTypes().closure(classType);
List<Type> superTypes = classTypeClosure.size() <= 1 ? Collections.emptyList() : classTypeClosure.subList(1, classTypeClosure.size());
for (Type type : superTypes) {
Scope scope = type.tsym.members();
ImmutableList.Builder<VarSymbol> varsList = ImmutableList.builder();
for (Symbol var : scope.getSymbols(VarSymbol.class::isInstance)) {
varsList.add((VarSymbol) var);
}
result.addAll(varsList.build().reverse());
}
break;
case FOR_LOOP:
addAllIfVariable(((ForLoopTree) curr).getInitializer(), result);
break;
case ENHANCED_FOR_LOOP:
result.add(ASTHelpers.getSymbol(((EnhancedForLoopTree) curr).getVariable()));
break;
case TRY:
TryTree tryTree = (TryTree) curr;
boolean inResources = false;
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
inResources = true;
break;
}
}
if (inResources) {
// Case 1: we're in one of the resource declarations
for (Tree resource : tryTree.getResources()) {
if (resource.equals(prev)) {
break;
}
addIfVariable(resource, result);
}
} else if (tryTree.getBlock().equals(prev)) {
// Case 2: We're in the block (not a catch or finally)
addAllIfVariable(tryTree.getResources(), result);
}
break;
case COMPILATION_UNIT:
for (ImportTree importTree : ((CompilationUnitTree) curr).getImports()) {
if (importTree.isStatic() && importTree.getQualifiedIdentifier().getKind() == Kind.MEMBER_SELECT) {
MemberSelectTree memberSelectTree = (MemberSelectTree) importTree.getQualifiedIdentifier();
Scope scope = state.getTypes().membersClosure(ASTHelpers.getType(memberSelectTree.getExpression()), /* skipInterface= */
false);
for (Symbol var : scope.getSymbols(sym -> sym instanceof VarSymbol && sym.getSimpleName().equals(memberSelectTree.getIdentifier()))) {
result.add((VarSymbol) var);
}
}
}
break;
default:
// other node types don't introduce variables
break;
}
prev = curr;
}
// TODO(eaftan): switch out collector for ImmutableSet.toImmutableSet()
return result.build().stream().filter(var -> isVisible(var, state.getPath())).collect(Collectors.toCollection(LinkedHashSet::new));
}
Aggregations