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

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: CompleteExpression.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private boolean performComplete(JCExpression jcExpression, ArrayList<SuggestItem> result) {
    if (DLog.DEBUG) DLog.d(TAG, "analyse: class = " + jcExpression.getClass());
    //error expression
    if (jcExpression instanceof JCErroneous) {
        System.out.println("CompleteExpression.JCErroneous");
        List<? extends JCTree> errorTrees = ((JCErroneous) jcExpression).getErrorTrees();
        JCTree errExpr = errorTrees.get(0);
        if (errExpr instanceof JCExpression) {
            return performComplete((JCExpression) errExpr, result);
        }

    } else if (jcExpression instanceof JCFieldAccess) {
        //arrayList.to <-  JCFieldAccess
        //arrayList.add("Hello");
        //s.toLower, incomplete method but give JCFieldAccess
        return performCompleteFieldAccess((JCFieldAccess) jcExpression, result);

    } else if (jcExpression instanceof JCTree.JCIdent) {
        return performCompleteIdent((JCTree.JCIdent) jcExpression, result);

    }
    return false;
}
 
Example 2
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private static void addVariable(
    SourceContext context,
    Source src,
    JCTree.JCExpression selected,
    String selectScope,
    String fqcn) {
  if (selected instanceof JCTree.JCIdent) {
    JCTree.JCIdent ident = (JCTree.JCIdent) selected;
    int vStart = ident.getStartPosition();
    int vEnd = ident.getEndPosition(context.endPosTable);
    Range vRange = Range.create(src, vStart, vEnd);
    Variable variable = new Variable(selectScope, ident.pos, vRange);
    variable.fqcn = fqcn;
    src.getCurrentScope()
        .ifPresent(
            scope -> {
              scope.addVariable(variable);
              addSymbolIndex(src, scope, variable);
            });
  }
}
 
Example 3
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("WeakerAccess")
public static boolean isJailbreakReceiver( JCTree.JCFieldAccess fieldAccess )
{
  Symbol sym = null;
  JCExpression selected = fieldAccess.selected;
  if( selected instanceof JCTree.JCIdent )
  {
    sym = ((JCTree.JCIdent)selected).sym;
  }
  else if( selected instanceof JCTree.JCMethodInvocation )
  {
    if( ((JCTree.JCMethodInvocation)selected).meth instanceof JCTree.JCFieldAccess )
    {
      sym = ((JCTree.JCFieldAccess)((JCTree.JCMethodInvocation)selected).meth).sym;
    }
    else if( ((JCTree.JCMethodInvocation)selected).meth instanceof JCTree.JCIdent )
    {
      sym = ((JCTree.JCIdent)((JCTree.JCMethodInvocation)selected).meth).sym;
    }
  }

  return isJailbreakSymbol( sym );
}
 
Example 4
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void assignTypes( JCExpression m, Symbol symbol )
{
  if( m instanceof JCTree.JCFieldAccess )
  {
    JCTree.JCFieldAccess fieldAccess = (JCTree.JCFieldAccess)m;
    fieldAccess.sym = symbol;
    fieldAccess.type = symbol.type;
    assignTypes( fieldAccess.selected, symbol.owner );
  }
  else if( m instanceof JCTree.JCIdent )
  {
    JCTree.JCIdent ident = (JCTree.JCIdent)m;
    ident.sym = symbol;
    ident.type = symbol.type;
  }
}
 
Example 5
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private static void analyzePackageName(
    CompilationUnitTree cut, Source src, EndPosTable endPosTable) {
  ExpressionTree packageExpr = cut.getPackageName();
  if (isNull(packageExpr)) {
    src.setPackageName("");
  } else {
    src.setPackageName(packageExpr.toString());
  }
  if (packageExpr instanceof JCTree.JCIdent) {
    JCTree.JCIdent ident = (JCTree.JCIdent) packageExpr;
    int startPos = ident.getPreferredPosition();
    int endPos = ident.getEndPosition(endPosTable);
    Range range = Range.create(src, startPos + 1, endPos);
    long pkgLine = range.begin.line;
    src.setPackageStartLine(pkgLine);
    addPackageIndex(src, pkgLine, 8, src.getPackageName());
  }
}
 
Example 6
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@Override
public void visitIdent(JCTree.JCIdent jcIdent) {
  super.visitIdent(jcIdent);
  Symbol symbol = jcIdent.sym;
  if (symbol.type instanceof Type.JCPrimitiveType) {
    Id id = parseId(symbol);
    if (id != null) {
      resourceIds.put(id.value, id);
    }
  }
}
 
