private static Symbol findSymbol(VisitorState state, String className, String symbolToString) { Symbol classSymbol = findSymbol(state, className); for (Symbol symbol : classSymbol.getEnclosedElements()) { if (symbolToString.equals(symbol.toString())) { return symbol; } } throw new IllegalStateException("couldn't find symbol " + className + "." + symbolToString); }
/** * Validates a {@code @RestrictedApi} annotation and that the declared restriction makes sense. * * <p>The other match functions in this class check the <em>usages</em> of a restricted API. */ @Override public Description matchAnnotation(AnnotationTree tree, VisitorState state) { // TODO(bangert): Validate all the fields if (!ASTHelpers.getSymbol(tree).toString().equals(RestrictedApi.class.getName())) { return Description.NO_MATCH; } // TODO(bangert): make a more elegant API to get the annotation within an annotation tree. // Maybe find the declared object and get annotations on that... AnnotationMirror restrictedApi = ASTHelpers.getAnnotationMirror(tree); if (restrictedApi == null) { return Description.NO_MATCH; } return Description.NO_MATCH; }
@Override public Void visitVariable(VariableTree tree, Void unused) { Symbol symbol = ASTHelpers.getSymbol(tree); if (!symbol.getKind().equals(ElementKind.PARAMETER)) { return super.visitVariable(tree, unused); } String variableName = symbol.toString(); Symbol matchedField = fields.get(Ascii.toLowerCase(variableName)); if (matchedField != null) { String fieldName = matchedField.toString(); if (!variableName.equals(fieldName)) { matchedParameters.put(getCurrentPath(), matchedField); } } return super.visitVariable(tree, unused); } }
@Override public boolean matches(ExpressionTree expressionTree, VisitorState state) { Symbol sym = ASTHelpers.getSymbol(expressionTree); if (sym == null) { return false; } if (!(sym instanceof MethodSymbol)) { throw new IllegalArgumentException( "DescendantOf matcher expects a method call but found " + sym.getClass() + ". Expression: " + expressionTree); } if (sym.isStatic()) { return false; } if (methodName.equals(sym.toString())) { Type accessedReferenceType = sym.owner.type; Type collectionType = state.getTypeFromString(fullClassName); if (collectionType != null) { return state .getTypes() .isSubtype(accessedReferenceType, state.getTypes().erasure(collectionType)); } } return false; } }
Set<Symbol> difference = new HashSet<>(Sets.difference(fieldsInHashCode, fieldsInEquals)); difference.removeIf(f -> Ascii.toLowerCase(f.toString()).contains("hash")); String message = String.format(MESSAGE, difference);
.collect( toImmutableMap( symbol -> Ascii.toLowerCase(symbol.toString()), x -> x, (x, y) -> x)); ImmutableMap<TreePath, Symbol> matchedParameters = MatchingParametersScanner.findMatchingParameters(fieldNamesMap, state.getPath());
return StaticImportInfo.create(access.toString(), getOnlyElement(members).toString());
private void getRootRelativeJavaName(StringBuilder sb, Symbol symbol) { if (!isRootPackage(symbol)) { if (sb.length() > 0 && !"".equals(symbol.toString())) { sb.insert(0, "."); } String name = symbol.getSimpleName().toString(); sb.insert(0, name); symbol = (symbol instanceof PackageSymbol) ? ((PackageSymbol) symbol).owner : symbol.getEnclosingElement(); if (symbol != null) { getRootRelativeJavaName(sb, symbol); } } }
private void getRootRelativeName(Map<Symbol, String> nameMapping, StringBuilder sb, Symbol symbol) { if (useModules && symbol instanceof PackageSymbol && !symbol.toString().startsWith(JSweetConfig.LIBS_PACKAGE + ".")) { return; } if (!isRootPackage(symbol)) { if (sb.length() > 0 && !"".equals(symbol.toString())) { sb.insert(0, "."); } String name = symbol.getSimpleName().toString(); if (nameMapping != null && nameMapping.containsKey(symbol)) { name = nameMapping.get(symbol); } else { if (hasAnnotationType(symbol, JSweetConfig.ANNOTATION_NAME)) { String originalName = getAnnotationValue(symbol, JSweetConfig.ANNOTATION_NAME, String.class, null); if (!isBlank(originalName)) { name = originalName; } } } sb.insert(0, name); symbol = (symbol instanceof PackageSymbol) ? ((PackageSymbol) symbol).owner : symbol.getEnclosingElement(); if (symbol != null) { getRootRelativeName(nameMapping, sb, symbol); } } }
/** * Checks that the given invocation conforms to JSweet contraints. */ public boolean checkApply(JCMethodInvocation invocation, MethodSymbol methSym) { // if (Util.hasAnnotationType(methSym, JSweetConfig.ANNOTATION_ERASED)) // { // translator.report(invocation, JSweetProblem.ERASED_METHOD, methSym); // } if (!JSweetConfig.isJDKReplacementMode() && !jdkAllowed) { if (methSym.owner.toString().startsWith("java.")) { if (invocation.meth instanceof JCFieldAccess && "super".equals(((JCFieldAccess) invocation.meth).selected.toString())) { translator.report(invocation, JSweetProblem.JDK_METHOD, methSym); return false; } if (translator.getContext().strictMode || AUTHORIZED_OBJECT_METHODS.contains(methSym.name.toString())) { return true; } if (methSym.owner.toString().equals(String.class.getName()) && AUTHORIZED_STRING_METHODS.contains(methSym.toString())) { return true; } translator.report(invocation, JSweetProblem.JDK_METHOD, methSym); return false; } } return true; }
String signature = symbol.toString(); if (symbol.getEnclosingElement() != null) { signature = symbol.getEnclosingElement().getQualifiedName().toString() + "." + signature;
String signature = symbol.toString(); if (!(symbol instanceof TypeSymbol) && symbol.getEnclosingElement() != null) { signature = symbol.getEnclosingElement().getQualifiedName().toString() + "." + signature;
/** * 获取类的包名 * * @return 返回类的包名 */ public String getPackageName() { return classSym.owner.toString(); }
/** A Java source description of the location of this symbol; used for * error reporting. Use of this method may result in the loss of the * symbol's description. */ public String location() { if (owner.name == null || (owner.name.len == 0 && owner.kind != PCK)) { return ""; } return owner.toString(); }
@Override public JType visitIdentifier(IdentifierTree it, Void __) { JCIdent idt = (JCIdent) it; return codeModel.ref(idt.sym.toString()); }
/** * Given a {@link MethodSymbol}, returns a method name in the form "owningClass#methodName", e.g. * "java.util.function.Function#apply". */ private static String getMethodName(MethodSymbol methodSym) { return String.format("%s#%s", methodSym.owner.toString(), methodSym.getSimpleName()); }
@Override public Description matchNewClass(final NewClassTree tree, final VisitorState state) { final Symbol sym = ASTHelpers.getSymbol(tree.getIdentifier()); if (sym != null && sym.toString().equals("java.security.SecureRandom")) { return describeMatch(tree); } return Description.NO_MATCH; } }
if (Object.class.getName().equals(s.getEnclosingElement().toString())) { s = null;
@Override public int hashCode() { VarSymbol vs = (VarSymbol) element; return HashCodeUtils.hash( vs.name.toString(), TypeAnnotationUtils.unannotatedType(vs.type).toString(), vs.owner.toString()); }
(MethodType) invocationElement.getMethod().asType()); if (methSym != null && (Object.class.getName().equals(methSym.getEnclosingElement().toString()) || methSym.getEnclosingElement().isInterface()) || invocationElement.getTargetExpression().getTypeAsElement().getKind() == ElementKind.INTERFACE