From 12771880260d57209afcda15fd2b00f3181c38a6 Mon Sep 17 00:00:00 2001 From: Robbert Jan Grootjans Date: Fri, 8 Mar 2013 16:52:14 +0100 Subject: Added switch to CommentsCatcher. Assumption, until proven otherwise is that JDK8 behaves similar to JDK7. --- src/utils/lombok/javac/CommentCatcher.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/utils/lombok/javac/CommentCatcher.java') diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index 474dc43d..e3754627 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -64,8 +64,10 @@ public class CommentCatcher { try { if (JavaCompiler.version().startsWith("1.6")) { Class.forName("lombok.javac.java6.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context); - } else { + } else if (JavaCompiler.version().startsWith("1.7") || JavaCompiler.version().startsWith("1.8")) { Class.forName("lombok.javac.java7.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context); + } else { + throw new IllegalStateException("No comments parser for compiler version " + JavaCompiler.version()); } } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException)e; @@ -79,10 +81,13 @@ public class CommentCatcher { if (JavaCompiler.version().startsWith("1.6")) { Class parserFactory = Class.forName("lombok.javac.java6.CommentCollectingParserFactory"); parserFactory.getMethod("setInCompiler",JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); - } else { + } else if (JavaCompiler.version().startsWith("1.7") || JavaCompiler.version().startsWith("1.8")) { Class parserFactory = Class.forName("lombok.javac.java7.CommentCollectingParserFactory"); parserFactory.getMethod("setInCompiler",JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); + } else { + throw new IllegalStateException("No comments parser for compiler version " + JavaCompiler.version()); } + } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException)e; throw new RuntimeException(e); -- cgit From 1b4d8669c48af215c38a73f6dfd15741619a1c45 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 23 Jul 2013 00:54:59 +0200 Subject: A source file with just @Getter in it now compiles in javac8, but there is still a looooong way to go. --- src/core/lombok/javac/JavacAST.java | 2 +- .../javac/handlers/HandleEqualsAndHashCode.java | 2 +- .../lombok/javac/handlers/HandleSneakyThrows.java | 3 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 4 +- .../lombok/delombok/PrettyCommentsPrinter.java | 4 +- src/utils/lombok/javac/CommentCatcher.java | 10 +- src/utils/lombok/javac/Javac.java | 126 ++++++++++++++++----- 7 files changed, 109 insertions(+), 42 deletions(-) (limited to 'src/utils/lombok/javac/CommentCatcher.java') diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 36c51210..04d7540f 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -360,7 +360,7 @@ public class JavacAST extends AST { void removeDeferredErrors(JavacNode node) { DiagnosticPosition pos = node.get().pos(); JCCompilationUnit top = (JCCompilationUnit) top().get(); - removeFromDeferredDiagnostics(pos.getStartPosition(), pos.getEndPosition(top.endPositions)); + removeFromDeferredDiagnostics(pos.getStartPosition(), Javac.getEndPosition(pos, top)); } /** Supply either a position or a node (in that case, position of the node is used) */ diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 741e7e21..f5f1bbd6 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -139,7 +139,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler { JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL), node.toName("$ex"), varType, null); JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow"); - JCBlock catchBody = maker.Block(0, List.of(maker.Throw(maker.Apply( + JCBlock catchBody = maker.Block(0, List.of(Javac.makeThrow(maker, maker.Apply( List.nil(), lombokLombokSneakyThrowNameRef, List.of(maker.Ident(node.toName("$ex"))))))); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 211f5d36..a9c47e78 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -946,8 +946,8 @@ public class JavacHandlerUtil { if (isPrimitive(varDecl.vartype)) return null; Name fieldName = varDecl.name; JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException"); - JCTree exception = maker.NewClass(null, List.nil(), npe, List.of(maker.Literal(fieldName.toString())), null); - JCStatement throwStatement = maker.Throw(exception); + JCExpression exception = maker.NewClass(null, List.nil(), npe, List.of(maker.Literal(fieldName.toString())), null); + JCStatement throwStatement = Javac.makeThrow(maker, exception); JCBlock throwBlock = maker.Block(0, List.of(throwStatement)); return maker.If(Javac.makeBinary(maker, CTC_EQUAL, maker.Ident(fieldName), Javac.makeLiteral(maker, CTC_BOT, null)), throwBlock, null); } diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index 075cc64f..481380f7 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -742,9 +742,9 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { else print("class " + tree.name); printTypeParameters(tree.typarams); - if (tree.getExtendsClause() != null) { + if (Javac.getExtendsClause(tree) != null) { print(" extends "); - printExpr(tree.getExtendsClause()); + printExpr(Javac.getExtendsClause(tree)); } if (tree.implementing.nonEmpty()) { print(" implements "); diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index 8d1e71c0..eb747554 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -25,8 +25,6 @@ import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.WeakHashMap; -import lombok.Lombok; - import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; @@ -73,9 +71,9 @@ public class CommentCatcher { } scannerFactory.getMethod("preRegister", Context.class).invoke(null, context); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw Javac.sneakyThrow(e.getCause()); } catch (Exception e) { - throw Lombok.sneakyThrow(e); + throw Javac.sneakyThrow(e); } } @@ -89,9 +87,9 @@ public class CommentCatcher { } parserFactory.getMethod("setInCompiler", JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw Javac.sneakyThrow(e.getCause()); } catch (Exception e) { - throw Lombok.sneakyThrow(e); + throw Javac.sneakyThrow(e); } } } diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java index dfa51e00..1f2a7031 100644 --- a/src/utils/lombok/javac/Javac.java +++ b/src/utils/lombok/javac/Javac.java @@ -35,22 +35,23 @@ import javax.lang.model.type.NoType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeVisitor; -import lombok.Lombok; - import com.sun.tools.javac.code.Type; import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCBinary; import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCLiteral; import com.sun.tools.javac.tree.JCTree.JCModifiers; import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree; +import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCTypeParameter; import com.sun.tools.javac.tree.JCTree.JCUnary; import com.sun.tools.javac.tree.TreeMaker; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.Name; @@ -62,6 +63,9 @@ public class Javac { // prevent instantiation } + private static final ConcurrentMap TYPE_TAG_CACHE = new ConcurrentHashMap(); + private static final ConcurrentMap TREE_TAG_CACHE = new ConcurrentHashMap(); + /** Matches any of the 8 primitive names, such as {@code boolean}. */ private static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile("^(boolean|byte|short|int|long|float|double|char)$"); @@ -148,10 +152,6 @@ public class Javac { return ctc1 == null ? ctc2 == null : ctc1.equals(ctc2); } - private static final ConcurrentMap TYPE_TAG_CACHE = new ConcurrentHashMap(); - private static final ConcurrentMap TREE_TAG_CACHE = new ConcurrentHashMap(); - - /** * Retrieves the provided TypeTag value, in a compiler version independent manner. * @@ -168,7 +168,7 @@ public class Javac { * @return the value of the typetag constant (either enum instance or an Integer object). */ public static Object getTypeTag(String identifier) { - return getFieldCached(TYPE_TAG_CACHE, getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.code.TypeTag" : "com.sun.tools.javac.code.TypeTags", identifier); + return getFieldCached(TYPE_TAG_CACHE, getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.code.TypeTags" : "com.sun.tools.javac.code.TypeTag", identifier); } public static Object getTreeTag(String identifier) { @@ -181,11 +181,11 @@ public class Javac { try { value = Class.forName(className).getField(fieldName).get(null); } catch (NoSuchFieldException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (ClassNotFoundException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } cache.putIfAbsent(fieldName, value); @@ -200,8 +200,8 @@ public class Javac { return tree.typetag; } - private static final Method createIdent, createLiteral, createUnary, createBinary; - + private static final Method createIdent, createLiteral, createUnary, createBinary, createThrow, getExtendsClause, getEndPosition; + static { if (getJavaCompilerVersion() < 8) { createIdent = getMethod(TreeMaker.class, "TypeIdent", int.class); @@ -220,23 +220,40 @@ public class Javac { if (getJavaCompilerVersion() < 8) { createUnary = getMethod(TreeMaker.class, "Unary", int.class, JCExpression.class); } else { - createUnary = getMethod(TreeMaker.class, "Unary", "com.sun.tools.javac.code.TypeTag", JCExpression.class.getName()); + createUnary = getMethod(TreeMaker.class, "Unary", "com.sun.tools.javac.tree.JCTree$Tag", JCExpression.class.getName()); } createUnary.setAccessible(true); if (getJavaCompilerVersion() < 8) { createBinary = getMethod(TreeMaker.class, "Binary", Integer.TYPE, JCExpression.class, JCExpression.class); } else { - createBinary = getMethod(TreeMaker.class, "Binary", "com.sun.tools.javac.code.TypeTag", JCExpression.class.getName(), JCExpression.class.getName()); + createBinary = getMethod(TreeMaker.class, "Binary", "com.sun.tools.javac.tree.JCTree$Tag", JCExpression.class.getName(), JCExpression.class.getName()); + } + createBinary.setAccessible(true); + + if (getJavaCompilerVersion() < 8) { + createThrow = getMethod(TreeMaker.class, "Throw", JCTree.class); + } else { + createThrow = getMethod(TreeMaker.class, "Throw", JCExpression.class); } createBinary.setAccessible(true); + + getExtendsClause = getMethod(JCClassDecl.class, "getExtendsClause", new Class[0]); + getExtendsClause.setAccessible(true); + + if (getJavaCompilerVersion() < 8) { + getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", java.util.Map.class); + } else { + getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", "com.sun.tools.javac.tree.EndPosTable"); + } + getEndPosition.setAccessible(true); } private static Method getMethod(Class clazz, String name, Class... paramTypes) { try { return clazz.getMethod(name, paramTypes); } catch (NoSuchMethodException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } } @@ -246,9 +263,9 @@ public class Javac { for (int i = 0; i < paramTypes.length; i++) c[i] = Class.forName(paramTypes[i]); return clazz.getMethod(name, c); } catch (NoSuchMethodException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (ClassNotFoundException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } } @@ -256,9 +273,9 @@ public class Javac { try { return (JCExpression) createIdent.invoke(maker, ctc); } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw sneakyThrow(e.getCause()); } } @@ -266,9 +283,9 @@ public class Javac { try { return (JCLiteral) createLiteral.invoke(maker, ctc, argument); } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw sneakyThrow(e.getCause()); } } @@ -276,9 +293,9 @@ public class Javac { try { return (JCUnary) createUnary.invoke(maker, ctc, argument); } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw sneakyThrow(e.getCause()); } } @@ -286,9 +303,48 @@ public class Javac { try { return (JCBinary) createBinary.invoke(maker, ctc, lhsArgument, rhsArgument); } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); + } catch (InvocationTargetException e) { + throw sneakyThrow(e.getCause()); + } + } + + public static JCStatement makeThrow(TreeMaker maker, JCExpression expression) { + try { + return (JCStatement) createThrow.invoke(maker, expression); + } catch (IllegalAccessException e) { + throw sneakyThrow(e); } catch (InvocationTargetException e) { - throw Lombok.sneakyThrow(e.getCause()); + throw sneakyThrow(e.getCause()); + } + } + + public static JCTree getExtendsClause(JCClassDecl decl) { + try { + return (JCTree) getExtendsClause.invoke(decl); + } catch (IllegalAccessException e) { + throw sneakyThrow(e); + } catch (InvocationTargetException e) { + throw sneakyThrow(e.getCause()); + } + } + + public static Object getDocComments(JCCompilationUnit cu) { + try { + return JCCOMPILATIONUNIT_DOCCOMMENTS.get(cu); + } catch (IllegalAccessException e) { + throw sneakyThrow(e); + } + } + + public static int getEndPosition(DiagnosticPosition pos, JCCompilationUnit top) { + try { + Object endPositions = JCCOMPILATIONUNIT_ENDPOSITIONS.get(top); + return (Integer) getEndPosition.invoke(pos, endPositions); + } catch (IllegalAccessException e) { + throw sneakyThrow(e); + } catch (InvocationTargetException e) { + throw sneakyThrow(e.getCause()); } } @@ -318,9 +374,9 @@ public class Javac { return (Type) JC_NO_TYPE.newInstance(); } } catch (IllegalAccessException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } catch (InstantiationException e) { - throw Lombok.sneakyThrow(e); + throw sneakyThrow(e); } } } @@ -343,7 +399,7 @@ public class Javac { } } - private static final Field JCTREE_TAG, JCLITERAL_TYPETAG, JCPRIMITIVETYPETREE_TYPETAG; + private static final Field JCTREE_TAG, JCLITERAL_TYPETAG, JCPRIMITIVETYPETREE_TYPETAG, JCCOMPILATIONUNIT_ENDPOSITIONS, JCCOMPILATIONUNIT_DOCCOMMENTS; private static final Method JCTREE_GETTAG; static { Field f = null; @@ -364,6 +420,18 @@ public class Javac { } catch (NoSuchFieldException e) {} JCPRIMITIVETYPETREE_TYPETAG = f; + f = null; + try { + f = JCCompilationUnit.class.getDeclaredField("endPositions"); + } catch (NoSuchFieldException e) {} + JCCOMPILATIONUNIT_ENDPOSITIONS = f; + + f = null; + try { + f = JCCompilationUnit.class.getDeclaredField("docComments"); + } catch (NoSuchFieldException e) {} + JCCOMPILATIONUNIT_DOCCOMMENTS = f; + Method m = null; try { m = JCTree.class.getDeclaredMethod("getTag"); @@ -424,7 +492,7 @@ public class Javac { } } - private static RuntimeException sneakyThrow(Throwable t) { + static RuntimeException sneakyThrow(Throwable t) { if (t == null) throw new NullPointerException("t"); Javac.sneakyThrow0(t); return null; -- cgit From 132d603dd4e43f50555ef33bac290b1080dfc5fa Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Tue, 13 Aug 2013 00:24:51 +0200 Subject: Getting the java8 compiler remember the comments --- src/stubs/com/sun/tools/javac/parser/Scanner.java | 3 + src/utils/lombok/javac/CommentCatcher.java | 14 +- .../javac/java6/CommentCollectingScanner.java | 3 +- .../java6/CommentCollectingScannerFactory.java | 2 +- .../lombok/javac/java6/DocCommentScanner.java | 461 --------------------- .../javac/java7/CommentCollectingScanner.java | 4 +- .../javac/java8/CommentCollectingParser.java | 32 ++ .../java8/CommentCollectingParserFactory.java | 52 +++ .../javac/java8/CommentCollectingScanner.java | 21 + .../java8/CommentCollectingScannerFactory.java | 89 ++++ .../javac/java8/CommentCollectingTokenizer.java | 108 +++++ 11 files changed, 320 insertions(+), 469 deletions(-) delete mode 100644 src/utils/lombok/javac/java6/DocCommentScanner.java create mode 100644 src/utils/lombok/javac/java8/CommentCollectingParser.java create mode 100644 src/utils/lombok/javac/java8/CommentCollectingParserFactory.java create mode 100644 src/utils/lombok/javac/java8/CommentCollectingScanner.java create mode 100644 src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java create mode 100644 src/utils/lombok/javac/java8/CommentCollectingTokenizer.java (limited to 'src/utils/lombok/javac/CommentCatcher.java') diff --git a/src/stubs/com/sun/tools/javac/parser/Scanner.java b/src/stubs/com/sun/tools/javac/parser/Scanner.java index 266208e5..36c93df7 100644 --- a/src/stubs/com/sun/tools/javac/parser/Scanner.java +++ b/src/stubs/com/sun/tools/javac/parser/Scanner.java @@ -20,6 +20,9 @@ public class Scanner implements Lexer { protected Scanner(ScannerFactory fac, char[] input, int inputLength) { } + protected Scanner(ScannerFactory fac, JavaTokenizer tokenizer) { + } + public static class Factory { public static final Context.Key scannerFactoryKey = null; diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index eb747554..48dd7e75 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -64,10 +64,13 @@ public class CommentCatcher { private static void registerCommentsCollectingScannerFactory(Context context) { try { Class scannerFactory; - if (Javac.getJavaCompilerVersion() <= 6) { + int javaCompilerVersion = Javac.getJavaCompilerVersion(); + if (javaCompilerVersion <= 6) { scannerFactory = Class.forName("lombok.javac.java6.CommentCollectingScannerFactory"); - } else { + } else if (javaCompilerVersion == 7) { scannerFactory = Class.forName("lombok.javac.java7.CommentCollectingScannerFactory"); + } else { + scannerFactory = Class.forName("lombok.javac.java8.CommentCollectingScannerFactory"); } scannerFactory.getMethod("preRegister", Context.class).invoke(null, context); } catch (InvocationTargetException e) { @@ -80,10 +83,13 @@ public class CommentCatcher { private static void setInCompiler(JavaCompiler compiler, Context context, Map> commentsMap) { try { Class parserFactory; - if (Javac.getJavaCompilerVersion() <= 6) { + int javaCompilerVersion = Javac.getJavaCompilerVersion(); + if (javaCompilerVersion <= 6) { parserFactory = Class.forName("lombok.javac.java6.CommentCollectingParserFactory"); - } else { + } else if (javaCompilerVersion == 7) { parserFactory = Class.forName("lombok.javac.java7.CommentCollectingParserFactory"); + } else { + parserFactory = Class.forName("lombok.javac.java8.CommentCollectingParserFactory"); } parserFactory.getMethod("setInCompiler", JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); } catch (InvocationTargetException e) { diff --git a/src/utils/lombok/javac/java6/CommentCollectingScanner.java b/src/utils/lombok/javac/java6/CommentCollectingScanner.java index b584ec16..5d820ba0 100644 --- a/src/utils/lombok/javac/java6/CommentCollectingScanner.java +++ b/src/utils/lombok/javac/java6/CommentCollectingScanner.java @@ -27,10 +27,11 @@ import lombok.javac.CommentInfo; import lombok.javac.CommentInfo.EndConnection; import lombok.javac.CommentInfo.StartConnection; +import com.sun.tools.javac.parser.Scanner; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.ListBuffer; -public class CommentCollectingScanner extends DocCommentScanner { +public class CommentCollectingScanner extends Scanner { private final ListBuffer comments = ListBuffer.lb(); private int endComment = 0; diff --git a/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java index f3d6bd72..b7d8ed13 100644 --- a/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java +++ b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java @@ -26,7 +26,7 @@ import java.nio.CharBuffer; import com.sun.tools.javac.parser.Scanner; import com.sun.tools.javac.util.Context; -public class CommentCollectingScannerFactory extends DocCommentScanner.Factory { +public class CommentCollectingScannerFactory extends Scanner.Factory { @SuppressWarnings("all") public static void preRegister(final Context context) { diff --git a/src/utils/lombok/javac/java6/DocCommentScanner.java b/src/utils/lombok/javac/java6/DocCommentScanner.java deleted file mode 100644 index ff3eadd4..00000000 --- a/src/utils/lombok/javac/java6/DocCommentScanner.java +++ /dev/null @@ -1,461 +0,0 @@ -package lombok.javac.java6; - -/* - * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Sun designates this - * particular file as subject to the "Classpath" exception as provided - * by Sun in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, - * CA 95054 USA or visit www.sun.com if you need additional information or - * have any questions. - */ - -import static com.sun.tools.javac.util.LayoutCharacters.*; - -import java.nio.CharBuffer; - -import com.sun.tools.javac.parser.Scanner; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.Position; - -/** An extension to the base lexical analyzer that captures - * and processes the contents of doc comments. It does so by - * translating Unicode escape sequences and by stripping the - * leading whitespace and starts from each line of the comment. - * - *

