com.sun.tools.javac.tree.JCTree.JCIdent Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCIdent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TreePruner.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static boolean isUnused(
    JCCompilationUnit unit, Set<String> usedNames, JCImport importTree) {
  String simpleName =
      importTree.getQualifiedIdentifier() instanceof JCIdent
          ? ((JCIdent) importTree.getQualifiedIdentifier()).getName().toString()
          : ((JCFieldAccess) importTree.getQualifiedIdentifier()).getIdentifier().toString();
  String qualifier =
      ((JCFieldAccess) importTree.getQualifiedIdentifier()).getExpression().toString();
  if (qualifier.equals("java.lang")) {
    return true;
  }
  if (unit.getPackageName() != null && unit.getPackageName().toString().equals(qualifier)) {
    // remove imports of classes from the current package
    return true;
  }
  if (importTree.getQualifiedIdentifier() instanceof JCFieldAccess
      && ((JCFieldAccess) importTree.getQualifiedIdentifier())
          .getIdentifier()
          .contentEquals("*")) {
    return false;
  }
  if (usedNames.contains(simpleName)) {
    return false;
  }
  return true;
}
 
Example #2
Source File: TransTypes.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitIdent(JCIdent tree) {
    Type et = tree.sym.erasure(types);

    // Map type variables to their bounds.
    if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) {
        result = make.at(tree.pos).Type(et);
    } else
    // Map constants expressions to themselves.
    if (tree.type.constValue() != null) {
        result = tree;
    }
    // Insert casts of variable uses as needed.
    else if (tree.sym.kind == VAR) {
        result = retype(tree, et, pt);
    }
    else {
        tree.type = erasure(tree.type);
        result = tree;
    }
}
 
Example #3
Source File: TestInvokeDynamic.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #4
Source File: TestInvokeDynamic.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #5
Source File: TestInvokeDynamic.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #6
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Construct definition of an access constructor.
 *  @param pos        The source code position of the definition.
 *  @param constr     The private constructor.
 *  @param accessor   The access method for the constructor.
 */
JCTree accessConstructorDef(int pos, Symbol constr, MethodSymbol accessor) {
    make.at(pos);
    JCMethodDecl md = make.MethodDef(accessor,
                                  accessor.externalType(types),
                                  null);
    JCIdent callee = make.Ident(names._this);
    callee.sym = constr;
    callee.type = constr.type;
    md.body =
        make.Block(0, List.of(
            make.Call(
                make.App(
                    callee,
                    make.Idents(md.params.reverse().tail.reverse())))));
    return md;
}
 
Example #7
Source File: LambdaToMethod.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Translate identifiers within a lambda to the mapped identifier
 * @param tree
 */
@Override
public void visitIdent(JCIdent tree) {
    if (context == null || !analyzer.lambdaIdentSymbolFilter(tree.sym)) {
        super.visitIdent(tree);
    } else {
        int prevPos = make.pos;
        try {
            make.at(tree);

            LambdaTranslationContext lambdaContext = (LambdaTranslationContext) context;
            JCTree ltree = lambdaContext.translate(tree);
            if (ltree != null) {
                result = ltree;
            } else {
                //access to untranslated symbols (i.e. compile-time constants,
                //members defined inside the lambda body, etc.) )
                super.visitIdent(tree);
            }
        } finally {
            make.at(prevPos);
        }
    }
}
 
Example #8
Source File: TestInvokeDynamic.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #9
Source File: TestInvokeDynamic.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #10
Source File: TestInvokeDynamic.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #11
Source File: TestInvokeDynamic.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #12
Source File: TestInvokeDynamic.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        Object[] staticArgs = new Object[arity.arity];
        for (int i = 0; i < arity.arity ; i++) {
            staticArgs[i] = saks[i].getValue(syms, names, types);
        }
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
    }
    return null;
}
 
