Search in sources :

Example 1 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project checkstyle by checkstyle.

the class TokenTypesDocletTest method getRootDoc.

private static RootDoc getRootDoc(JavadocTool javadocTool, ListBuffer<String[]> options, ListBuffer<String> names) throws Exception {
    final Method getRootDocImpl = getMethodGetRootDocImplByReflection();
    final RootDoc rootDoc;
    if (System.getProperty("java.version").startsWith("1.7.")) {
        rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "", "UTF-8", new ModifierFilter(ModifierFilter.ALL_ACCESS), names.toList(), options.toList(), false, new ListBuffer<String>().toList(), new ListBuffer<String>().toList(), false, false, false);
    } else {
        rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "", "UTF-8", new ModifierFilter(ModifierFilter.ALL_ACCESS), names.toList(), options.toList(), new ListBuffer<JavaFileObject>().toList(), false, new ListBuffer<String>().toList(), new ListBuffer<String>().toList(), false, false, false);
    }
    return rootDoc;
}
Also used : ListBuffer(com.sun.tools.javac.util.ListBuffer) ModifierFilter(com.sun.tools.javadoc.ModifierFilter) Method(java.lang.reflect.Method) RootDoc(com.sun.javadoc.RootDoc)

Example 2 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project checkstyle by checkstyle.

the class TokenTypesDocletTest method testNotConstants.

@Test
public void testNotConstants() throws Exception {
    // Token types must be public int constants, which means that they must have
    // modifiers public, static, final and type int, because they are referenced statically in
    // a lot of different places, must not be changed and an int value is used to encrypt
    // a token type.
    final ListBuffer<String[]> options = new ListBuffer<>();
    options.add(new String[] { "-doclet", "TokenTypesDoclet" });
    options.add(new String[] { "-destfile", "target/tokentypes.properties" });
    final ListBuffer<String> names = new ListBuffer<>();
    names.add(getPath("InputTokenTypesDocletNotConstants.java"));
    final Context context = new Context();
    new TestMessager(context);
    final JavadocTool javadocTool = JavadocTool.make0(context);
    final RootDoc rootDoc = getRootDoc(javadocTool, options, names);
    assertTrue(TokenTypesDoclet.start(rootDoc));
}
Also used : Context(com.sun.tools.javac.util.Context) ListBuffer(com.sun.tools.javac.util.ListBuffer) RootDoc(com.sun.javadoc.RootDoc) JavadocTool(com.sun.tools.javadoc.JavadocTool) Test(org.junit.Test)

Example 3 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project checkstyle by checkstyle.

the class TokenTypesDocletTest method testCorrect.

@Test
public void testCorrect() throws Exception {
    final ListBuffer<String[]> options = new ListBuffer<>();
    options.add(new String[] { "-destfile", "target/tokentypes.properties" });
    final ListBuffer<String> names = new ListBuffer<>();
    names.add(getPath("InputTokenTypesDocletCorrect.java"));
    final Context context = new Context();
    new TestMessager(context);
    final JavadocTool javadocTool = JavadocTool.make0(context);
    final RootDoc rootDoc = getRootDoc(javadocTool, options, names);
    assertTrue(TokenTypesDoclet.start(rootDoc));
}
Also used : Context(com.sun.tools.javac.util.Context) ListBuffer(com.sun.tools.javac.util.ListBuffer) RootDoc(com.sun.javadoc.RootDoc) JavadocTool(com.sun.tools.javadoc.JavadocTool) Test(org.junit.Test)

Example 4 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class HandleCleanup method handle.

