use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class DeclarationsIntoElements method storeMethod.
/**
* Add inherited declaration annotations from overridden methods into the corresponding Elements
* so they are written into bytecode.
*
* @param env ProcessingEnvironment
* @param atypeFactory the type factory
* @param meth the MethodTree to add the annotations
*/
private static void storeMethod(ProcessingEnvironment env, AnnotatedTypeFactory atypeFactory, MethodTree meth) {
ExecutableElement element = TreeUtils.elementFromDeclaration(meth);
MethodSymbol sym = (MethodSymbol) element;
java.util.List<? extends AnnotationMirror> elementAnnos = element.getAnnotationMirrors();
Set<AnnotationMirror> declAnnotations = atypeFactory.getDeclAnnotations(sym);
List<Compound> tcs = List.nil();
for (AnnotationMirror anno : declAnnotations) {
// Only add the annotation if it isn't in the Element already.
if (!AnnotationUtils.containsSame(elementAnnos, anno)) {
tcs = tcs.append(TypeAnnotationUtils.createCompoundFromAnnotationMirror(anno, env));
}
}
sym.appendAttributes(tcs);
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project checker-framework by typetools.
the class DefaultReflectionResolver method getConstructorSymbolsfor.
/**
* Get set of Symbols for constructors based on class name and parameter length.
*
* @return the (potentially empty) set of corresponding constructor Symbol(s)
*/
private List<Symbol> getConstructorSymbolsfor(String className, int paramLength, Env<AttrContext> env) {
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
Resolve resolve = Resolve.instance(context);
Names names = Names.instance(context);
Symbol symClass = getSymbol(className, env, names, resolve);
if (!symClass.exists()) {
debugReflection("Unable to resolve class: " + className);
return Collections.emptyList();
}
// TODO: Should this be used instead of the below??
ElementFilter.constructorsIn(symClass.getEnclosedElements());
// The common case is probably that `result` is a singleton at method exit.
List<Symbol> result = new ArrayList<>();
for (Symbol s : symClass.getEnclosedElements()) {
// Check all constructors
if (s.getKind() == ElementKind.CONSTRUCTOR) {
// Check for number of parameters
if (((MethodSymbol) s).getParameters().size() == paramLength) {
result.add(s);
}
}
}
if (result.isEmpty()) {
debugReflection("Unable to resolve constructor!");
}
return result;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class SizeGreaterThanOrEqualsZero method isProtoRepeatedFieldCountMethod.
private static boolean isProtoRepeatedFieldCountMethod(ExpressionTree tree, VisitorState state) {
// Instance method, on proto class, named `get<Field>Count`.
if (!PROTO_METHOD_NAMED_GET_COUNT.matches(tree, state)) {
return false;
}
// Make sure it's the count method for a repeated field, not the get method for a non-repeated
// field named <something>_count, by checking for other methods on the repeated field.
MethodSymbol methodCallSym = getSymbol((MethodInvocationTree) tree);
if (methodCallSym == null) {
return false;
}
Scope protoClassMembers = methodCallSym.owner.members();
java.util.regex.Matcher getCountRegexMatcher = PROTO_COUNT_METHOD_PATTERN.matcher(methodCallSym.getSimpleName().toString());
if (!getCountRegexMatcher.matches()) {
return false;
}
String fieldName = getCountRegexMatcher.group(1);
return protoClassMembers.findFirst(state.getName("get" + fieldName + "List")) != null;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class ReferenceEquality method inEqualsOrCompareTo.
private boolean inEqualsOrCompareTo(Type classType, Type type, VisitorState state) {
MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
if (methodTree == null) {
return false;
}
MethodSymbol sym = ASTHelpers.getSymbol(methodTree);
if (sym == null || sym.isStatic()) {
return false;
}
Symbol compareTo = getOnlyMember(state, state.getSymtab().comparableType, "compareTo");
Symbol equals = getOnlyMember(state, state.getSymtab().objectType, "equals");
if (!sym.overrides(compareTo, classType.tsym, state.getTypes(), /* checkResult= */
false) && !sym.overrides(equals, classType.tsym, state.getTypes(), /* checkResult= */
false)) {
return false;
}
if (!ASTHelpers.isSameType(type, classType, state)) {
return false;
}
return true;
}
use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.
the class RestrictedApiChecker method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
RestrictedApi annotation = ASTHelpers.getAnnotation(tree, RestrictedApi.class);
if (annotation != null) {
return checkRestriction(annotation, tree, state);
}
MethodSymbol methSymbol = ASTHelpers.getSymbol(tree);
if (methSymbol == null) {
// This shouldn't happen, but has. (See b/33758055)
return Description.NO_MATCH;
}
// Try each super method for @RestrictedApi
Optional<MethodSymbol> superWithRestrictedApi = ASTHelpers.findSuperMethods(methSymbol, state.getTypes()).stream().filter((t) -> ASTHelpers.hasAnnotation(t, RestrictedApi.class, state)).findFirst();
if (!superWithRestrictedApi.isPresent()) {
return Description.NO_MATCH;
}
return checkRestriction(ASTHelpers.getAnnotation(superWithRestrictedApi.get(), RestrictedApi.class), tree, state);
}
Aggregations