Example #13
Source File: Javac.java    From EasyMPermission with MIT License 6 votes vote down vote up
/**
 * Turns an expression into a guessed intended literal. Only works for
 * literals, as you can imagine.
 * 
 * Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'.
 */
public static Object calculateGuess(JCExpression expr) {
	if (expr instanceof JCLiteral) {
		JCLiteral lit = (JCLiteral) expr;
		if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) {
			return ((Number) lit.value).intValue() == 0 ? false : true;
		}
		return lit.value;
	} else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) {
		String x = expr.toString();
		if (x.endsWith(".class")) x = x.substring(0, x.length() - 6);
		else {
			int idx = x.lastIndexOf('.');
			if (idx > -1) x = x.substring(idx + 1);
		}
		return x;
	} else
		return null;
}
 
Example #14
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 6 votes vote down vote up
public void visitAnnotation(JCAnnotation tree) {
	try {
		print("@");
		printExpr(tree.annotationType);
		if (tree.args.nonEmpty()) {
			print("(");
			if (tree.args.length() == 1 && tree.args.get(0) instanceof JCAssign) {
				 JCExpression lhs = ((JCAssign)tree.args.get(0)).lhs;
				 if (lhs instanceof JCIdent && ((JCIdent)lhs).name.toString().equals("value")) tree.args = List.of(((JCAssign)tree.args.get(0)).rhs);
			}
			printExprs(tree.args);
			print(")");
		}
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #15
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static boolean isConstructorCall(final JCStatement statement) {
	if (!(statement instanceof JCExpressionStatement)) return false;
	JCExpression expr = ((JCExpressionStatement) statement).expr;
	if (!(expr instanceof JCMethodInvocation)) return false;
	JCExpression invocation = ((JCMethodInvocation) expr).meth;
	String name;
	if (invocation instanceof JCFieldAccess) {
		name = ((JCFieldAccess) invocation).name.toString();
	} else if (invocation instanceof JCIdent) {
		name = ((JCIdent) invocation).name.toString();
	} else {
		name = "";
	}
	
	return "super".equals(name) || "this".equals(name);
}
 
Example #16
Source File: TreePruner.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static boolean delegatingConstructor(List<JCStatement> stats) {
  if (stats.isEmpty()) {
    return false;
  }
  JCStatement stat = stats.get(0);
  if (stat.getKind() != Kind.EXPRESSION_STATEMENT) {
    return false;
  }
  JCExpression expr = ((JCExpressionStatement) stat).getExpression();
  if (expr.getKind() != Kind.METHOD_INVOCATION) {
    return false;
  }
  JCExpression method = ((JCMethodInvocation) expr).getMethodSelect();
  Name name;
  switch (method.getKind()) {
    case IDENTIFIER:
      name = ((JCIdent) method).getName();
      break;
    case MEMBER_SELECT:
      name = ((JCFieldAccess) method).getIdentifier();
      break;
    default:
      return false;
  }
  return name.contentEquals("this") || name.contentEquals("super");
}
 
Example #17
Source File: PathAndPackageVerifier.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String next() {
    Name name;
    if (next instanceof JCIdent) {
        name = ((JCIdent) next).name;
        next = null;
    } else {
        JCFieldAccess fa = (JCFieldAccess) next;
        name = fa.name;
        next = fa.selected;
    }
    return name.toString();
}
 
Example #18
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static boolean isThisExpression(JCExpression expression) {
  if (expression instanceof JCIdent) {
    JCIdent ident = (JCIdent) expression;
    return ident.getName().contentEquals("this");
  }
  return false;
}
 
Example #19
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static MethodSymbol getMemberSymbol(JCTree.JCExpression node) {
  switch (node.getKind()) {
    case IDENTIFIER:
      return (MethodSymbol) ((JCTree.JCIdent) node).sym.baseSymbol();
    case MEMBER_SELECT:
      return (MethodSymbol) ((JCTree.JCFieldAccess) node).sym;
    default:
      throw new AssertionError("Unexpected tree kind: " + node.getKind());
  }
}
 
Example #20
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void checkModuleName (JCModuleDecl tree) {
    Name moduleName = tree.sym.name;
    Assert.checkNonNull(moduleName);
    if (lint.isEnabled(LintCategory.MODULE)) {
        JCExpression qualId = tree.qualId;
        while (qualId != null) {
            Name componentName;
            DiagnosticPosition pos;
            switch (qualId.getTag()) {
                case SELECT:
                    JCFieldAccess selectNode = ((JCFieldAccess) qualId);
                    componentName = selectNode.name;
                    pos = selectNode.pos();
                    qualId = selectNode.selected;
                    break;
                case IDENT:
                    componentName = ((JCIdent) qualId).name;
                    pos = qualId.pos();
                    qualId = null;
                    break;
                default:
                    throw new AssertionError("Unexpected qualified identifier: " + qualId.toString());
            }
            if (componentName != null) {
                String moduleNameComponentString = componentName.toString();
                int nameLength = moduleNameComponentString.length();
                if (nameLength > 0 && Character.isDigit(moduleNameComponentString.charAt(nameLength - 1))) {
                    log.warning(Lint.LintCategory.MODULE, pos, Warnings.PoorChoiceForModuleName(componentName));
                }
            }
        }
    }
}
 
Example #21
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void visitIdent(JCIdent tree) {
	try {
		print(tree.name);
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #22
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
private boolean isNoArgsSuperCall(JCExpression expr) {
	if (!(expr instanceof JCMethodInvocation)) return false;
	JCMethodInvocation tree = (JCMethodInvocation) expr;
	if (!tree.typeargs.isEmpty() || !tree.args.isEmpty()) return false;
	if (!(tree.meth instanceof JCIdent)) return false;
	return ((JCIdent) tree.meth).name.toString().equals("super");
}
 
Example #23
Source File: TestBootstrapMethodsCount.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    super.visitMethodInvocation(node, p);
    JCMethodInvocation apply = (JCMethodInvocation)node;
    JCIdent ident = (JCIdent)apply.meth;
    Symbol oldSym = ident.sym;
    if (!oldSym.isConstructor()) {
        ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
                oldSym.owner, REF_invokeStatic, bsm, oldSym.type, new Object[0]);
    }
    return null;
}
 
Example #24
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static boolean isSuperExpression(JCExpression expression) {
  if (expression instanceof JCIdent) {
    JCIdent ident = (JCIdent) expression;
    return ident.getName().contentEquals("super");
  }
  return false;
}
 
Example #25
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Expression convertIdent(JCIdent identifier) {
  if (isThisExpression(identifier)) {
    return new ThisReference(getCurrentType().getTypeDescriptor());
  }
  if (isSuperExpression(identifier)) {
    return new SuperReference(getCurrentType().getTypeDescriptor());
  }
  Symbol symbol = identifier.sym;
  if (symbol instanceof ClassSymbol) {
    return new JavaScriptConstructorReference(
        environment.createDeclarationForType((ClassSymbol) identifier.sym));
  }
  if (symbol instanceof MethodSymbol) {
    return NullLiteral.get().withComment(identifier.toString());
  }

  VarSymbol varSymbol = (VarSymbol) symbol;
  if (symbol.getKind() == ElementKind.LOCAL_VARIABLE
      || symbol.getKind() == ElementKind.RESOURCE_VARIABLE
      || symbol.getKind() == ElementKind.PARAMETER
      || symbol.getKind() == ElementKind.EXCEPTION_PARAMETER) {
    Variable variable = variableByVariableElement.get(symbol);
    return resolveVariableReference(variable);
  }

  FieldDescriptor fieldDescriptor = environment.createFieldDescriptor(varSymbol, identifier.type);
  Expression qualifier =
      fieldDescriptor.isStatic()
          ? null
          : resolveOuterClassReference(fieldDescriptor.getEnclosingTypeDescriptor(), false);
  return FieldAccess.newBuilder()
      .setQualifier(qualifier)
      .setTargetFieldDescriptor(fieldDescriptor)
      .build();
}
 
Example #26
Source File: HandleCleanup.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void doAssignmentCheck0(JavacNode node, JCTree statement, Name name) {
	if (statement instanceof JCAssign) doAssignmentCheck0(node, ((JCAssign)statement).rhs, name);
	if (statement instanceof JCExpressionStatement) doAssignmentCheck0(node,
			((JCExpressionStatement)statement).expr, name);
	if (statement instanceof JCVariableDecl) doAssignmentCheck0(node, ((JCVariableDecl)statement).init, name);
	if (statement instanceof JCTypeCast) doAssignmentCheck0(node, ((JCTypeCast)statement).expr, name);
	if (statement instanceof JCIdent) {
		if (((JCIdent)statement).name.contentEquals(name)) {
			JavacNode problemNode = node.getNodeFor(statement);
			if (problemNode != null) problemNode.addWarning(
			"You're assigning an auto-cleanup variable to something else. This is a bad idea.");
		}
	}
}
 
Example #27
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private VariableDeclarationExpression toResource(JCIdent ident) {
  // Create temporary variables for resources declared outside of the try statement.
  Expression expression = convertIdent(ident);
  return VariableDeclarationExpression.newBuilder()
      .addVariableDeclaration(
          Variable.newBuilder()
              .setName("$resource")
              .setTypeDescriptor(expression.getTypeDescriptor())
              .setFinal(true)
              .build(),
          expression)
      .build();
}
 
Example #28
Source File: HandleNonNull.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * Checks if the statement is of the form 'if (x == null) {throw WHATEVER;},
 * where the block braces are optional. If it is of this form, returns "x".
 * If it is not of this form, returns null.
 */
public String returnVarNameIfNullCheck(JCStatement stat) {
	if (!(stat instanceof JCIf)) return null;
	
	/* Check that the if's statement is a throw statement, possibly in a block. */ {
		JCStatement then = ((JCIf) stat).thenpart;
		if (then instanceof JCBlock) {
			List<JCStatement> stats = ((JCBlock) then).stats;
			if (stats.length() == 0) return null;
			then = stats.get(0);
		}
		if (!(then instanceof JCThrow)) return null;
	}
	
	/* Check that the if's conditional is like 'x == null'. Return from this method (don't generate
	   a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ {
		JCExpression cond = ((JCIf) stat).cond;
		while (cond instanceof JCParens) cond = ((JCParens) cond).expr;
		if (!(cond instanceof JCBinary)) return null;
		JCBinary bin = (JCBinary) cond;
		if (!CTC_EQUAL.equals(treeTag(bin))) return null;
		if (!(bin.lhs instanceof JCIdent)) return null;
		if (!(bin.rhs instanceof JCLiteral)) return null;
		if (!CTC_BOT.equals(typeTag(bin.rhs))) return null;
		return ((JCIdent) bin.lhs).name.toString();
	}
}
 
Example #29
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private VariableDeclarationExpression toResource(JCTree resourceTree) {
  if (resourceTree.getTag() == Tag.VARDEF) {
    return createVariableDeclarationExpression((JCVariableDecl) resourceTree);
  }
  checkArgument(resourceTree.getTag() == Tag.IDENT);
  return toResource((JCIdent) resourceTree);
}
 
Example #30
Source File: HandleExtensionMethod.java    From EasyMPermission with MIT License 5 votes vote down vote up
private String methodNameOf(final JCMethodInvocation methodCall) {
	if (methodCall.meth instanceof JCIdent) {
		return ((JCIdent) methodCall.meth).name.toString();
	} else {
		return ((JCFieldAccess) methodCall.meth).name.toString();
	}
}