Example 7
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Name getLeftTreeName(StatementTree statement) {
    if (statement.getKind() != Kind.EXPRESSION_STATEMENT) {
        return null;
    }
    JCTree.JCExpressionStatement jceTree = (JCTree.JCExpressionStatement) statement;
    if (jceTree.expr.getKind() != Kind.ASSIGNMENT) {
        return null;
    }
    JCTree.JCAssign assignTree = (JCTree.JCAssign) jceTree.expr;
    return ((JCTree.JCIdent) assignTree.lhs).name;
}
 
Example 8
Source File: SingletonJavacHandler.java    From tutorials with MIT License 5 votes vote down vote up
private void addInstanceVar(JavacNode singletonClass, JavacTreeMaker singletonClassTM, JavacNode holderClass) {
    JCTree.JCModifiers fieldMod = singletonClassTM.Modifiers(Flags.PRIVATE | Flags.STATIC | Flags.FINAL);

    JCTree.JCClassDecl singletonClassDecl = (JCTree.JCClassDecl) singletonClass.get();
    JCTree.JCIdent singletonClassType = singletonClassTM.Ident(singletonClassDecl.name);

    JCTree.JCNewClass newKeyword = singletonClassTM.NewClass(null, List.nil(), singletonClassType, List.nil(), null);

    JCTree.JCVariableDecl instanceVar = singletonClassTM.VarDef(fieldMod, singletonClass.toName("INSTANCE"), singletonClassType, newKeyword);
    JavacHandlerUtil.injectField(holderClass, instanceVar);
}
 
Example 9
Source File: SingletonJavacHandler.java    From tutorials with MIT License 5 votes vote down vote up
private JCTree.JCBlock addReturnBlock(JavacTreeMaker singletonClassTreeMaker, JavacNode holderInnerClass) {

        JCTree.JCClassDecl holderInnerClassDecl = (JCTree.JCClassDecl) holderInnerClass.get();
        JavacTreeMaker holderInnerClassTreeMaker = holderInnerClass.getTreeMaker();
        JCTree.JCIdent holderInnerClassType = holderInnerClassTreeMaker.Ident(holderInnerClassDecl.name);

        JCTree.JCFieldAccess instanceVarAccess = holderInnerClassTreeMaker.Select(holderInnerClassType, holderInnerClass.toName("INSTANCE"));
        JCTree.JCReturn returnValue = singletonClassTreeMaker.Return(instanceVarAccess);

        ListBuffer<JCTree.JCStatement> statements = new ListBuffer<>();
        statements.append(returnValue);

        return singletonClassTreeMaker.Block(0L, statements.toList());
    }
 
Example 10
Source File: SingletonJavacHandler.java    From tutorials with MIT License 5 votes vote down vote up
private void addFactoryMethod(JavacNode singletonClass, JavacTreeMaker singletonClassTreeMaker, JavacNode holderInnerClass) {
    JCTree.JCModifiers modifiers = singletonClassTreeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC);

    JCTree.JCClassDecl singletonClassDecl = (JCTree.JCClassDecl) singletonClass.get();
    JCTree.JCIdent singletonClassType = singletonClassTreeMaker.Ident(singletonClassDecl.name);

    JCTree.JCBlock block = addReturnBlock(singletonClassTreeMaker, holderInnerClass);

    JCTree.JCMethodDecl factoryMethod = singletonClassTreeMaker.MethodDef(modifiers, singletonClass.toName("getInstance"), singletonClassType, List.nil(), List.nil(), List.nil(), block, null);
    JavacHandlerUtil.injectMethod(singletonClass, factoryMethod);
}
 
Example 11
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Erase all structural interface type literals to Object
 */
@Override
public void visitIdent( JCTree.JCIdent tree )
{
  super.visitIdent( tree );

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

  if( TypeUtil.isStructuralInterface( _tp, tree.sym ) && !isReceiver( tree ) )
  {
    Symbol.ClassSymbol objectSym = getObjectClass();
    Tree parent = _tp.getParent( tree );
    JCTree.JCIdent objIdent = _tp.getTreeMaker().Ident( objectSym );
    if( parent instanceof JCTree.JCVariableDecl )
    {
      ((JCTree.JCVariableDecl)parent).type = objectSym.type;

      long parameterModifier = 8589934592L; // Flag.Flag.PARAMETER.value
      if( (((JCTree.JCVariableDecl)parent).mods.flags & parameterModifier) != 0 )
      {
        objIdent.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).sym.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).vartype = objIdent;
      }
    }
    else if( parent instanceof JCTree.JCWildcard )
    {
      JCTree.JCWildcard wildcard = (JCTree.JCWildcard)parent;
      wildcard.type = new Type.WildcardType( objectSym.type, wildcard.kind.kind, wildcard.type.tsym );
    }
    tree = objIdent;
    tree.type = objectSym.type;
  }
  result = tree;
}
 
Example 12
Source File: NameReplacer.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitIdent( JCTree.JCIdent jcIdent )
{
  super.visitIdent( jcIdent );
  Names names = Names.instance( _javacTask.getContext() );
  jcIdent.name = names.fromString( jcIdent.name.toString() );
  jcIdent.pos = _offset;
}
 
