/** * Pretty-prints a method signature for use in diagnostics. * * <p>Uses simple names for declared types, and omitting formal type parameters and the return * type since they do not affect overload resolution. */ public static String prettyMethodSignature(ClassSymbol origin, MethodSymbol m) { StringBuilder sb = new StringBuilder(); if (!m.owner.equals(origin)) { sb.append(m.owner.getSimpleName()).append('.'); } sb.append(m.isConstructor() ? origin.getSimpleName() : m.getSimpleName()).append('('); sb.append( m.getParameters().stream() .map(v -> v.type.accept(PRETTY_TYPE_VISITOR, null)) .collect(joining(", "))); sb.append(')'); return sb.toString(); }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { if (!EQUALS.matches(node, state)) { return null; } ExpressionTree argument = getOnlyElement(node.getArguments()); ExpressionTree receiver = getReceiver(node); if (receiver == null) { return null; } if (matchesEitherWay(argument, receiver, THIS_CLASS, otherClass)) { matchedGetClass = true; String replacement = String.format( "%s instanceof %s", parameter.getSimpleName(), classSymbol.getSimpleName()); if (getCurrentPath().getParentPath().getLeaf() instanceof UnaryTree) { replacement = String.format("(%s)", replacement); } fix.replace(node, replacement); } return super.visitMethodInvocation(node, null); }
private void fixQualifier(Tree tree, ExpressionTree qualifierExpression) { if (sym.equals(ASTHelpers.getSymbol(tree))) { builder.replace(qualifierExpression, sym.owner.enclClass().getSimpleName().toString()); } } }.scan(state.getPath().getCompilationUnit(), null);
? "implements" : "overrides", override.enclClass().getSimpleName())) .build();
@Override public Void visitBinary(BinaryTree binaryTree, Void unused) { if (binaryTree.getKind() != Kind.NOT_EQUAL_TO && binaryTree.getKind() != Kind.EQUAL_TO) { return super.visitBinary(binaryTree, null); } if (matchesEitherWay(binaryTree, isParameter, (tree, s) -> matchesNull(tree))) { if (binaryTree.getKind() == Kind.NOT_EQUAL_TO) { makeAlwaysTrue(); } if (binaryTree.getKind() == Kind.EQUAL_TO) { makeAlwaysFalse(); } return null; } if (matchesEitherWay(binaryTree, THIS_CLASS, otherClass)) { matchedGetClass = true; String instanceOf = String.format( "%s instanceof %s", parameter.getSimpleName().toString(), classSymbol.getSimpleName().toString()); if (binaryTree.getKind() == Kind.EQUAL_TO) { fix.replace(binaryTree, instanceOf); } if (binaryTree.getKind() == Kind.NOT_EQUAL_TO) { fix.replace(binaryTree, String.format("!(%s)", instanceOf)); } } return super.visitBinary(binaryTree, null); }
private Pair<Symbol.ClassSymbol, JCTree.JCCompilationUnit> getClassSymbolForProducedClass( String fqn, BasicJavacTask[] task ) { StringWriter errors = new StringWriter(); // need javac with ManifoldJavaFileManager because the produced class must come from manifold task[0] = getJavacTask_ManFileMgr(); Symbol.ClassSymbol e = IDynamicJdk.instance().getTypeElement( task[0].getContext(), null, fqn ); if( e != null && e.getSimpleName().contentEquals( ManClassUtil.getShortClassName( fqn ) ) ) { JavacTrees trees = JavacTrees.instance( task[0].getContext() ); TreePath path = trees.getPath( e ); if( path != null ) { return new Pair<>( e, (JCTree.JCCompilationUnit)path.getCompilationUnit() ); } else { // TreePath is only applicable to a source file; // if fqn is not a source file, there is no compilation unit available return new Pair<>( e, null ); } } StringBuffer errorText = errors.getBuffer(); if( errorText.length() > 0 ) { throw new RuntimeException( "Compile errors:\n" + errorText ); } return null; }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree); if (isSynchronized(methodSymbol)) { return NO_MATCH; } for (MethodSymbol s : ASTHelpers.findSuperMethods(methodSymbol, state.getTypes())) { if (isSynchronized(s)) { // Input streams are typically not used across threads, so this case isn't // worth enforcing. if (isSameType(s.owner.type, state.getTypeFromString("java.io.InputStream"), state)) { continue; } if (ignore(methodTree, state)) { return NO_MATCH; } return buildDescription(methodTree) .addFix(SuggestedFixes.addModifiers(methodTree, state, Modifier.SYNCHRONIZED)) .setMessage( String.format( "Unsynchronized method %s overrides synchronized method in %s", methodSymbol.getSimpleName(), s.enclClass().getSimpleName())) .build(); } } return NO_MATCH; }
/** * Resolves a simple name as a type. Considers super classes, lexically enclosing classes, and * then arbitrary types available in the current environment. */ private Symbol resolveType(String name, SearchSuperTypes searchSuperTypes) { Symbol type = null; if (searchSuperTypes == SearchSuperTypes.YES) { type = getSuperType(enclosingClass, name); } if (enclosingClass.getSimpleName().contentEquals(name)) { type = enclosingClass; } if (type == null) { type = getLexicallyEnclosing(enclosingClass, name); } if (type == null) { type = attribIdent(name); } checkGuardedBy( !(type instanceof Symbol.PackageSymbol), "All we could find for '%s' was a package symbol.", name); return type; }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { if (!EQUALS.matches(node, state)) { return null; } ExpressionTree argument = getOnlyElement(node.getArguments()); ExpressionTree receiver = getReceiver(node); if (receiver == null) { return null; } if (matchesEitherWay(argument, receiver, THIS_CLASS, otherClass)) { matchedGetClass = true; fix.replace( node, String.format( "%s instanceof %s", parameter.getSimpleName().toString(), classSymbol.getSimpleName().toString())); } return super.visitMethodInvocation(node, null); }
/** * Pretty-prints a method signature for use in diagnostics. * * <p>Uses simple names for declared types, and omitting formal type parameters and the return * type since they do not affect overload resolution. */ public static String prettyMethodSignature(ClassSymbol origin, MethodSymbol m) { StringBuilder sb = new StringBuilder(); if (!m.owner.equals(origin)) { sb.append(m.owner.getSimpleName()).append('.'); } sb.append(m.isConstructor() ? origin.getSimpleName() : m.getSimpleName()).append('('); sb.append( m.getParameters().stream() .map(v -> v.type.accept(PRETTY_TYPE_VISITOR, null)) .collect(joining(", "))); sb.append(')'); return sb.toString(); }
private static String getClassName(ClassSymbol s) { if (s.isAnonymous()) { return s.getSuperclass().tsym.getSimpleName().toString(); } else { return s.getSimpleName().toString(); } }
private void fixQualifier(Tree tree, ExpressionTree qualifierExpression) { if (sym.equals(ASTHelpers.getSymbol(tree))) { builder.replace(qualifierExpression, sym.owner.enclClass().getSimpleName().toString()); } } }.scan(state.getPath().getCompilationUnit(), null);
if (!classdecl.sym.isAnonymous()) { println().printIndent() .print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name) .print("[\"" + CLASS_NAME_IN_CONSTRUCTOR + "\"] = ") .print("\"" + classdecl.sym.getQualifiedName().toString() + "\";"); if (!interfaces.isEmpty()) { println().printIndent() .print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name) .print("[\"" + INTERFACES_FIELD_NAME + "\"] = "); print("["); println().printIndent().print(classdecl.sym.getSimpleName().toString()) .print("[\"" + ENUM_WRAPPER_CLASS_WRAPPERS + "\"] = ["); int index = 0;
public void visitNewClass(JCNewClass newClass) { ClassSymbol clazz = ((ClassSymbol) newClass.clazz.type.tsym); if (clazz.getSimpleName().toString().equals(JSweetConfig.GLOBALS_CLASS_NAME)) { report(newClass, JSweetProblem.GLOBAL_CANNOT_BE_INSTANTIATED); return;
context.addClassNameMapping(classdecl.sym, "__" + classdecl.sym.getEnclosingElement().getQualifiedName().toString().replace('.', '_') + "_" + clashingInnerClass.getSimpleName());
? "implements" : "overrides", override.enclClass().getSimpleName())) .build();
@Override public Void visitBinary(BinaryTree binaryTree, Void unused) { if (binaryTree.getKind() != Kind.NOT_EQUAL_TO && binaryTree.getKind() != Kind.EQUAL_TO) { return super.visitBinary(binaryTree, null); } if (matchesEitherWay(binaryTree, isParameter, (tree, s) -> matchesNull(tree))) { if (binaryTree.getKind() == Kind.NOT_EQUAL_TO) { makeAlwaysTrue(); } if (binaryTree.getKind() == Kind.EQUAL_TO) { makeAlwaysFalse(); } return null; } if (matchesEitherWay(binaryTree, THIS_CLASS, otherClass)) { matchedGetClass = true; String instanceOf = String.format( "%s instanceof %s", parameter.getSimpleName(), classSymbol.getSimpleName()); if (binaryTree.getKind() == Kind.EQUAL_TO) { fix.replace(binaryTree, instanceOf); } if (binaryTree.getKind() == Kind.NOT_EQUAL_TO) { fix.replace(binaryTree, String.format("!(%s)", instanceOf)); } } return super.visitBinary(binaryTree, null); }
@Override public Description matchMethod(MethodTree methodTree, VisitorState state) { MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree); if (isSynchronized(methodSymbol)) { return NO_MATCH; } for (MethodSymbol s : ASTHelpers.findSuperMethods(methodSymbol, state.getTypes())) { if (isSynchronized(s)) { // Input streams are typically not used across threads, so this case isn't // worth enforcing. if (isSameType(s.owner.type, state.getTypeFromString("java.io.InputStream"), state)) { continue; } if (ignore(methodTree, state)) { return NO_MATCH; } return buildDescription(methodTree) .addFix(SuggestedFixes.addModifiers(methodTree, state, Modifier.SYNCHRONIZED)) .setMessage( String.format( "Unsynchronized method %s overrides synchronized method in %s", methodSymbol.getSimpleName(), s.enclClass().getSimpleName())) .build(); } } return NO_MATCH; }
/** * Resolves a simple name as a type. Considers super classes, lexically enclosing classes, and * then arbitrary types available in the current environment. */ private Symbol resolveType(String name, SearchSuperTypes searchSuperTypes) { Symbol type = null; if (searchSuperTypes == SearchSuperTypes.YES) { type = getSuperType(enclosingClass, name); } if (enclosingClass.getSimpleName().contentEquals(name)) { type = enclosingClass; } if (type == null) { type = getLexicallyEnclosing(enclosingClass, name); } if (type == null) { type = attribIdent(name); } checkGuardedBy( !(type instanceof Symbol.PackageSymbol), "All we could find for '%s' was a package symbol.", name); return type; }
private static String getClassName(ClassSymbol s) { if (s.isAnonymous()) { return s.getSuperclass().tsym.getSimpleName().toString(); } else { return s.getSimpleName().toString(); } }