/** Detects if the class is annotated as @BTrace(trusted=true). */ private boolean hasTrustedAnnotation(ClassTree ct, Element topElement) { for (AnnotationTree at : ct.getModifiers().getAnnotations()) { String annFqn = ((JCTree)at.getAnnotationType()).type.tsym.getQualifiedName().toString(); if (!annFqn.equals(BTrace.class.getName())) { continue; } // now we have @BTrace, look for unsafe = xxx or trusted = xxx for (ExpressionTree ext : at.getArguments()) { if (!(ext instanceof JCAssign)) { continue; } JCAssign assign = (JCAssign) ext; String name = ((JCIdent)assign.lhs).name.toString(); if (!"unsafe".equals(name) && !"trusted".equals(name)) { continue; } // now rhs is the value of @BTrace.unsafe. // The value can be complex (!!true, 1 == 2, etc.) - we support only booleans String val = assign.rhs.toString(); if ("true".equals(val)) { return true; // bingo! } else if (!"false".equals(val)) { processingEnv.getMessager().printMessage(Kind.WARNING, Messages.get("no.complex.unsafe.value"), topElement); } } } return false; }
(TypeSymbol) methodDecl.sym.getEnclosingElement(), invocation); if (method != null && method.getSimpleName().toString().equals(methodName)) { String inv = invocation.meth.toString(); if (!(inv.equals(methodName) || inv.equals("this." + methodName) || /* constructor case */ inv.equals("this"))) {
if (fieldAccess.selected.toString().equals("this")) { if (fieldAccess.sym.isStatic()) { report(variableAccess, JSweetProblem.CANNOT_ACCESS_STATIC_MEMBER_ON_THIS, fieldAccess.name); && !"this".equals(fieldAccess.selected.toString()) && !"class".equals(targetFieldName)) { String relTarget = getRootRelativeName((Symbol) targetType); getPrinter().print(relTarget)
if (importDecl.getQualifiedIdentifier() instanceof JCFieldAccess) { JCFieldAccess fa = (JCFieldAccess) importDecl.getQualifiedIdentifier(); switch (fa.selected.toString()) { case "java.lang.Math": return null;
&& !"<init>".equals(memberReference.name.toString()) && !JSweetConfig.GLOBALS_CLASS_NAME.equals(memberReferenceSimpleName); boolean exprIsInstance = memberReference.expr.toString().equals("this") || memberReference.expr.toString().equals("super") || (memberReference.expr instanceof JCIdent && ((JCIdent) memberReference.expr).sym instanceof VarSymbol) || (memberReference.expr instanceof JCFieldAccess && ((JCFieldAccess) memberReference.expr).sym instanceof VarSymbol);
boolean regularFunction = false; if (getParent() instanceof JCMethodInvocation && ((JCMethodInvocation) getParent()).meth.toString().endsWith("function") && getParentOfParent() instanceof JCMethodInvocation && ((JCMethodInvocation) getParentOfParent()).meth.toString().endsWith("$noarrow")) { MethodInvocationElement invocation = (MethodInvocationElement) ExtendedElementFactory.INSTANCE .create(getParent());
/** * 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; }
@Override public void visitApply(JCMethodInvocation invocation) { // TODO: same for static variables if (invocation.meth instanceof JCIdent && JSweetConfig.TS_STRICT_MODE_KEYWORDS.contains(invocation.meth.toString().toLowerCase())) { PackageSymbol invocationPackage = (PackageSymbol) ((JCIdent) invocation.meth).sym .getEnclosingElement().getEnclosingElement(); String rootRelativeInvocationPackageName = getRootRelativeName(invocationPackage); if (rootRelativeInvocationPackageName.indexOf('.') == -1) { super.visitApply(invocation); return; } String targetRootPackageName = rootRelativeInvocationPackageName.substring(0, rootRelativeInvocationPackageName.indexOf('.')); String pathToReachRootPackage = Util.getRelativePath( "/" + compilationUnit.packge.getQualifiedName().toString().replace('.', '/'), "/" + targetRootPackageName); if (pathToReachRootPackage == null) { super.visitApply(invocation); return; } File moduleFile = new File(new File(pathToReachRootPackage), JSweetConfig.MODULE_FILE_NAME); if (!invocationPackage.toString().equals(compilationUnit.packge.getSimpleName().toString())) { useModule(false, invocationPackage, invocation, targetRootPackageName, moduleFile.getPath().replace('\\', '/'), null); } } super.visitApply(invocation); }
private static boolean isAllowedStatementInMap(JCStatement statement) { if (statement instanceof JCExpressionStatement) { JCExpressionStatement exprStat = (JCExpressionStatement) statement; if (exprStat.getExpression() instanceof JCAssign) { return true; } if (exprStat.getExpression() instanceof JCMethodInvocation) { JCMethodInvocation inv = (JCMethodInvocation) exprStat.getExpression(); String methodName; if (inv.meth instanceof JCFieldAccess) { methodName = ((JCFieldAccess) inv.meth).name.toString(); } else { methodName = inv.meth.toString(); } if (JSweetConfig.INDEXED_GET_FUCTION_NAME.equals(methodName) || JSweetConfig.INDEXED_SET_FUCTION_NAME.equals(methodName)) { return true; } } } return false; }
/** * Finds the method declaration within the given type, for the given invocation. */ public static MethodSymbol findMethodDeclarationInType(Types types, TypeSymbol typeSymbol, JCMethodInvocation invocation) { String meth = invocation.meth.toString(); String methName = meth.substring(meth.lastIndexOf('.') + 1); return findMethodDeclarationInType(types, typeSymbol, methName, (MethodType) invocation.meth.type); }
access.getExpression().toString(), canonicalOwner.toString(), identifier.toString(),
@Override public void visitApply(JCMethodInvocation tree) { JCExpression select = tree.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitCommentBefore")) { String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue(); JCExpression expr = tree.getArguments().get(1); try { print("/* " + commentLiteral + " */ "); } catch (IOException e) { throw new RuntimeException(e); } expr.accept(this); } else { super.visitApply(tree); } }
@Override public void printStat(JCTree tree) throws IOException { if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) { JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression(); JCExpression select = invocation.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitComment")) { String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue(); print("// " + commentLiteral); return; } } super.printStat(tree); }
@Override public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { Symbol baseSym = ASTHelpers.getSymbol(tree.getExpression()); if (baseSym == null || baseSym.getKind() != ElementKind.TYPE_PARAMETER) { return Description.NO_MATCH; } TreeMaker make = TreeMaker.instance(state.context) .forToplevel((JCCompilationUnit) state.getPath().getCompilationUnit()); JCExpression qual = make.QualIdent(ASTHelpers.getSymbol(tree)); return describeMatch(tree, SuggestedFix.replace(tree, qual.toString())); } }
@Override public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException { JCExpression expression = getExpression().inline(inliner); if (expression.toString().equals(CONVERT_TO_IDENT)) { return inliner.maker().Ident(getIdentifier().inline(inliner)); } // TODO(lowasser): consider inlining this.foo() as foo() return inliner.maker().Select(getExpression().inline(inliner), getIdentifier().inline(inliner)); } }