private static boolean isMapMethod(Symbol.MethodSymbol symbol, Types types, String methodName) { if (!symbol.getSimpleName().toString().equals(methodName)) { return false; } Symbol owner = symbol.owner; if (owner.getQualifiedName().toString().equals("java.util.Map")) { return true; } com.sun.tools.javac.util.List<Type> supertypes = types.closure(owner.type); for (Type t : supertypes) { if (t.asElement().getQualifiedName().toString().equals("java.util.Map")) { return true; } } return false; }
@Override public Violation visitType(Type type, Void s) { switch (type.tsym.getKind()) { case ANNOTATION_TYPE: break; default: throw new AssertionError(String.format("Unexpected type kind %s", type.tsym.getKind())); return threadSafeInstantiation(containerTypeParameters, annotation, type); String nameStr = type.tsym.flatName().toString(); if (knownTypes.getKnownUnsafeClasses().contains(nameStr)) { return Violation.of( String.format( "'%s' is %s", type.tsym.getSimpleName(), purpose.mutableOrNotThreadSafe())); String.format("'%s' is a mutable proto message", type.tsym.getSimpleName()));
/** * Finds all methods in any superclass of {@code startClass} with a certain {@code name} that * match the given {@code predicate}. * * @return The (possibly empty) set of methods in any superclass that match {@code predicate} and * have the given {@code name}. */ public static Set<MethodSymbol> findMatchingMethods( Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) { Filter<Symbol> matchesMethodPredicate = sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym); Set<MethodSymbol> matchingMethods = new HashSet<>(); // Iterate over all classes and interfaces that startClass inherits from. for (Type superClass : types.closure(startClass)) { // Iterate over all the methods declared in superClass. TypeSymbol superClassSymbol = superClass.tsym; Scope superClassSymbols = superClassSymbol.members(); if (superClassSymbols != null) { // Can be null if superClass is a type variable for (Symbol symbol : superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) { // By definition of the filter, we know that the symbol is a MethodSymbol. matchingMethods.add((MethodSymbol) symbol); } } } return matchingMethods; }
return cls; ClassType G = (ClassType)cls.asElement().asType(); List<Type> A = G.getTypeArguments(); List<Type> T = cls.getTypeArguments();
return cls; ClassType G = (ClassType)cls.asElement().asType(); List<Type> A = G.getTypeArguments(); List<Type> T = cls.getTypeArguments();
return cls; ClassType G = (ClassType)cls.asElement().asType(); List<Type> A = G.getTypeArguments(); List<Type> T = cls.getTypeArguments();
return cls; ClassType G = (ClassType)cls.asElement().asType(); List<Type> A = G.getTypeArguments(); List<Type> T = cls.getTypeArguments();
public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) { boolean isAccessible = false; switch ((short)(c.flags() & AccessFlags)) { case PRIVATE: isAccessible = env.toplevel.packge == c.packge() || env.toplevel.packge == c.packge() || isInnerSubClass(env.enclClass.sym, c.owner);
if (otc.owner.kind != PCK && !otc.hasOuterInstance()) { chk.earlyRefError(pos, c);
if (otc.owner.kind != PCK && !otc.hasOuterInstance()) { chk.earlyRefError(pos, c); assert false; // should have been caught in Attr
if (otc.owner.kind != PCK && !otc.hasOuterInstance()) { chk.earlyRefError(pos, c);
public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) { boolean isAccessible = false; switch ((short)(c.flags() & AccessFlags)) { case PRIVATE: isAccessible = env.toplevel.packge == c.packge() || env.toplevel.packge == c.packge() || isInnerSubClass(env.enclClass.sym, c.owner);
if (otc.owner.kind != PCK && !otc.hasOuterInstance()) { chk.earlyRefError(pos, c); assert false; // should have been caught in Attr
&& ASTHelpers.getType(tree.getExtendsClause()).tsym.hasOuterInstance()) { return NO_MATCH;
@Override public void visitMethodDef(JCMethodDecl tree) { if (tree.name == names.init && (currentClass.isInner() || currentClass.isLocal())) { List<Pair<TypeSymbol, Symbol>> prevOuterThisStack = outerThisStack; try { if (currentClass.hasOuterInstance()) outerThisDef(tree.sym); super.visitMethodDef(tree);
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } if (state.getTypes().closure(ASTHelpers.getSymbol(tree).enclClass().asType()).stream() .anyMatch( s -> s.asElement() .packge() .getQualifiedName() .toString() .startsWith("java.util.concurrent"))) { return NO_MATCH; } if (blockEndsInBreakOrReturn(state)) { return NO_MATCH; } ExpressionTree collection = getReceiver(tree); if (collection == null) { return NO_MATCH; } if (!enclosingLoop(state.getPath(), collection)) { return NO_MATCH; } return describeMatch(tree); }
/** Find an identifier in a package which matches a specified kind set. * @param env The current environment. * @param name The identifier's name. * @param kind Indicates the possible symbol kinds * (a nonempty subset of TYP, PCK). */ Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck, Name name, int kind) { Name fullname = TypeSymbol.formFullName(name, pck); Symbol bestSoFar = typeNotFound; PackageSymbol pack = null; if ((kind & PCK) != 0) { pack = reader.enterPackage(fullname); if (pack.exists()) return pack; } if ((kind & TYP) != 0) { Symbol sym = loadClass(env, fullname); if (sym.exists()) { // don't allow programs to use flatnames if (name == sym.name) return sym; } else if (sym.kind < bestSoFar.kind) bestSoFar = sym; } return (pack != null) ? pack : bestSoFar; }
private void checkThis(DiagnosticPosition pos, TypeSymbol c) { if (checkThis && currentClass != c) { List<Pair<TypeSymbol, Symbol>> ots = outerThisStack; if (ots.isEmpty()) { log.error(pos, "no.encl.instance.of.type.in.scope", c); //NOI18N return; } Pair<TypeSymbol, Symbol> ot = ots.head; TypeSymbol otc = ot.fst; while (otc != c) { do { ots = ots.tail; if (ots.isEmpty()) { log.error(pos, "no.encl.instance.of.type.in.scope", c); //NOI18N return; } ot = ots.head; } while (ot.snd != otc); if (otc.owner.kind != Kinds.Kind.PCK && !otc.hasOuterInstance()) { log.error(pos, "cant.ref.before.ctor.called", c); //NOI18N return; } otc = ot.fst; } } }