Java Code Examples for com.sun.tools.javac.tree.JCTree#JCUnary

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCUnary . 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: NullAway.java    From NullAway with MIT License 6 votes vote down vote up
/**
 * strip out enclosing parentheses, type casts and Nullchk operators.
 *
 * @param expr a potentially parenthesised expression.
 * @return the same expression without parentheses.
 */
private static ExpressionTree stripParensAndCasts(ExpressionTree expr) {
  boolean someChange = true;
  while (someChange) {
    someChange = false;
    if (expr.getKind().equals(PARENTHESIZED)) {
      expr = ((ParenthesizedTree) expr).getExpression();
      someChange = true;
    }
    if (expr.getKind().equals(TYPE_CAST)) {
      expr = ((TypeCastTree) expr).getExpression();
      someChange = true;
    }

    // Strips Nullchk operator
    if (expr.getKind().equals(OTHER) && expr instanceof JCTree.JCUnary) {
      expr = ((JCTree.JCUnary) expr).getExpression();
      someChange = true;
    }
  }
  return expr;
}
 
Example 2
Source File: ManAttr_8.java    From manifold with Apache License 2.0 6 votes vote down vote up
@Override
public void visitUnary( JCTree.JCUnary tree )
{
  if( !JavacPlugin.instance().isExtensionsEnabled() )
  {
    super.visitUnary( tree );
    return;
  }

  if( handleNegationOverloading( tree ) )
  {
    // Handle negation overloading
    return;
  }

  super.visitUnary( tree );
}
 
Example 3
Source File: ManAttr.java    From manifold with Apache License 2.0 6 votes vote down vote up
default boolean handleNegationOverloading( JCTree.JCUnary tree )
{
  if( tree.getTag() != Tag.NEG )
  {
    return false;
  }

  // Attribute arguments
  ReflectUtil.LiveMethodRef checkNonVoid = ReflectUtil.method( chk(), "checkNonVoid", JCDiagnostic.DiagnosticPosition.class, Type.class );
  ReflectUtil.LiveMethodRef attribExpr = ReflectUtil.method( this, "attribExpr", JCTree.class, Env.class );
  Type expr = (Type)checkNonVoid.invoke( tree.arg.pos(), attribExpr.invoke( tree.arg, getEnv() ) );

  // Handle operator overloading
  Symbol.MethodSymbol overloadOperator = ManAttr.resolveNegationMethod( types(), tree.getTag(), expr );
  if( overloadOperator != null )
  {
    overloadOperator = new OverloadOperatorSymbol( overloadOperator, false );
    IDynamicJdk.instance().setOperator( tree, (Symbol.OperatorSymbol)overloadOperator );
    Type owntype = overloadOperator.type.isErroneous()
                   ? overloadOperator.type
                   : types().memberType( expr, overloadOperator ).getReturnType();
    setResult( tree, owntype );
    return true;
  }
  return false;
}
 
Example 4
Source File: JavaDynamicJdk_8.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void setOperator( JCTree.JCExpression tree, Symbol.OperatorSymbol operator )
{
  if( tree instanceof JCTree.JCBinary )
  {
    ((JCTree.JCBinary)tree).operator = operator;
  }
  else
  {
    ((JCTree.JCUnary)tree).operator = operator;
  }
}
 
