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

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCLiteral . 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: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
@Override
public void visitLiteral( JCTree.JCLiteral tree )
{
  super.visitLiteral( tree );

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

  if( tree.typetag.getKindLiteral() == Tree.Kind.STRING_LITERAL )
  {
    result = replaceStringLiteral( tree );
  }
  else
  {
    result = tree;
  }
}
 
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 analyzeLiteral(
    SourceContext context, JCTree.JCLiteral literal, int preferredPos, int endPos) {
  Source src = context.source;
  Tree.Kind kind = literal.getKind();
  Object value = literal.getValue();
  Range range = Range.create(src, preferredPos, endPos);
  Variable variable = new Variable(kind.toString(), preferredPos, range);
  if (nonNull(value)) {
    variable.fqcn = value.getClass().getCanonicalName();
  } else {
    variable.fqcn = "<null>";
  }
  variable.argumentIndex = context.argumentIndex;
  context.setArgumentFQCN(variable.fqcn);
  src.getCurrentScope()
      .ifPresent(
          scope -> {
            scope.addVariable(variable);
            addSymbolIndex(src, scope, variable);
          });
}
 
Example 3
Source File: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 5 votes vote down vote up
private Boolean getDisableAnnotationValue( JCTree.JCModifiers modifiers )
{
  Boolean disable = null;
  for( JCTree.JCAnnotation anno: modifiers.getAnnotations() )
  {
    if( anno.annotationType.toString().contains( DisableStringLiteralTemplates.class.getSimpleName() ) )
    {
      try
      {
        com.sun.tools.javac.util.List<JCTree.JCExpression> args = anno.getArguments();
        if( args.isEmpty() )
        {
          disable = true;
        }
        else
        {
          JCTree.JCExpression argExpr = args.get( 0 );
          Object value;
          if( argExpr instanceof JCTree.JCLiteral &&
              (value = ((JCTree.JCLiteral)argExpr).getValue()) instanceof Boolean )
          {
            disable = (boolean)value;
          }
          else
          {
            IDynamicJdk.instance().logError( Log.instance( _javacTask.getContext() ), argExpr.pos(),
              "proc.messager", "Only boolean literal values 'true' and 'false' allowed here" );
            disable = true;
          }
        }
      }
      catch( Exception e )
      {
        disable = true;
      }
    }
  }
  return disable;
}
 
Example 4
Source File: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitLiteral( JCTree.JCLiteral jcLiteral )
{
  super.visitLiteral( jcLiteral );

  Object value = jcLiteral.getValue();
  if( !(value instanceof String) )
  {
    return;
  }

  if( isDisabled() )
  {
    return;
  }

  TreeMaker maker = TreeMaker.instance( _javacTask.getContext() );
  String stringValue = (String)value;
  List<JCTree.JCExpression> exprs = parse( stringValue, jcLiteral.getPreferredPosition() );
  JCTree.JCBinary concat = null;
  while( !exprs.isEmpty() )
  {
    if( concat == null )
    {
      concat = maker.Binary( JCTree.Tag.PLUS, exprs.remove( 0 ), exprs.remove( 0 ) );
    }
    else
    {
      concat = maker.Binary( JCTree.Tag.PLUS, concat, exprs.remove( 0 ) );
    }
  }

  result = concat == null ? result : concat;
}
 
Example 5
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * If the binding expression is of the form `A b` where `A` is a Float or Double *literal* and `b` defines a
 * `R postfixBind(String)` where `R` is the same return type as the original binding expression, then get the
 * token that was parsed for the Float or Double literal and use the String version of `postfixBind()`. This has
 * the effect of preserving the value of the token, where otherwise it can be lost due to IEEE floating point
 * encoding.
 */