Example 13
Source File: ManAttr_8.java    From manifold with Apache License 2.0 4 votes vote down vote up
/**
 * Handles @Jailbreak
 */
@Override
public void visitApply( JCTree.JCMethodInvocation tree )
{
  if( !(tree.meth instanceof JCTree.JCFieldAccess) )
  {
    super.visitApply( tree );
    patchMethodType( tree );
    return;
  }

  if( JAILBREAK_PRIVATE_FROM_SUPERS )
  {
    _manLog.pushSuspendIssues( tree ); // since method-calls can be nested, we need a tree of stacks TreeNode(JCTree.JCFieldAccess, Stack<JCDiagnostic>>)
  }

  JCTree.JCFieldAccess fieldAccess = (JCTree.JCFieldAccess)tree.meth;
  try
  {
    super.visitApply( tree );
    patchMethodType( tree );

    if( JAILBREAK_PRIVATE_FROM_SUPERS )
    {
      if( fieldAccess.type instanceof Type.ErrorType )
      {
        if( shouldCheckSuperType( fieldAccess.selected.type ) && _manLog.isJailbreakSelect( fieldAccess ) )
        {
          // set qualifier type to supertype to handle private methods
          Type.ClassType oldType = (Type.ClassType)fieldAccess.selected.type;
          fieldAccess.selected.type = ((Symbol.ClassSymbol)oldType.tsym).getSuperclass();
          ((JCTree.JCIdent)fieldAccess.selected).sym.type = fieldAccess.selected.type;
          fieldAccess.type = null;
          fieldAccess.sym = null;
          tree.type = null;

          // retry with supertype
          visitApply( tree );

          // restore original type
          fieldAccess.selected.type = oldType;
          ((JCTree.JCIdent)fieldAccess.selected).sym.type = fieldAccess.selected.type;
        }
      }
      else
      {
        // apply any issues logged for the found method (only the top of the suspend stack)
        _manLog.recordRecentSuspendedIssuesAndRemoveOthers( tree );
      }
    }
  }
  finally
  {
    if( JAILBREAK_PRIVATE_FROM_SUPERS )
    {
      _manLog.popSuspendIssues( tree );
    }
  }
}
 
Example 14
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private Symbol.MethodSymbol findExtMethod( JCTree.JCMethodInvocation tree )
{
  Symbol sym = null;
  if( tree.meth instanceof JCTree.JCFieldAccess )
  {
    sym = ((JCTree.JCFieldAccess)tree.meth).sym;
  }
  else if( tree.meth instanceof JCTree.JCIdent )
  {
    sym = ((JCTree.JCIdent)tree.meth).sym;
  }

  if( sym == null || !sym.hasAnnotations() )
  {
    return null;
  }

  for( Attribute.Compound annotation: sym.getAnnotationMirrors() )
  {
    if( annotation.type.toString().equals( ExtensionMethod.class.getName() ) )
    {
      String extensionClass = (String)annotation.values.get( 0 ).snd.getValue();
      boolean isStatic = (boolean)annotation.values.get( 1 ).snd.getValue();
      BasicJavacTask javacTask = (BasicJavacTask)_tp.getJavacTask(); //JavacHook.instance() != null ? (JavacTaskImpl)JavacHook.instance().getJavacTask_PlainFileMgr() : ClassSymbols.instance( _sp.getModule() ).getJavacTask_PlainFileMgr();
      Pair<Symbol.ClassSymbol, JCTree.JCCompilationUnit> classSymbol = ClassSymbols.instance( _sp.getModule() ).getClassSymbol( javacTask, _tp, extensionClass );
      if( classSymbol == null )
      {
        // In module mode if a package in another module is not exported, classes in the package
        // will not be accessible to other modules, hence the null classSymbol
        continue;
      }

      Symbol.ClassSymbol extClassSym = classSymbol.getFirst();
      if( extClassSym == null )
      {
        // This can happen during bootstrapping with Dark Java classes from Manifold itself
        // So we short-circuit that here (ManClassFinder_9 or any other darkj class used during bootstrapping doesn't really need to use extensions)
        return null;
      }
      Types types = Types.instance( javacTask.getContext() );
      outer:
      for( Symbol elem: IDynamicJdk.instance().getMembers( extClassSym ) )
      {
        if( elem instanceof Symbol.MethodSymbol && elem.flatName().toString().equals( sym.name.toString() ) )
        {
          Symbol.MethodSymbol extMethodSym = (Symbol.MethodSymbol)elem;
          List<Symbol.VarSymbol> extParams = extMethodSym.getParameters();
          List<Symbol.VarSymbol> calledParams = ((Symbol.MethodSymbol)sym).getParameters();
          int thisOffset = isStatic ? 0 : 1;
          if( extParams.size() - thisOffset != calledParams.size() )
          {
            continue;
          }
          for( int i = thisOffset; i < extParams.size(); i++ )
          {
            Symbol.VarSymbol extParam = extParams.get( i );
            Symbol.VarSymbol calledParam = calledParams.get( i - thisOffset );
            if( !types.isSameType( types.erasure( extParam.type ), types.erasure( calledParam.type ) ) )
            {
              continue outer;
            }
          }
          return extMethodSym;
        }
      }
    }
  }
  return null;
}
 
