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

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCModifiers . 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: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void process( JCTree.JCModifiers modifiers, Runnable processor )
{
  Boolean disable = getDisableAnnotationValue( modifiers );
  if( disable != null )
  {
    pushDisabled( disable );
  }

  try
  {
    // processor
    processor.run();
  }
  finally
  {
    if( disable != null )
    {
      popDisabled( disable );
    }
  }
}
 
Example 2
Source File: TreeTypeInternal.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
public TypeUse.TypeInternal forReturn(ProcessingEnvironment env, ExecutableElement methodElt) {
  Trees trees = Trees.instance(env);
  if (trees == null) {
    return null;
  }
  JCTree.JCMethodDecl tree = (JCTree.JCMethodDecl) trees.getTree(methodElt);
  if (tree == null) {
    return null;
  }
  JCTree type = tree.getReturnType();
  boolean nullable = isNullable(type);
  if (!nullable) {
    JCTree.JCModifiers mods = tree.mods;
    for (JCTree.JCAnnotation jca : mods.annotations) {
      if (jca.type.toString().equals(TypeUse.NULLABLE)) {
        nullable = true;
        break;
      }
    }
  }
  return new TreeTypeInternal(type, nullable);
}
 
Example 3
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
private void generateDeleteTable(JavacTreeMaker maker, JavacNode tableElement, Set<String> allStaticMethodNames, String tableClassName) {
  final String callClassName = CLASS_DELETE_TABLE;
  final String methodName = METHOD_DELETE_TABLE;
  final String invokeKey = getCreateInvokeKey(callClassName, tableClassName);
  if (!allStaticMethodNames.contains(methodName)) {
    final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()),
        List.<JCTree.JCExpression>of(maker.Literal(invokeKey)));
    final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_STATIC, List.of(invokesAnnotation));
    final Name name = tableElement.toName(methodName);
    final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName);
    final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement);
    final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType,
        List.<JCTree.JCTypeParameter>nil(),
        List.<JCTree.JCVariableDecl>nil(),
        List.<JCTree.JCExpression>nil(),
        body,
        null);
    injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext()));
  }
}
 
Example 4
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
private void generateMethod(JavacTreeMaker maker, JavacNode tableElement, Set<String> allMethodNames, String methodName, String callClassName, String tableClassName) {
  final String invokeKey = getCreateInvokeKey(callClassName, tableClassName);
  if (!allMethodNames.contains(methodName)) {
    final JCTree.JCAssign value = maker.Assign(maker.Ident(tableElement.toName("value")),
        maker.Literal(invokeKey));
    final JCTree.JCAssign useThisAsOnlyParam = maker.Assign(maker.Ident(tableElement.toName("useThisAsOnlyParam")),
        maker.Literal(true));
    final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()),
        List.<JCTree.JCExpression>of(value, useThisAsOnlyParam));
    final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_FINAL, List.of(invokesAnnotation));
    final Name name = tableElement.toName(methodName);
    final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName);
    final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement);
    final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType,
        List.<JCTree.JCTypeParameter>nil(),
        List.<JCTree.JCVariableDecl>nil(),
        List.<JCTree.JCExpression>nil(),
        body,
        null);
    injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext()));
  }
}
 
Example 5
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
private void generateBulkMethod(JavacTreeMaker maker, JavacNode tableElement,
                                Set<String> allStaticMethodNames, String methodName,
                                String callClassName, String tableClassName,
                                CollectionType collectionType) {
  final String invokeKey = getCreateInvokeKey(callClassName, tableClassName);
  if (!allStaticMethodNames.contains(methodName)) {
    final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()),
        List.<JCTree.JCExpression>of(maker.Literal(invokeKey)));
    final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_STATIC, List.of(invokesAnnotation));
    final Name name = tableElement.toName(methodName);
    final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName);
    final JCTree.JCExpression tableClassType = chainDotsString(tableElement, tableElement.getPackageDeclaration() + "." + tableClassName);
    final JCTree.JCExpression paramType = maker.TypeApply(collectionType.genericType(tableElement), List.of(tableClassType));
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, tableElement.getContext());
    final JCTree.JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), tableElement.toName("o"), paramType, null);
    final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement);
    final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType,
        List.<JCTree.JCTypeParameter>nil(),
        List.of(param),
        List.<JCTree.JCExpression>nil(),
        body,
        null);
    injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext()));
  }
}
 
