aboutsummaryrefslogtreecommitdiff
path: root/src/utils/lombok/javac
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/lombok/javac')
-rw-r--r--src/utils/lombok/javac/Comment.java58
-rw-r--r--src/utils/lombok/javac/Comments.java52
-rw-r--r--src/utils/lombok/javac/Javac.java139
-rw-r--r--src/utils/lombok/javac/TreeMirrorMaker.java86
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingScanner.java95
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java67
-rw-r--r--src/utils/lombok/javac/java7/CommentCollectingScanner.java95
-rw-r--r--src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java68
8 files changed, 660 insertions, 0 deletions
diff --git a/src/utils/lombok/javac/Comment.java b/src/utils/lombok/javac/Comment.java
new file mode 100644
index 00000000..f66e1f50
--- /dev/null
+++ b/src/utils/lombok/javac/Comment.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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;
+
+public final class Comment {
+ public enum StartConnection {
+ START_OF_LINE,
+ ON_NEXT_LINE,
+ DIRECT_AFTER_PREVIOUS,
+ AFTER_PREVIOUS
+ }
+
+ public enum EndConnection {
+ DIRECT_AFTER_COMMENT,
+ AFTER_COMMENT,
+ ON_NEXT_LINE
+ }
+
+ public final int pos;
+ public final int prevEndPos;
+ public final String content;
+ public final int endPos;
+ public final StartConnection start;
+ public final EndConnection end;
+
+ public Comment(int prevEndPos, int pos, int endPos, String content, StartConnection start, EndConnection end) {
+ this.pos = pos;
+ this.prevEndPos = prevEndPos;
+ this.endPos = endPos;
+ this.content = content;
+ this.start = start;
+ this.end = end;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%d: %s (%s,%s)", pos, content, start, end);
+ }
+}
diff --git a/src/utils/lombok/javac/Comments.java b/src/utils/lombok/javac/Comments.java
new file mode 100644
index 00000000..2e8ceb26
--- /dev/null
+++ b/src/utils/lombok/javac/Comments.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright © 2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
+ *
+ * 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;
+
+import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.util.Context;
+
+public class Comments {
+ public void register(Context context) {
+ try {
+ if (JavaCompiler.version().startsWith("1.6")) {
+ Class.forName("lombok.javac.java6.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context);
+ } else {
+ Class.forName("lombok.javac.java7.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context);
+ }
+ } catch (Exception e) {
+ if (e instanceof RuntimeException) throw (RuntimeException)e;
+ throw new RuntimeException(e);
+ }
+ context.put(Comments.class, (Comments) null);
+ context.put(Comments.class, this);
+ }
+
+ private com.sun.tools.javac.util.ListBuffer<Comment> comments = com.sun.tools.javac.util.ListBuffer.lb();
+
+ public com.sun.tools.javac.util.ListBuffer<Comment> getComments() {
+ return comments;
+ }
+
+ public void add(Comment comment) {
+ comments.append(comment);
+ }
+}
diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java
new file mode 100644
index 00000000..cd24b061
--- /dev/null
+++ b/src/utils/lombok/javac/Javac.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright © 2009-2011 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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;
+
+import lombok.core.TransformationsUtil;
+
+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.JCVariableDecl;
+
+/**
+ * Container for static utility methods relevant to lombok's operation on javac.
+ */
+public class Javac {
+ private Javac() {
+ //prevent instantiation
+ }
+
+ /**
+ * Translates the given field into all possible getter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllGetterNames(CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllGetterNames(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ boolean isBoolean = field.vartype.toString().equals("boolean");
+
+ return TransformationsUtil.toAllGetterNames(fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely getter name for the stated field. (e.g. private boolean foo; to isFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toGetterName(CharSequence, boolean)}.
+ */
+ public static String toGetterName(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ boolean isBoolean = field.vartype.toString().equals("boolean");
+
+ return TransformationsUtil.toGetterName(fieldName, isBoolean);
+ }
+
+ /**
+ * Translates the given field into all possible setter names.
+ * Convenient wrapper around {@link TransformationsUtil#toAllSetterNames(CharSequence, boolean)}.
+ */
+ public static java.util.List<String> toAllSetterNames(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ boolean isBoolean = field.vartype.toString().equals("boolean");
+
+ return TransformationsUtil.toAllSetterNames(fieldName, isBoolean);
+ }
+
+ /**
+ * @return the likely setter name for the stated field. (e.g. private boolean foo; to setFoo).
+ *
+ * Convenient wrapper around {@link TransformationsUtil#toSetterName(CharSequence, boolean)}.
+ */
+ public static String toSetterName(JCVariableDecl field) {
+ CharSequence fieldName = field.name;
+
+ boolean isBoolean = field.vartype.toString().equals("boolean");
+
+ return TransformationsUtil.toSetterName(fieldName, isBoolean);
+ }
+
+ /**
+ * Checks if the given expression (that really ought to refer to a type expression) represents a primitive type.
+ */
+ public static boolean isPrimitive(JCExpression ref) {
+ String typeName = ref.toString();
+ return TransformationsUtil.PRIMITIVE_TYPE_NAME_PATTERN.matcher(typeName).matches();
+ }
+
+ /**
+ * Turns an expression into a guessed intended literal. Only works for literals, as you can imagine.
+ *
+ * Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'.
+ */
+ public static Object calculateGuess(JCExpression expr) {
+ if (expr instanceof JCLiteral) {
+ JCLiteral lit = (JCLiteral)expr;
+ if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) {
+ return ((Number)lit.value).intValue() == 0 ? false : true;
+ }
+ return lit.value;
+ } else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) {
+ String x = expr.toString();
+ if (x.endsWith(".class")) x = x.substring(0, x.length() - 6);
+ else {
+ int idx = x.lastIndexOf('.');
+ if (idx > -1) x = x.substring(idx + 1);
+ }
+ return x;
+ } else return null;
+ }
+
+ /**
+ * Retrieves a compile time constant of type int from the specified class location.
+ *
+ * Solves the problem of compile time constant inlining, resulting in lombok having the wrong value
+ * (javac compiler changes private api constants from time to time)
+ *
+ * @param ctcLocation location of the compile time constant
+ * @param identifier the name of the field of the compile time constant.
+ */
+ public static int getCtcInt(Class<?> ctcLocation, String identifier) {
+ try {
+ return (Integer)ctcLocation.getField(identifier).get(null);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/utils/lombok/javac/TreeMirrorMaker.java b/src/utils/lombok/javac/TreeMirrorMaker.java
new file mode 100644
index 00000000..4fe59ff1
--- /dev/null
+++ b/src/utils/lombok/javac/TreeMirrorMaker.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright © 2010-2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
+ *
+ * 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;
+
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import com.sun.source.tree.VariableTree;
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
+import com.sun.tools.javac.tree.TreeCopier;
+import com.sun.tools.javac.tree.TreeMaker;
+import com.sun.tools.javac.util.List;
+
+public class TreeMirrorMaker extends TreeCopier<Void> {
+ private final IdentityHashMap<JCTree, JCTree> originalToCopy = new IdentityHashMap<JCTree, JCTree>();
+
+ public TreeMirrorMaker(TreeMaker maker) {
+ super(maker);
+ }
+
+ @Override public <T extends JCTree> T copy(T original) {
+ T copy = super.copy(original);
+ originalToCopy.put(original, copy);
+ return copy;
+ }
+
+ @Override public <T extends JCTree> T copy(T original, Void p) {
+ T copy = super.copy(original, p);
+ originalToCopy.put(original, copy);
+ return copy;
+ }
+
+ @Override public <T extends JCTree> List<T> copy(List<T> originals) {
+ List<T> copies = super.copy(originals);
+ if (originals != null) {
+ Iterator<T> it1 = originals.iterator();
+ Iterator<T> it2 = copies.iterator();
+ while (it1.hasNext()) originalToCopy.put(it1.next(), it2.next());
+ }
+ return copies;
+ }
+
+ @Override public <T extends JCTree> List<T> copy(List<T> originals, Void p) {
+ List<T> copies = super.copy(originals, p);
+ if (originals != null) {
+ Iterator<T> it1 = originals.iterator();
+ Iterator<T> it2 = copies.iterator();
+ while (it1.hasNext()) originalToCopy.put(it1.next(), it2.next());
+ }
+ return copies;
+ }
+
+ public Map<JCTree, JCTree> getOriginalToCopyMap() {
+ return Collections.unmodifiableMap(originalToCopy);
+ }
+
+ // Fix for NPE in HandleVal. See http://code.google.com/p/projectlombok/issues/detail?id=205
+ // Maybe this should be done elsewhere...
+ @Override public JCTree visitVariable(VariableTree node, Void p) {
+ JCVariableDecl copy = (JCVariableDecl) super.visitVariable(node, p);
+ copy.sym = ((JCVariableDecl) node).sym;
+ return copy;
+ }
+}
diff --git a/src/utils/lombok/javac/java6/CommentCollectingScanner.java b/src/utils/lombok/javac/java6/CommentCollectingScanner.java
new file mode 100644
index 00000000..363e04ba
--- /dev/null
+++ b/src/utils/lombok/javac/java6/CommentCollectingScanner.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2011 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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.java6;
+
+import java.nio.CharBuffer;
+
+import lombok.javac.Comment;
+import lombok.javac.Comments;
+import lombok.javac.Comment.EndConnection;
+import lombok.javac.Comment.StartConnection;
+
+import com.sun.tools.javac.parser.Scanner;
+
+public class CommentCollectingScanner extends Scanner {
+ private final Comments comments;
+ private int endComment = 0;
+
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer, Comments comments) {
+ super(factory, charBuffer);
+ this.comments = comments;
+ }
+
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength, Comments comments) {
+ super(factory, input, inputLength);
+ this.comments = comments;
+ }
+
+ @Override
+ protected void processComment(CommentStyle style) {
+ int prevEndPos = Math.max(prevEndPos(), endComment);
+ int pos = pos();
+ int endPos = endPos();
+ endComment = endPos;
+ String content = new String(getRawCharacters(pos, endPos));
+ StartConnection start = determineStartConnection(prevEndPos, pos);
+ EndConnection end = determineEndConnection(endPos);
+
+ Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
+ comments.add(comment);
+ }
+
+ private EndConnection determineEndConnection(int pos) {
+ boolean first = true;
+ for (int i = pos;; i++) {
+ char c = getRawCharacters(i, i + 1)[0];
+ 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 = 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';
+ }
+}
diff --git a/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java
new file mode 100644
index 00000000..c259a4cf
--- /dev/null
+++ b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright © 2011 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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.java6;
+
+import java.nio.CharBuffer;
+
+import lombok.javac.Comments;
+
+import com.sun.tools.javac.parser.Scanner;
+import com.sun.tools.javac.util.Context;
+
+public class CommentCollectingScannerFactory extends Scanner.Factory {
+ private final Context context;
+
+ public static void preRegister(final Context context) {
+ if (context.get(scannerFactoryKey) == null) {
+ context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() {
+ public CommentCollectingScanner.Factory make() {
+ return new CommentCollectingScannerFactory(context);
+ }
+
+ public CommentCollectingScanner.Factory make(Context c) {
+ return new CommentCollectingScannerFactory(c);
+ }
+ });
+ }
+ }
+
+ /** Create a new scanner factory. */
+ protected CommentCollectingScannerFactory(Context context) {
+ super(context);
+ this.context = context;
+ }
+
+ @Override
+ public Scanner newScanner(CharSequence input) {
+ if (input instanceof CharBuffer) {
+ return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
+ }
+ char[] array = input.toString().toCharArray();
+ return newScanner(array, array.length);
+ }
+
+ @Override
+ public Scanner newScanner(char[] input, int inputLength) {
+ return new CommentCollectingScanner(this, input, inputLength, context.get(Comments.class));
+ }
+} \ No newline at end of file
diff --git a/src/utils/lombok/javac/java7/CommentCollectingScanner.java b/src/utils/lombok/javac/java7/CommentCollectingScanner.java
new file mode 100644
index 00000000..2c588175
--- /dev/null
+++ b/src/utils/lombok/javac/java7/CommentCollectingScanner.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2011 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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.java7;
+
+import java.nio.CharBuffer;
+
+import lombok.javac.Comment;
+import lombok.javac.Comment.EndConnection;
+import lombok.javac.Comment.StartConnection;
+import lombok.javac.Comments;
+
+import com.sun.tools.javac.parser.Scanner;
+
+public class CommentCollectingScanner extends Scanner {
+ private final Comments comments;
+ private int endComment = 0;
+
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer, Comments comments) {
+ super(factory, charBuffer);
+ this.comments = comments;
+ }
+
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength, Comments comments) {
+ super(factory, input, inputLength);
+ this.comments = comments;
+ }
+
+ @Override
+ protected void processComment(CommentStyle style) {
+ int prevEndPos = Math.max(prevEndPos(), endComment);
+ int pos = pos();
+ int endPos = endPos();
+ endComment = endPos;
+ String content = new String(getRawCharacters(pos, endPos));
+ StartConnection start = determineStartConnection(prevEndPos, pos);
+ EndConnection end = determineEndConnection(endPos);
+
+ Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
+ comments.add(comment);
+ }
+
+ private EndConnection determineEndConnection(int pos) {
+ boolean first = true;
+ for (int i = pos;; i++) {
+ char c = getRawCharacters(i, i + 1)[0];
+ 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 = 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';
+ }
+}
diff --git a/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java
new file mode 100644
index 00000000..c6cf4011
--- /dev/null
+++ b/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2011 Reinier Zwitserloot and Roel Spilker.
+ *
+ * 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.java7;
+
+import java.nio.CharBuffer;
+
+import lombok.javac.Comments;
+
+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 {
+ private final Context context;
+
+ public static void preRegister(final Context context) {
+ if (context.get(scannerFactoryKey) == null) {
+ context.put(scannerFactoryKey, new Context.Factory<ScannerFactory>() {
+ public ScannerFactory make() {
+ return new CommentCollectingScannerFactory(context);
+ }
+
+ @Override public ScannerFactory make(Context c) {
+ return new CommentCollectingScannerFactory(c);
+ }
+ });
+ }
+ }
+
+ /** Create a new scanner factory. */
+ protected CommentCollectingScannerFactory(Context context) {
+ super(context);
+ this.context = context;
+ }
+
+ @Override
+ public Scanner newScanner(CharSequence input, boolean keepDocComments) {
+ if (input instanceof CharBuffer) {
+ return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
+ }
+ 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, input, inputLength, context.get(Comments.class));
+ }
+}