This is NOT part of any API supported by Sun Microsystems. If - * you write code that depends on this, you do so at your own risk. - * This code and its internal interfaces are subject to change or - * deletion without notice. - */ -public class DocCommentScanner extends Scanner { - - /** A factory for creating scanners. */ - public static class Factory extends Scanner.Factory { - - @SuppressWarnings({"unchecked", "all"}) - public static void preRegister(final Context context) { - context.put(scannerFactoryKey, new Context.Factory() { - public Object make() { - return new Factory(context); - } - - public Object make(Context c) { - return new Factory(c); - } - }); - } - - /** Create a new scanner factory. */ - protected Factory(Context context) { - super(context); - } - - @Override - public Scanner newScanner(CharSequence input) { - if (input instanceof CharBuffer) { - return new DocCommentScanner(this, (CharBuffer)input); - } else { - char[] array = input.toString().toCharArray(); - return newScanner(array, array.length); - } - } - - @Override - public Scanner newScanner(char[] input, int inputLength) { - return new DocCommentScanner(this, input, inputLength); - } - } - - - /** Create a scanner from the input buffer. buffer must implement - * array() and compact(), and remaining() must be less than limit(). - */ - protected DocCommentScanner(Factory fac, CharBuffer buffer) { - super(fac, buffer); - } - - /** Create a scanner from the input array. The array must have at - * least a single character of extra space. - */ - protected DocCommentScanner(Factory fac, char[] input, int inputLength) { - super(fac, input, inputLength); - } - - /** Starting position of the comment in original source - */ - private int pos; - - /** The comment input buffer, index of next chacter to be read, - * index of one past last character in buffer. - */ - private char[] buf; - private int bp; - private int buflen; - - /** The current character. - */ - private char ch; - - /** The column number position of the current character. - */ - private int col; - - /** The buffer index of the last converted Unicode character - */ - private int unicodeConversionBp = 0; - - /** - * Buffer for doc comment. - */ - private char[] docCommentBuffer = new char[1024]; - - /** - * Number of characters in doc comment buffer. - */ - private int docCommentCount; - - /** - * Translated and stripped contents of doc comment - */ - private String docComment = null; - - - /** Unconditionally expand the comment buffer. - */ - private void expandCommentBuffer() { - char[] newBuffer = new char[docCommentBuffer.length * 2]; - System.arraycopy(docCommentBuffer, 0, newBuffer, - 0, docCommentBuffer.length); - docCommentBuffer = newBuffer; - } - - /** Convert an ASCII digit from its base (8, 10, or 16) - * to its value. - */ - private int digit(int base) { - char c = ch; - int result = Character.digit(c, base); - if (result >= 0 && c > 0x7f) { - ch = "0123456789abcdef".charAt(result); - } - return result; - } - - /** Convert Unicode escape; bp points to initial '\' character - * (Spec 3.3). - */ - private void convertUnicode() { - if (ch == '\\' && unicodeConversionBp != bp) { - bp++; ch = buf[bp]; col++; - if (ch == 'u') { - do { - bp++; ch = buf[bp]; col++; - } while (ch == 'u'); - int limit = bp + 3; - if (limit < buflen) { - int d = digit(16); - int code = d; - while (bp < limit && d >= 0) { - bp++; ch = buf[bp]; col++; - d = digit(16); - code = (code << 4) + d; - } - if (d >= 0) { - ch = (char)code; - unicodeConversionBp = bp; - return; - } - } - // "illegal.Unicode.esc", reported by base scanner - } else { - bp--; - ch = '\\'; - col--; - } - } - } - - - /** Read next character. - */ - private void scanChar() { - bp++; - ch = buf[bp]; - switch (ch) { - case '\r': // return - col = 0; - break; - case '\n': // newline - if (bp == 0 || buf[bp-1] != '\r') { - col = 0; - } - break; - case '\t': // tab - col = (col / TabInc * TabInc) + TabInc; - break; - case '\\': // possible Unicode - col++; - convertUnicode(); - break; - default: - col++; - break; - } - } - - /** - * Read next character in doc comment, skipping over double '\' characters. - * If a double '\' is skipped, put in the buffer and update buffer count. - */ - private void scanDocCommentChar() { - scanChar(); - if (ch == '\\') { - if (buf[bp+1] == '\\' && unicodeConversionBp != bp) { - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = ch; - bp++; col++; - } else { - convertUnicode(); - } - } - } - - /* Reset doc comment before reading each new token - */ - public void nextToken() { - docComment = null; - super.nextToken(); - } - - /** - * Returns the documentation string of the current token. - */ - public String docComment() { - return docComment; - } - - /** - * Process a doc comment and make the string content available. - * Strips leading whitespace and stars. - */ - @SuppressWarnings("fallthrough") - protected void processComment(CommentStyle style) { - if (style != CommentStyle.JAVADOC) { - return; - } - - pos = pos(); - buf = getRawCharacters(pos, endPos()); - buflen = buf.length; - bp = 0; - col = 0; - - docCommentCount = 0; - - boolean firstLine = true; - - // Skip over first slash - scanDocCommentChar(); - // Skip over first star - scanDocCommentChar(); - - // consume any number of stars - while (bp < buflen && ch == '*') { - scanDocCommentChar(); - } - // is the comment in the form /**/, /***/, /****/, etc. ? - if (bp < buflen && ch == '/') { - docComment = ""; - return; - } - - // skip a newline on the first line of the comment. - if (bp < buflen) { - if (ch == LF) { - scanDocCommentChar(); - firstLine = false; - } else if (ch == CR) { - scanDocCommentChar(); - if (ch == LF) { - scanDocCommentChar(); - firstLine = false; - } - } - } - - outerLoop: - - // The outerLoop processes the doc comment, looping once - // for each line. For each line, it first strips off - // whitespace, then it consumes any stars, then it - // puts the rest of the line into our buffer. - while (bp < buflen) { - - // The wsLoop consumes whitespace from the beginning - // of each line. - wsLoop: - - while (bp < buflen) { - switch(ch) { - case ' ': - scanDocCommentChar(); - break; - case '\t': - col = ((col - 1) / TabInc * TabInc) + TabInc; - scanDocCommentChar(); - break; - case FF: - col = 0; - scanDocCommentChar(); - break; -// Treat newline at beginning of line (blank line, no star) -// as comment text. Old Javadoc compatibility requires this. -/*---------------------------------* - case CR: // (Spec 3.4) - scanDocCommentChar(); - if (ch == LF) { - col = 0; - scanDocCommentChar(); - } - break; - case LF: // (Spec 3.4) - scanDocCommentChar(); - break; -*---------------------------------*/ - default: - // we've seen something that isn't whitespace; - // jump out. - break wsLoop; - } - } - - // Are there stars here? If so, consume them all - // and check for the end of comment. - if (ch == '*') { - // skip all of the stars - do { - scanDocCommentChar(); - } while (ch == '*'); - - // check for the closing slash. - if (ch == '/') { - // We're done with the doc comment - // scanChar() and breakout. - break outerLoop; - } - } else if (! firstLine) { - //The current line does not begin with a '*' so we will indent it. - for (int i = 1; i < col; i++) { - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = ' '; - } - } - - // The textLoop processes the rest of the characters - // on the line, adding them to our buffer. - textLoop: - while (bp < buflen) { - switch (ch) { - case '*': - // Is this just a star? Or is this the - // end of a comment? - scanDocCommentChar(); - if (ch == '/') { - // This is the end of the comment, - // set ch and return our buffer. - break outerLoop; - } - // This is just an ordinary star. Add it to - // the buffer. - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = '*'; - break; - case ' ': - case '\t': - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = ch; - scanDocCommentChar(); - break; - case FF: - scanDocCommentChar(); - break textLoop; // treat as end of line - case CR: // (Spec 3.4) - scanDocCommentChar(); - if (ch != LF) { - // Canonicalize CR-only line terminator to LF - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = (char)LF; - break textLoop; - } - /* fall through to LF case */ - case LF: // (Spec 3.4) - // We've seen a newline. Add it to our - // buffer and break out of this loop, - // starting fresh on a new line. - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = ch; - scanDocCommentChar(); - break textLoop; - default: - // Add the character to our buffer. - if (docCommentCount == docCommentBuffer.length) - expandCommentBuffer(); - docCommentBuffer[docCommentCount++] = ch; - scanDocCommentChar(); - } - } // end textLoop - firstLine = false; - } // end outerLoop - - if (docCommentCount > 0) { - int i = docCommentCount - 1; - trailLoop: - while (i > -1) { - switch (docCommentBuffer[i]) { - case '*': - i--; - break; - default: - break trailLoop; - } - } - docCommentCount = i + 1; - - // Store the text of the doc comment - docComment = new String(docCommentBuffer, 0 , docCommentCount); - } else { - docComment = ""; - } - } - - /** Build a map for translating between line numbers and - * positions in the input. - * - * @return a LineMap */ - public Position.LineMap getLineMap() { - char[] buf = getRawCharacters(); - return Position.makeLineMap(buf, buf.length, true); - } -} diff --git a/src/utils/lombok/javac/java7/CommentCollectingScanner.java b/src/utils/lombok/javac/java7/CommentCollectingScanner.java index 6ebd3ac1..86c474ea 100644 --- a/src/utils/lombok/javac/java7/CommentCollectingScanner.java +++ b/src/utils/lombok/javac/java7/CommentCollectingScanner.java @@ -27,11 +27,11 @@ import lombok.javac.CommentInfo; import lombok.javac.CommentInfo.EndConnection; import lombok.javac.CommentInfo.StartConnection; +import com.sun.tools.javac.parser.Scanner; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.ListBuffer; -import com.sun.tools.javac.parser.DocCommentScanner; -public class CommentCollectingScanner extends DocCommentScanner { +public class CommentCollectingScanner extends Scanner { private final ListBuffer comments = ListBuffer.lb(); private int endComment = 0; diff --git a/src/utils/lombok/javac/java8/CommentCollectingParser.java b/src/utils/lombok/javac/java8/CommentCollectingParser.java new file mode 100644 index 00000000..6cf65dca --- /dev/null +++ b/src/utils/lombok/javac/java8/CommentCollectingParser.java @@ -0,0 +1,32 @@ +package lombok.javac.java8; + +import java.util.List; +import java.util.Map; + +import lombok.javac.CommentInfo; + +import com.sun.tools.javac.parser.JavacParser; +import com.sun.tools.javac.parser.Lexer; +import com.sun.tools.javac.parser.ParserFactory; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; + +class CommentCollectingParser extends JavacParser { + private final Map> commentsMap; + private final Lexer lexer; + + protected CommentCollectingParser(ParserFactory fac, Lexer S, + boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions, Map> commentsMap) { + super(fac, S, keepDocComments, keepLineMap, keepEndPositions); + lexer = S; + this.commentsMap = commentsMap; + } + + public JCCompilationUnit parseCompilationUnit() { + JCCompilationUnit result = super.parseCompilationUnit(); + if (lexer instanceof CommentCollectingScanner) { + List comments = ((CommentCollectingScanner)lexer).getComments(); + commentsMap.put(result, comments); + } + return result; + } +} \ No newline at end of file diff --git a/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java b/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java new file mode 100644 index 00000000..594ff8ba --- /dev/null +++ b/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java @@ -0,0 +1,52 @@ +package lombok.javac.java8; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Map; + +import lombok.javac.CommentInfo; + +import com.sun.tools.javac.main.JavaCompiler; +import com.sun.tools.javac.parser.JavacParser; +import com.sun.tools.javac.parser.Lexer; +import com.sun.tools.javac.parser.ParserFactory; +import com.sun.tools.javac.parser.ScannerFactory; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.util.Context; + +public class CommentCollectingParserFactory extends ParserFactory { + private final Map> commentsMap; + private final Context context; + + static Context.Key key() { + return parserFactoryKey; + } + + protected CommentCollectingParserFactory(Context context, Map> commentsMap) { + super(context); + this.context = context; + this.commentsMap = commentsMap; + } + + public JavacParser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) { + ScannerFactory scannerFactory = ScannerFactory.instance(context); + Lexer lexer = scannerFactory.newScanner(input, true); + Object x = new CommentCollectingParser(this, lexer, true, keepLineMap, keepEndPos, commentsMap); + return (JavacParser) x; + // CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either + //javac6's EndPosParser which extends Parser, or javac8's JavacParser which implements Parser. + //Either way this will work out. + } + + public static void setInCompiler(JavaCompiler compiler, Context context, Map> commentsMap) { + context.put(CommentCollectingParserFactory.key(), (ParserFactory)null); + Field field; + try { + field = JavaCompiler.class.getDeclaredField("parserFactory"); + field.setAccessible(true); + field.set(compiler, new CommentCollectingParserFactory(context, commentsMap)); + } catch (Exception e) { + throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e); + } + } +} \ No newline at end of file diff --git a/src/utils/lombok/javac/java8/CommentCollectingScanner.java b/src/utils/lombok/javac/java8/CommentCollectingScanner.java new file mode 100644 index 00000000..1b8474ac --- /dev/null +++ b/src/utils/lombok/javac/java8/CommentCollectingScanner.java @@ -0,0 +1,21 @@ +package lombok.javac.java8; + +import lombok.javac.CommentInfo; + +import com.sun.tools.javac.parser.Scanner; +import com.sun.tools.javac.parser.ScannerFactory; +import com.sun.tools.javac.util.List; + +public class CommentCollectingScanner extends Scanner { + + private CommentCollectingTokenizer tokenizer; + + public CommentCollectingScanner(ScannerFactory fac, CommentCollectingTokenizer tokenizer) { + super(fac, tokenizer); + this.tokenizer = tokenizer; + } + + public List getComments() { + return tokenizer.getComments(); + } +} \ No newline at end of file diff --git a/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java new file mode 100644 index 00000000..fa79ff67 --- /dev/null +++ b/src/utils/lombok/javac/java8/CommentCollectingScannerFactory.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2011-2013 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.javac.java8; + +import java.nio.CharBuffer; + +import com.sun.tools.javac.parser.Scanner; +import com.sun.tools.javac.parser.ScannerFactory; +import com.sun.tools.javac.util.Context; + +public class CommentCollectingScannerFactory extends ScannerFactory { + + @SuppressWarnings("all") + public static void preRegister(final Context context) { + if (context.get(scannerFactoryKey) == null) { + // Careful! There is voodoo magic here! + // + // Context.Factory is parameterized. make() is for javac6 and below; make(Context) is for javac7 and up. + // this anonymous inner class definition is intentionally 'raw' - the return type of both 'make' methods is 'T', + // which means the compiler will only generate the correct "real" override method (with returntype Object, which is + // the lower bound for T, as a synthetic accessor for the make with returntype ScannerFactory) for that make method which + // is actually on the classpath (either make() for javac6-, or make(Context) for javac7+). + // + // We normally solve this issue via src/stubs, with BOTH make methods listed, but for some reason the presence of a stubbed out + // Context (or even a complete copy, it doesn't matter) results in a really strange eclipse bug, where any mention of any kind + // of com.sun.tools.javac.tree.TreeMaker in a source file disables ALL usage of 'go to declaration' and auto-complete in the entire + // source file. + // + // Thus, in short: + // * Do NOT parameterize the anonymous inner class literal. + // * Leave the return types as 'j.l.Object'. + // * Leave both make methods intact; deleting one has no effect on javac6- / javac7+, but breaks the other. Hard to test for. + // * Do not stub com.sun.tools.javac.util.Context or any of its inner types, like Factory. + @SuppressWarnings("all") + class MyFactory implements Context.Factory { + // This overrides the javac6- version of make. + public Object make() { + return new CommentCollectingScannerFactory(context); + } + + // This overrides the javac7+ version. + public Object make(Context c) { + return new CommentCollectingScannerFactory(c); + } + } + @SuppressWarnings("unchecked") Context.Factory factory = new MyFactory(); + context.put(scannerFactoryKey, factory); + } + } + + /** Create a new scanner factory. */ + protected CommentCollectingScannerFactory(Context context) { + super(context); + } + + @Override + public Scanner newScanner(CharSequence input, boolean keepDocComments) { + if (input instanceof CharBuffer) { + CharBuffer buf = (CharBuffer) input; + return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, buf)); + } + char[] array = input.toString().toCharArray(); + return newScanner(array, array.length, keepDocComments); + } + + @Override + public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { + return new CommentCollectingScanner(this, new CommentCollectingTokenizer(this, input, inputLength)); + } +} diff --git a/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java b/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java new file mode 100644 index 00000000..95945f8f --- /dev/null +++ b/src/utils/lombok/javac/java8/CommentCollectingTokenizer.java @@ -0,0 +1,108 @@ +package lombok.javac.java8; + +import java.nio.CharBuffer; + +import lombok.javac.CommentInfo; +import lombok.javac.CommentInfo.EndConnection; +import lombok.javac.CommentInfo.StartConnection; + +import com.sun.tools.javac.parser.JavaTokenizer; +import com.sun.tools.javac.parser.ScannerFactory; +import com.sun.tools.javac.parser.Tokens.Comment; +import com.sun.tools.javac.parser.Tokens.Token; +import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; +import com.sun.tools.javac.parser.UnicodeReader; +import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; + +class CommentCollectingTokenizer extends JavaTokenizer { + private int prevEndPosition = 0; + private final ListBuffer comments = ListBuffer.lb(); + private int endComment = 0; + + CommentCollectingTokenizer(ScannerFactory fac, char[] buf, int inputLength) { + super(fac, new PositionUnicodeReader(fac, buf, inputLength)); + } + + CommentCollectingTokenizer(ScannerFactory fac, CharBuffer buf) { + super(fac, new PositionUnicodeReader(fac, buf)); + } + + @Override public Token readToken() { + Token token = super.readToken(); + prevEndPosition = ((PositionUnicodeReader)reader).pos(); + return token; + } + + @Override + protected Comment processComment(int pos, int endPos, CommentStyle style) { + int prevEndPos = Math.max(prevEndPosition, endComment); + endComment = endPos; + String content = new String(reader.getRawCharacters(pos, endPos)); + StartConnection start = determineStartConnection(prevEndPos, pos); + EndConnection end = determineEndConnection(endPos); + + CommentInfo comment = new CommentInfo(prevEndPos, pos, endPos, content, start, end); + comments.append(comment); + + return super.processComment(pos, endPos, style); + } + + private EndConnection determineEndConnection(int pos) { + boolean first = true; + for (int i = pos;; i++) { + char c; + try { + c = reader.getRawCharacters(i, i + 1)[0]; + } catch (IndexOutOfBoundsException e) { + c = '\n'; + } + if (isNewLine(c)) { + return EndConnection.ON_NEXT_LINE; + } + if (Character.isWhitespace(c)) { + first = false; + continue; + } + return first ? EndConnection.DIRECT_AFTER_COMMENT : EndConnection.AFTER_COMMENT; + } + } + + private StartConnection determineStartConnection(int from, int to) { + if (from == to) { + return StartConnection.DIRECT_AFTER_PREVIOUS; + } + char[] between = reader.getRawCharacters(from, to); + if (isNewLine(between[between.length - 1])) { + return StartConnection.START_OF_LINE; + } + for (char c : between) { + if (isNewLine(c)) { + return StartConnection.ON_NEXT_LINE; + } + } + return StartConnection.AFTER_PREVIOUS; + } + + private boolean isNewLine(char c) { + return c == '\n' || c == '\r'; + } + + public List getComments() { + return comments.toList(); + } + + static class PositionUnicodeReader extends UnicodeReader { + protected PositionUnicodeReader(ScannerFactory sf, char[] input, int inputLength) { + super(sf, input, inputLength); + } + + public PositionUnicodeReader(ScannerFactory sf, CharBuffer buffer) { + super(sf, buffer); + } + + int pos() { + return bp; + } + } +} \ No newline at end of file -- cgit From 6a02cb40137788e6f77919c762b72b3d5880d2da Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 9 Sep 2013 23:54:28 +0200 Subject: [jdk8support] We now handle doc comments internally, alleviating the need to try and keep up with the complete rewrites of the doc comment parser from java6 to java7 to java8. Still doesn't actually work in jdk8, but only because of a last-mile issue. (we communicate the doc comment via compilationUnit.docComments but that changed types in jdk8, we just need to make a wrapper to make that work). --- src/delombok/lombok/delombok/Delombok.java | 4 + .../lombok/delombok/DocCommentIntegrator.java | 97 ++++++++++++++++++++++ .../lombok/delombok/PrettyCommentsPrinter.java | 42 ++-------- src/utils/lombok/javac/CommentCatcher.java | 13 ++- src/utils/lombok/javac/Javac.java | 11 +++ .../after-delombok/GetterSetterJavadoc.java | 12 +-- .../resource/before/GetterSetterJavadoc.java | 12 +-- 7 files changed, 143 insertions(+), 48 deletions(-) create mode 100644 src/delombok/lombok/delombok/DocCommentIntegrator.java (limited to 'src/utils/lombok/javac/CommentCatcher.java') diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 1fae4560..0128e44f 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -383,6 +383,10 @@ public class Delombok { return false; } + for (JCCompilationUnit unit : roots) { + catcher.setComments(unit, new DocCommentIntegrator().integrate(catcher.getComments(unit), unit)); + } + com.sun.tools.javac.util.List trees = compiler.enterTrees(toJavacList(roots)); JavaCompiler delegate = compiler.processAnnotations(trees); diff --git a/src/delombok/lombok/delombok/DocCommentIntegrator.java b/src/delombok/lombok/delombok/DocCommentIntegrator.java new file mode 100644 index 00000000..80aec16a --- /dev/null +++ b/src/delombok/lombok/delombok/DocCommentIntegrator.java @@ -0,0 +1,97 @@ +package lombok.delombok; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import lombok.javac.CommentInfo; +import lombok.javac.Javac; + +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; + +public class DocCommentIntegrator { + /** + * Returns the same comment list as when this integrator was created, minus all doc comments that have been successfully integrated into the compilation unit. + */ + public List integrate(List comments, JCCompilationUnit unit) { + List out = new ArrayList(); + CommentInfo lastExcisedComment = null; + JCTree lastNode = null; + + for (CommentInfo cmt : comments) { + if (!cmt.isJavadoc()) { + out.add(cmt); + continue; + } + + JCTree node = findJavadocableNodeOnOrAfter(unit, cmt.endPos); + if (node == null) { + out.add(cmt); + continue; + } + + if (node == lastNode) { + out.add(lastExcisedComment); + } + if (!attach(unit, node, cmt)) { + out.add(cmt); + } else { + lastNode = node; + lastExcisedComment = cmt; + } + } + return out; + } + + private static final Pattern CONTENT_STRIPPER = Pattern.compile("^(?:\\s*\\*)?[ \\t]*(.*?)$", Pattern.MULTILINE); + @SuppressWarnings("unchecked") private boolean attach(JCCompilationUnit top, JCTree node, CommentInfo cmt) { + String docCommentContent = cmt.content; + if (docCommentContent.startsWith("/**")) docCommentContent = docCommentContent.substring(3); + if (docCommentContent.endsWith("*/")) docCommentContent = docCommentContent.substring(0, docCommentContent.length() -2); + docCommentContent = CONTENT_STRIPPER.matcher(docCommentContent).replaceAll("$1"); + docCommentContent = docCommentContent.trim(); + + if (Javac.getDocComments(top) == null) Javac.initDocComments(top); + + Object map_ = Javac.getDocComments(top); + if (map_ instanceof Map) { + ((Map) map_).put(node, docCommentContent); + return true; + } + + return false; + } + + private JCTree findJavadocableNodeOnOrAfter(JCCompilationUnit unit, int endPos) { + if (unit.pid != null && endPos <= unit.pid.pos) return null; + Iterator it = unit.defs.iterator(); + + while (it.hasNext()) { + JCTree node = it.next(); + if (node.pos < endPos) { + if (node instanceof JCClassDecl) { + com.sun.tools.javac.util.List defs = ((JCClassDecl) node).defs; + if (!defs.isEmpty()) while (!defs.tail.isEmpty()) defs = defs.tail; + if (defs.head != null && defs.head.pos >= endPos) { + // The associated node is IN this class declaration, so, replace the iterator. + // There's no point looking beyond this member in the current iteration 'context' + // so we don't need to save the old ref. Just start over inside this type declaration. + it = ((JCClassDecl) node).defs.iterator(); + } + } + continue; + } + + if (node instanceof JCMethodDecl || node instanceof JCClassDecl || node instanceof JCVariableDecl) return node; + return null; + } + + return null; + } +} diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index 9978a681..525773f4 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -29,18 +29,14 @@ */ package lombok.delombok; -import static com.sun.tools.javac.code.Flags.ANNOTATION; -import static com.sun.tools.javac.code.Flags.ENUM; -import static com.sun.tools.javac.code.Flags.INTERFACE; -import static com.sun.tools.javac.code.Flags.SYNTHETIC; -import static com.sun.tools.javac.code.Flags.StandardFlags; -import static com.sun.tools.javac.code.Flags.VARARGS; +import static com.sun.tools.javac.code.Flags.*; +import static lombok.javac.Javac.*; +import static lombok.javac.JavacTreeMaker.TreeTag.treeTag; +import static lombok.javac.JavacTreeMaker.TypeTag.typeTag; import java.io.IOException; import java.io.Writer; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -49,19 +45,11 @@ import lombok.javac.CommentInfo.EndConnection; import lombok.javac.CommentInfo.StartConnection; import lombok.javac.JavacTreeMaker.TreeTag; import lombok.javac.JavacTreeMaker.TypeTag; -import static lombok.javac.Javac.*; -import static lombok.javac.JavacTreeMaker.TreeTag.treeTag; -import static lombok.javac.JavacTreeMaker.TypeTag.typeTag; import com.sun.source.tree.Tree; -import com.sun.tools.javac.code.BoundKind; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Symbol; -//import com.sun.tools.javac.code.TypeTags; -import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.TreeInfo; -import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCArrayAccess; import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree; @@ -113,10 +101,13 @@ import com.sun.tools.javac.tree.JCTree.JCWhileLoop; import com.sun.tools.javac.tree.JCTree.JCWildcard; import com.sun.tools.javac.tree.JCTree.LetExpr; import com.sun.tools.javac.tree.JCTree.TypeBoundKind; +import com.sun.tools.javac.tree.TreeInfo; +import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.Convert; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.Position; +//import com.sun.tools.javac.code.TypeTags; /** Prints out a tree as an indented Java source program. * @@ -198,13 +189,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { boolean found = false; CommentInfo head = comments.head; while (comments.nonEmpty() && head.pos < until) { - if (tree != null && docComments != null && docComments.containsKey(tree) && head.isJavadoc() && noFurtherJavadocForthcoming(until)) { - // This is (presumably) the exact same javadoc that has already been associated with the node that we're just about to - // print. These javadoc can be modified by lombok handlers, and as such we should NOT print them from the consumed comments db, - // and instead print the actual javadoc associated with the upcoming node (which the visit method for that node will take care of). - } else { - printComment(head); - } + printComment(head); comments = comments.tail; head = comments.head; } @@ -213,17 +198,6 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { } } - private boolean noFurtherJavadocForthcoming(int until) { - List c = comments; - if (c.nonEmpty()) c = c.tail; - while (c.nonEmpty()) { - if (c.head.pos >= until) return true; - if (c.head.isJavadoc()) return false; - c = c.tail; - } - return true; - } - private void consumeTrailingComments(int from) throws IOException { boolean prevNewLine = onNewLine; CommentInfo head = comments.head; diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index 48dd7e75..36d90e30 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -22,13 +22,14 @@ package lombok.javac; import java.lang.reflect.InvocationTargetException; +import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.WeakHashMap; import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.List; public class CommentCatcher { private final JavaCompiler compiler; @@ -56,9 +57,17 @@ public class CommentCatcher { return compiler; } + public void setComments(JCCompilationUnit ast, List comments) { + if (comments != null) { + commentsMap.put(ast, comments); + } else { + commentsMap.remove(ast); + } + } + public List getComments(JCCompilationUnit ast) { List list = commentsMap.get(ast); - return list == null ? List.nil() : list; + return list == null ? Collections.emptyList() : list; } private static void registerCommentsCollectingScannerFactory(Context context) { diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java index c90b8611..2f3c9be4 100644 --- a/src/utils/lombok/javac/Javac.java +++ b/src/utils/lombok/javac/Javac.java @@ -27,6 +27,7 @@ import static lombok.javac.JavacTreeMaker.TypeTag.typeTag; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -196,6 +197,16 @@ public class Javac { } } + public static void initDocComments(JCCompilationUnit cu) { + try { + JCCOMPILATIONUNIT_DOCCOMMENTS.set(cu, new HashMap()); + } catch (IllegalArgumentException e) { + // That's fine - we're on JDK8, we'll fix that later. + } catch (IllegalAccessException e) { + throw sneakyThrow(e); + } + } + public static int getEndPosition(DiagnosticPosition pos, JCCompilationUnit top) { try { Object endPositions = JCCOMPILATIONUNIT_ENDPOSITIONS.get(top); diff --git a/test/transform/resource/after-delombok/GetterSetterJavadoc.java b/test/transform/resource/after-delombok/GetterSetterJavadoc.java index 7bf92d1f..af9cbb5e 100644 --- a/test/transform/resource/after-delombok/GetterSetterJavadoc.java +++ b/test/transform/resource/after-delombok/GetterSetterJavadoc.java @@ -9,7 +9,7 @@ class GetterSetterJavadoc1 { /** * Getter section * - * @return Sky is blue + * @return Sky is blue1 */ @java.lang.SuppressWarnings("all") public int getFieldName() { @@ -18,7 +18,7 @@ class GetterSetterJavadoc1 { /** * Some text * - * @param fieldName Hello, World + * @param fieldName Hello, World1 */ @java.lang.SuppressWarnings("all") public void setFieldName(final int fieldName) { @@ -60,7 +60,7 @@ class GetterSetterJavadoc2 { /** * Some text * - * @return Sky is blue + * @return Sky is blue2 */ @java.lang.SuppressWarnings("all") public int getFieldName() { @@ -69,7 +69,7 @@ class GetterSetterJavadoc2 { /** * Some text * - * @param fieldName Hello, World + * @param fieldName Hello, World2 */ @java.lang.SuppressWarnings("all") public void setFieldName(final int fieldName) { @@ -83,7 +83,7 @@ class GetterSetterJavadoc3 { private int fieldName; /** * Getter section - * @return Sky is blue + * @return Sky is blue3 */ @java.lang.SuppressWarnings("all") public int getFieldName() { @@ -91,7 +91,7 @@ class GetterSetterJavadoc3 { } /** * Setter section - * @param fieldName Hello, World + * @param fieldName Hello, World3 */ @java.lang.SuppressWarnings("all") public void setFieldName(final int fieldName) { diff --git a/test/transform/resource/before/GetterSetterJavadoc.java b/test/transform/resource/before/GetterSetterJavadoc.java index e3ae0aac..0dc64092 100644 --- a/test/transform/resource/before/GetterSetterJavadoc.java +++ b/test/transform/resource/before/GetterSetterJavadoc.java @@ -3,11 +3,11 @@ class GetterSetterJavadoc1 { /** * Some text * - * @param fieldName Hello, World + * @param fieldName Hello, World1 * --- GETTER --- * Getter section * - * @return Sky is blue + * @return Sky is blue1 */ private int fieldName; } @@ -16,8 +16,8 @@ class GetterSetterJavadoc2 { /** * Some text * - * @param fieldName Hello, World - * @return Sky is blue + * @param fieldName Hello, World2 + * @return Sky is blue2 */ @lombok.Getter @lombok.Setter private int fieldName; } @@ -28,10 +28,10 @@ class GetterSetterJavadoc3 { * * **SETTER** * Setter section - * @param fieldName Hello, World + * @param fieldName Hello, World3 * **GETTER** * Getter section - * @return Sky is blue + * @return Sky is blue3 */ @lombok.Getter @lombok.Setter private int fieldName; } -- cgit