private Symbol.MethodSymbol favorStringsWithNumberCoercion( JCTree.JCBinary tree, Symbol.MethodSymbol operatorMethod )
{
  String operatorMethodName = operatorMethod.name.toString();
  if( !operatorMethodName.equals( "postfixBind" ) )
  {
    return operatorMethod;
  }

  if( tree.lhs instanceof JCTree.JCLiteral &&
      (tree.lhs.getKind() == Tree.Kind.FLOAT_LITERAL || tree.lhs.getKind() == Tree.Kind.DOUBLE_LITERAL) )
  {
    Type rhsType = tree.rhs.type;
    Symbol.MethodSymbol postfixBinding_string = ManAttr.getMethodSymbol(
      _tp.getTypes(), rhsType, _tp.getSymtab().stringType, operatorMethodName, (Symbol.ClassSymbol)rhsType.tsym, 1 );

    if( postfixBinding_string != null &&
        postfixBinding_string.getParameters().get( 0 ).type.tsym == _tp.getSymtab().stringType.tsym &&
        postfixBinding_string.getReturnType().equals( operatorMethod.getReturnType() ) )
    {
      // since the source may be preprocessed we attempt to get it in its preprocessed form
      CharSequence source = ManParserFactory.getSource( _tp.getCompilationUnit().getSourceFile() );
      int start = tree.lhs.pos;
      int end = tree.lhs.pos().getEndPosition( ((JCTree.JCCompilationUnit)_tp.getCompilationUnit()).endPositions );
      String token = source.subSequence( start, end ).toString();
      if( token.endsWith( "d" ) || token.endsWith( "f" ) )
      {
        token = token.substring( 0, token.length()-1 );
      }
      JCTree.JCLiteral temp = (JCTree.JCLiteral)tree.lhs;
      tree.lhs = _tp.getTreeMaker().Literal( token );
      tree.lhs.type = _tp.getSymtab().stringType;
      tree.lhs.pos = temp.pos;

      return postfixBinding_string;
    }
  }
  return operatorMethod;
}
 
Example 6
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private JCTree replaceStringLiteral( JCTree.JCLiteral tree )
{
  String literal = (String)tree.getValue();
  if( !literal.contains( FragmentProcessor.FRAGMENT_START ) ||
      !literal.contains( FragmentProcessor.FRAGMENT_END ) )
  {
    return tree;
  }

  JCTree.JCClassDecl enclosingClass = getEnclosingClass( tree );

  CharSequence source = ManParserFactory.getSource( enclosingClass.sym.sourcefile );
  CharSequence chars = source.subSequence( tree.pos().getStartPosition(),
    tree.pos().getEndPosition( ((JCTree.JCCompilationUnit)_tp.getCompilationUnit()).endPositions ) );
  FragmentProcessor.Fragment fragment = FragmentProcessor.instance().parseFragment(
    tree.pos().getStartPosition(), chars.toString(),
    chars.length() > 3 && chars.charAt( 1 ) == '"'
    ? TEXT_BLOCK_LITERAL
    : DOUBLE_QUOTE_LITERAL );
  if( fragment != null )
  {
    String fragClass = enclosingClass.sym.packge().toString() + '.' + fragment.getName();
    Symbol.ClassSymbol fragSym = IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), fragClass );
    for( Attribute.Compound annotation: fragSym.getAnnotationMirrors() )
    {
      if( annotation.type.toString().equals( FragmentValue.class.getName() ) )
      {
        return replaceStringLiteral( fragSym, tree, annotation );
      }
    }
  }

  return tree;
}
 
Example 7
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private JCTree replaceStringLiteral( Symbol.ClassSymbol fragSym, JCTree.JCLiteral tree, Attribute.Compound attribute )
{
  if( attribute == null )
  {
    return tree;
  }

  String methodName = null;
  String type = null;
  for( com.sun.tools.javac.util.Pair<Symbol.MethodSymbol, Attribute> pair: attribute.values )
  {
    Name argName = pair.fst.getSimpleName();
    if( argName.toString().equals( "methodName" ) )
    {
      methodName = (String)pair.snd.getValue();
    }
    else if( argName.toString().equals( "type" ) )
    {
      type = (String)pair.snd.getValue();
    }
  }

  if( type != null )
  {
    return replaceStringLiteral( fragSym, tree, methodName, type );
  }

  return tree;
}
 
Example 8
Source File: ManAttr_8.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Overrides to handle fragments in String literals
 */
public void visitLiteral( JCTree.JCLiteral tree )
{
  if( tree.typetag == CLASS && tree.value.toString().startsWith( "[>" ) )
  {
    Type type = getFragmentValueType( tree );
    tree.type = type;
    ReflectUtil.field( this, "result" ).set( type );
  }
  else
  {
    super.visitLiteral( tree );
  }
}
 
Example 9
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 4 votes vote down vote up
@Override public void visitLiteral(JCTree.JCLiteral jcLiteral) {
  try {
    int value = (Integer) jcLiteral.value;
    resourceIds.put(value, new Id(value));
  } catch (Exception ignored) { }
}