@Override
public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup");
    if (inNetbeansEditor(annotationNode))
        return;
    deleteAnnotationIfNeccessary(annotationNode, Cleanup.class);
    String cleanupName = annotation.getInstance().value();
    if (cleanupName.length() == 0) {
        annotationNode.addError("cleanupName cannot be the empty string.");
        return;
    }
    if (annotationNode.up().getKind() != Kind.LOCAL) {
        annotationNode.addError("@Cleanup is legal only on local variable declarations.");
        return;
    }
    JCVariableDecl decl = (JCVariableDecl) annotationNode.up().get();
    if (decl.init == null) {
        annotationNode.addError("@Cleanup variable declarations need to be initialized.");
        return;
    }
    JavacNode ancestor = annotationNode.up().directUp();
    JCTree blockNode = ancestor.get();
    final List<JCStatement> statements;
    if (blockNode instanceof JCBlock) {
        statements = ((JCBlock) blockNode).stats;
    } else if (blockNode instanceof JCCase) {
        statements = ((JCCase) blockNode).stats;
    } else if (blockNode instanceof JCMethodDecl) {
        statements = ((JCMethodDecl) blockNode).body.stats;
    } else {
        annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block.");
        return;
    }
    boolean seenDeclaration = false;
    ListBuffer<JCStatement> newStatements = new ListBuffer<JCStatement>();
    ListBuffer<JCStatement> tryBlock = new ListBuffer<JCStatement>();
    for (JCStatement statement : statements) {
        if (!seenDeclaration) {
            if (statement == decl)
                seenDeclaration = true;
            newStatements.append(statement);
        } else {
            tryBlock.append(statement);
        }
    }
    if (!seenDeclaration) {
        annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent.");
        return;
    }
    doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name);
    JavacTreeMaker maker = annotationNode.getTreeMaker();
    JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName));
    List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec(maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil())));
    JCExpression preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name));
    JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null));
    JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null);
    Context context = annotationNode.getContext();
    JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast, context);
    newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast, context), List.<JCCatch>nil(), finalizer), ast, context));
    if (blockNode instanceof JCBlock) {
        ((JCBlock) blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCCase) {
        ((JCCase) blockNode).stats = newStatements.toList();
    } else if (blockNode instanceof JCMethodDecl) {
        ((JCMethodDecl) blockNode).body.stats = newStatements.toList();
    } else
        throw new AssertionError("Should not get here");
    ancestor.rebuild();
}
Also used : Context(com.sun.tools.javac.util.Context) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCIf(com.sun.tools.javac.tree.JCTree.JCIf) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

Example 5 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class HandleConstructor method createStaticConstructor.

public JCMethodDecl createStaticConstructor(String name, AccessLevel level, JavacNode typeNode, List<JavacNode> fields, JCTree source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCClassDecl type = (JCClassDecl) typeNode.get();
    JCModifiers mods = maker.Modifiers(Flags.STATIC | toJavacModifier(level));
    JCExpression returnType, constructorType;
    ListBuffer<JCTypeParameter> typeParams = new ListBuffer<JCTypeParameter>();
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    ListBuffer<JCExpression> typeArgs1 = new ListBuffer<JCExpression>();
    ListBuffer<JCExpression> typeArgs2 = new ListBuffer<JCExpression>();
    ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
    if (!type.typarams.isEmpty()) {
        for (JCTypeParameter param : type.typarams) {
            typeArgs1.append(maker.Ident(param.name));
            typeArgs2.append(maker.Ident(param.name));
            typeParams.append(maker.TypeParameter(param.name, param.bounds));
        }
        returnType = maker.TypeApply(maker.Ident(type.name), typeArgs1.toList());
        constructorType = maker.TypeApply(maker.Ident(type.name), typeArgs2.toList());
    } else {
        returnType = maker.Ident(type.name);
        constructorType = maker.Ident(type.name);
    }
    for (JavacNode fieldNode : fields) {
        JCVariableDecl field = (JCVariableDecl) fieldNode.get();
        Name fieldName = removePrefixFromField(fieldNode);
        JCExpression pType = cloneType(maker, field.vartype, source, typeNode.getContext());
        List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
        List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
        long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
        JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, pType, null);
        params.append(param);
        args.append(maker.Ident(fieldName));
    }
    JCReturn returnStatement = maker.Return(maker.NewClass(null, List.<JCExpression>nil(), constructorType, args.toList(), null));
    JCBlock body = maker.Block(0, List.<JCStatement>of(returnStatement));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName(name), returnType, typeParams.toList(), params.toList(), List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

ListBuffer (com.sun.tools.javac.util.ListBuffer)96 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)61 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)33 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)32 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)29 Name (com.sun.tools.javac.util.Name)26 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)24 JCTree (com.sun.tools.javac.tree.JCTree)22 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)21 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)18 JavacNode (lombok.javac.JavacNode)18 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)17 JavacTreeMaker (lombok.javac.JavacTreeMaker)16 Type (com.redhat.ceylon.model.typechecker.model.Type)12 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)11 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)8 List (com.sun.tools.javac.util.List)7 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)6 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)6 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)6