Example 15
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
/**
 * Erase all structural interface type literals to Object
 */
@Override
public void visitSelect( JCTree.JCFieldAccess tree )
{
  super.visitSelect( tree );

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

  if( TypeUtil.isStructuralInterface( _tp, tree.sym ) && !isReceiver( tree ) )
  {
    Symbol.ClassSymbol objectSym = getObjectClass();
    JCTree.JCIdent objIdent = _tp.getTreeMaker().Ident( objectSym );
    Tree parent = _tp.getParent( tree );
    if( parent instanceof JCTree.JCVariableDecl )
    {
      ((JCTree.JCVariableDecl)parent).type = objectSym.type;
      long parameterModifier = 8589934592L; // Flag.Flag.PARAMETER.value
      if( (((JCTree.JCVariableDecl)parent).mods.flags & parameterModifier) != 0 )
      {
        objIdent.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).sym.type = objectSym.type;
        ((JCTree.JCVariableDecl)parent).vartype = objIdent;
      }
    }
    else if( parent instanceof JCTree.JCWildcard )
    {
      JCTree.JCWildcard wildcard = (JCTree.JCWildcard)parent;
      wildcard.type = new Type.WildcardType( objectSym.type, wildcard.kind.kind, wildcard.type.tsym );
    }
    result = objIdent;
  }
  else if( isJailbreakReceiver( tree ) )
  {
    result = replaceWithReflection( tree );
  }
  else
  {
    result = tree;
  }
}
 
Example 16
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 4 votes vote down vote up
@Override
public CodeModel visitInstanceOf(InstanceOfTree node, VisitContext p) {
  ExpressionModel classModel = scan(node.getExpression(), p);
  TypeElement type = (TypeElement) ((JCTree.JCIdent) node.getType()).sym;
  return classModel.onInstanceOf(type);
}
 
Example 17
Source File: StrictJavaDepsPlugin.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Visits an identifier in the AST. We only care about type symbols. */
@Override
public void visitIdent(JCTree.JCIdent tree) {
  checkTypeLiteral(tree, tree.sym);
}
 
Example 18
Source File: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 4 votes vote down vote up
public List<JCTree.JCExpression> parse( String stringValue, int literalOffset )
{
  List<StringLiteralTemplateParser.Expr> comps =
    StringLiteralTemplateParser.parse( new EscapeMatcher( _manDiagnosticHandler, literalOffset+1 ), stringValue );
  if( comps.isEmpty() )
  {
    return Collections.emptyList();
  }

  TreeMaker maker = TreeMaker.instance( _javacTask.getContext() );
  Names names = Names.instance( _javacTask.getContext() );

  List<JCTree.JCExpression> exprs = new ArrayList<>();
  StringLiteralTemplateParser.Expr prev = null;
  for( StringLiteralTemplateParser.Expr comp : comps )
  {
    JCTree.JCExpression expr;
    if( comp.isVerbatim() )
    {
      expr = maker.Literal( comp.getExpr() );
    }
    else
    {
      if( prev != null && !prev.isVerbatim() )
      {
        // enforce concatenation
        exprs.add( maker.Literal( "" ) );
      }

      int exprPos = literalOffset + 1 + comp.getOffset();

      if( comp.isIdentifier() )
      {
        JCTree.JCIdent ident = maker.Ident( names.fromString( comp.getExpr() ) );
        ident.pos = exprPos;
        expr = ident;
      }
      else
      {
        DiagnosticCollector<JavaFileObject> errorHandler = new DiagnosticCollector<>();
        expr = _tp.getHost().getJavaParser().parseExpr( comp.getExpr(), errorHandler );
        if( transferParseErrors( literalOffset, comp, expr, errorHandler ) )
        {
          return Collections.emptyList();
        }
        replaceNames( expr, exprPos );
      }
    }
    prev = comp;
    exprs.add( expr );
  }

  if( exprs.size() == 1 )
  {
    // insert an empty string so concat will make the expr a string
    exprs.add( 0, maker.Literal( "" ) );
  }

  return exprs;
}
 
Example 19
Source File: CompleteExpression.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Suggestion method, variable
 */
private boolean performCompleteIdent(JCTree.JCIdent jcIdent, ArrayList<SuggestItem> result) {
    return false;
}