Example 6
Source File: SingletonJavacHandler.java    From tutorials with MIT License 5 votes vote down vote up
private void addPrivateConstructor(JavacNode singletonClass, JavacTreeMaker singletonTM) {
    JCTree.JCModifiers modifiers = singletonTM.Modifiers(Flags.PRIVATE);
    JCTree.JCBlock block = singletonTM.Block(0L, List.nil());
    JCTree.JCMethodDecl constructor = singletonTM.MethodDef(modifiers, singletonClass.toName("<init>"), null, List.nil(), List.nil(), List.nil(), block, null);

    JavacHandlerUtil.injectMethod(singletonClass, constructor);
}
 
Example 7
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 8
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 9
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 10
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
private static String parseModifiers(
    SourceContext context, @Nullable JCTree.JCModifiers modifiers) throws IOException {

  if (nonNull(modifiers)) {
    List<JCTree.JCAnnotation> annotations = modifiers.getAnnotations();
    if (nonNull(annotations) && annotations.size() > 0) {
      for (JCTree.JCAnnotation anno : annotations) {
        JCTree annotationType = anno.getAnnotationType();
        if (nonNull(annotationType)) {

          analyzeParsedTree(context, annotationType);
        }
        List<JCTree.JCExpression> arguments = anno.getArguments();
        if (nonNull(arguments)) {
          for (JCTree.JCExpression jcExpression : arguments) {
            analyzeParsedTree(context, jcExpression);
          }
        }
      }

      String mod = modifiers.toString();
      int lastIndexOf = mod.lastIndexOf('\n');
      return mod.substring(lastIndexOf + 1);
    }
    return modifiers.toString();
  }
  return "";
}
 
Example 11
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void analyzeInnerClassDecl(
    SourceContext context, JCTree.JCClassDecl classDecl, int startPos, int endPos)
    throws IOException {
  // innerClass
  Source src = context.source;
  Range range = Range.create(src, startPos, endPos);
  Name simpleName = classDecl.getSimpleName();

  JCTree.JCModifiers modifiers = classDecl.getModifiers();
  final String classModifiers = parseModifiers(context, modifiers);

  analyzeParsedTree(context, classDecl.getExtendsClause());
  analyzeSimpleExpressions(context, classDecl.getImplementsClause());

  Tree.Kind kind = classDecl.getKind();
  boolean isInterface = kind.equals(Tree.Kind.INTERFACE);
  boolean isEnum = kind.equals(Tree.Kind.ENUM);
  int nameStartPos = startPos;
  if (isInterface) {
    nameStartPos += 10;
  } else if (isEnum) {
    nameStartPos += 5;
  } else {
    nameStartPos += 6;
  }
  Range nameRange = Range.create(src, nameStartPos, nameStartPos + simpleName.length());

  src.getCurrentClass()
      .ifPresent(
          parent -> {
            try {
              String parentName = parent.name;
              String fqcn = parentName + ClassNameUtils.INNER_MARK + simpleName;
              ClassScope classScope = new ClassScope(fqcn, nameRange, startPos, range);
              classScope.isInterface = isInterface;
              classScope.isEnum = isEnum;
              log.trace("maybe inner class={}", classScope);
              parent.startClass(classScope);
              for (final JCTree memberTree : classDecl.getMembers()) {
                analyzeParsedTree(context, memberTree);
              }
              addClassNameIndex(
                  src,
                  classScope.range.begin.line,
                  classScope.range.begin.column,
                  classScope.getFQCN());
              Optional<ClassScope> ignore = parent.endClass();
            } catch (IOException e) {
              throw new UncheckedIOException(e);
            }
          });
}
 
Example 12
Source File: SingletonJavacHandler.java    From tutorials with MIT License 4 votes vote down vote up
private JavacNode addInnerClass(JavacNode singletonClass, JavacTreeMaker singletonTM) {
    JCTree.JCModifiers modifiers = singletonTM.Modifiers(Flags.PRIVATE | Flags.STATIC);
    String innerClassName = singletonClass.getName() + "Holder";
    JCTree.JCClassDecl innerClassDecl = singletonTM.ClassDef(modifiers, singletonClass.toName(innerClassName), List.nil(), null, List.nil(), List.nil());
    return JavacHandlerUtil.injectType(singletonClass, innerClassDecl);
}
 