Example 5
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
public void visitUnary( JCTree.JCUnary tree )
{
  super.visitUnary( tree );

  if( _tp.isGenerate() && !shouldProcessForGeneration() )
  {
    // Don't process tree during GENERATE, unless the tree was generated e.g., a bridge method
    return;
  }

  Symbol op = IDynamicJdk.instance().getOperator( tree );
  if( op instanceof OverloadOperatorSymbol ) // handle negation overload
  {
    TreeMaker make = _tp.getTreeMaker();

    // Handle operator overload expressions

    Symbol.MethodSymbol operatorMethod = (Symbol.MethodSymbol)op;
    while( operatorMethod instanceof OverloadOperatorSymbol )
    {
      operatorMethod = ((OverloadOperatorSymbol)operatorMethod).getMethod();
    }

    if( operatorMethod != null )
    {
      JCTree.JCMethodInvocation methodCall;
      JCExpression receiver = tree.getExpression();
      methodCall = make.Apply( List.nil(), make.Select( receiver, operatorMethod ), List.nil() );
      methodCall.setPos( tree.pos );
      methodCall.type = operatorMethod.getReturnType();

      // If methodCall is an extension method, rewrite it accordingly
      Symbol.MethodSymbol extMethod = findExtMethod( methodCall );
      if( extMethod != null )
      {
        // Replace with extension method call
        methodCall = replaceExtCall( methodCall, extMethod );
      }

      result = methodCall;
    }
  }
  else if( isJailbreakReceiver( tree ) )
  {
    Tree.Kind kind = tree.getKind();
    if( kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.POSTFIX_DECREMENT ||
        kind == Tree.Kind.PREFIX_INCREMENT || kind == Tree.Kind.PREFIX_DECREMENT )
    {
      // ++, -- operators not supported with jailbreak access to fields, only direct assignment
      _tp.report( tree, Diagnostic.Kind.ERROR, ExtIssueMsg.MSG_INCREMENT_OP_NOT_ALLOWED_REFLECTION.get() );
      Types types = Types.instance( ((BasicJavacTask)_tp.getJavacTask()).getContext() );
      tree.type = types.createErrorType( tree.type );
    }
    result = tree;
  }
  else
  {
    result = tree;
  }
}
 
Example 6
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private JCTree replaceWithReflection( JCTree.JCFieldAccess tree )
{
  TreeMaker make = _tp.getTreeMaker();
  JavacElements javacElems = _tp.getElementUtil();

  boolean isStatic = tree.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC );
  if( tree.sym instanceof Symbol.MethodSymbol )
  {
    return tree;
  }

  Tree parent = _tp.getParent( tree );
  if( parent instanceof JCTree.JCAssign && ((JCTree.JCAssign)parent).lhs == tree ||
      parent instanceof JCTree.JCAssignOp && ((JCTree.JCAssignOp)parent).lhs == tree )
  {
    // handled in visitAssign() or visitAssignOp()
    return tree;
  }

  if( parent instanceof JCTree.JCUnary && ((JCTree.JCUnary)parent).arg == tree )
  {
    Tree.Kind kind = parent.getKind();

    if( kind != Tree.Kind.UNARY_MINUS && kind != Tree.Kind.UNARY_PLUS &&
        kind != Tree.Kind.LOGICAL_COMPLEMENT && kind != Tree.Kind.BITWISE_COMPLEMENT )
    {
      // supporting -, +, !, ~  not supporting --, ++
      _tp.report( (JCTree)parent, Diagnostic.Kind.ERROR, ExtIssueMsg.MSG_INCREMENT_OP_NOT_ALLOWED_REFLECTION.get() );
      return tree;
    }
  }

  Type type = tree.sym.type;
  if( type instanceof Type.ErrorType )
  {
    // No such field/method
    return tree;
  }

  Symbol.MethodSymbol reflectMethodSym = findFieldAccessReflectUtilMethod( tree, type, isStatic, false );

  ArrayList<JCExpression> newArgs = new ArrayList<>();
  newArgs.add( isStatic ? makeClassExpr( tree, tree.selected.type ) : tree.selected ); // receiver or class
  newArgs.add( make.Literal( tree.sym.flatName().toString() ) ); // field name

  Symbol.ClassSymbol reflectMethodClassSym =
    IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectionRuntimeMethods.class.getName() );

  JCTree.JCMethodInvocation reflectCall =
    make.Apply( List.nil(),
      memberAccess( make, javacElems, ReflectionRuntimeMethods.class.getName() + "." + reflectMethodSym.flatName().toString() ),
      List.from( newArgs ) );
  reflectCall.setPos( tree.pos );
  reflectCall.type = type;
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)reflectCall.getMethodSelect();
  newMethodSelect.sym = reflectMethodSym;
  newMethodSelect.type = reflectMethodSym.type;
  assignTypes( newMethodSelect.selected, reflectMethodClassSym );

  return reflectCall;
}
 
Example 7
Source File: JavaDynamicJdk_8.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
public Symbol getOperator( JCTree.JCExpression tree )
{
  return tree instanceof JCTree.JCBinary ? ((JCTree.JCBinary)tree).operator : ((JCTree.JCUnary)tree).operator;
}