Example 13
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**Checks whether the given tree represents a class.
 * @deprecated since 0.67, <code>Tree.getKind() == Kind.CLASS</code> should be used instead.
 */
@Deprecated
public boolean isClass(ClassTree tree) {
    return (((JCTree.JCModifiers)tree.getModifiers()).flags & (Flags.INTERFACE | Flags.ENUM | Flags.ANNOTATION)) == 0;
}
 
Example 14
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void analyzeTopLevelClass(SourceContext context, JCTree.JCClassDecl classDecl)
    throws IOException {
  Source src = context.source;
  EndPosTable endPosTable = context.endPosTable;

  Tree.Kind classDeclKind = classDecl.getKind();

  boolean isInterface = classDeclKind.equals(Tree.Kind.INTERFACE);
  boolean isEnum = classDeclKind.equals(Tree.Kind.ENUM);

  int startPos = classDecl.getPreferredPosition();
  int endPos = classDecl.getEndPosition(endPosTable);
  JCTree.JCModifiers modifiers = classDecl.getModifiers();
  List<JCTree.JCAnnotation> annotations = modifiers.getAnnotations();

  final String classModifiers = parseModifiers(context, modifiers);

  analyzeParsedTree(context, classDecl.getExtendsClause());

  analyzeSimpleExpressions(context, classDecl.getImplementsClause());

  Name simpleName = classDecl.getSimpleName();
  Range range = Range.create(src, startPos + 1, endPos);

  int nameStart = startPos + 6;
  if (isInterface) {
    nameStart = startPos + 10;
  } else if (isEnum) {
    nameStart = startPos + 5;
  }
  Range nameRange = Range.create(src, nameStart, nameStart + simpleName.length());

  String fqcn;
  if (src.getPackageName().isEmpty()) {
    fqcn = simpleName.toString();
  } else {
    fqcn = src.getPackageName() + '.' + simpleName.toString();
  }
  ClassScope classScope = new ClassScope(fqcn, nameRange, startPos, range);
  classScope.isEnum = isEnum;
  classScope.isInterface = isInterface;
  log.trace("class={}", classScope);
  analyzeAnnotations(context, annotations, classScope);

  src.startClass(classScope);

  for (JCTree tree : classDecl.getMembers()) {
    analyzeParsedTree(context, tree);
  }
  addClassNameIndex(
      src, classScope.range.begin.line, classScope.range.begin.column, classScope.getFQCN());
  Optional<ClassScope> endClass = src.endClass();
  log.trace("class={}", endClass);
}
 
Example 15
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**Checks whether the given tree represents an annotation.
 * @deprecated since 0.67, <code>Tree.getKind() == Kind.ANNOTATION_TYPE</code> should be used instead.
 */
@Deprecated
public boolean isAnnotation(ClassTree tree) {
    return (((JCTree.JCModifiers)tree.getModifiers()).flags & Flags.ANNOTATION) != 0;
}
 
Example 16
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks wheteher given variable tree represents an enum constant.
 */
public boolean isEnumConstant(VariableTree tree) {
    return (((JCTree.JCModifiers) tree.getModifiers()).flags & Flags.ENUM) != 0;
}
 
Example 17
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**Checks whether the given tree represents an enum.
 * @deprecated since 0.67, <code>Tree.getKind() == Kind.ENUM</code> should be used instead.
 */
@Deprecated
public boolean isEnum(ClassTree tree) {
    return (((JCTree.JCModifiers)tree.getModifiers()).flags & Flags.ENUM) != 0;
}
 
Example 18
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**Checks whether the given tree represents an interface.
 * @deprecated since 0.67, <code>Tree.getKind() == Kind.INTERFACE</code> should be used instead.
 */
@Deprecated
public boolean isInterface(ClassTree tree) {
    final long flags = ((JCTree.JCModifiers) tree.getModifiers()).flags;
    return (flags & Flags.INTERFACE) != 0 && (flags & Flags.ANNOTATION) == 0;
}