diff options
author | Roel Spilker <r.spilker@gmail.com> | 2014-06-05 23:45:43 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2014-06-05 23:45:43 +0200 |
commit | 1ce747178b8f24f29f94dd795f09f872aad9272f (patch) | |
tree | 76e66d528c56f22ac5782846df4758d75a8b55bd /src/utils | |
parent | f30485c91fd3f9553fbcbc02d922e6221182c26e (diff) | |
download | lombok-1ce747178b8f24f29f94dd795f09f872aad9272f.tar.gz lombok-1ce747178b8f24f29f94dd795f09f872aad9272f.tar.bz2 lombok-1ce747178b8f24f29f94dd795f09f872aad9272f.zip |
Finished refactor of FieldAugment; there's no longer a separate variant for boolean and references, and the code no longer blows up with a bunch of NPEs if you try to use the reference variant (which is now the only variant) with a primitive type.
Should have zero effect on features or bugs, 100% refactor.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/lombok/core/BooleanFieldAugment.java | 185 | ||||
-rw-r--r-- | src/utils/lombok/core/FieldAugment.java (renamed from src/utils/lombok/core/ReferenceFieldAugment.java) | 179 | ||||
-rw-r--r-- | src/utils/lombok/javac/CommentCatcher.java | 22 | ||||
-rw-r--r-- | src/utils/lombok/javac/java6/CommentCollectingParser.java | 9 | ||||
-rw-r--r-- | src/utils/lombok/javac/java6/CommentCollectingParserFactory.java | 16 | ||||
-rw-r--r-- | src/utils/lombok/javac/java7/CommentCollectingParser.java | 9 | ||||
-rw-r--r-- | src/utils/lombok/javac/java7/CommentCollectingParserFactory.java | 15 | ||||
-rw-r--r-- | src/utils/lombok/javac/java8/CommentCollectingParser.java | 9 | ||||
-rw-r--r-- | src/utils/lombok/javac/java8/CommentCollectingParserFactory.java | 17 |
9 files changed, 146 insertions, 315 deletions
diff --git a/src/utils/lombok/core/BooleanFieldAugment.java b/src/utils/lombok/core/BooleanFieldAugment.java deleted file mode 100644 index d843e9df..00000000 --- a/src/utils/lombok/core/BooleanFieldAugment.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (C) 2014 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.core; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.Map; -import java.util.WeakHashMap; - -/** - * Augments a instance of a type with a boolean field. - * <p> - * If the type already declares a boolean field, that field is used. Otherwise the field will be augmented. - * - * @param <T> the type to augment. - */ -public abstract class BooleanFieldAugment<T> { - - /** - * Augments a instance of a type with a boolean field. - * <p> - * If the type already declares a boolean instance field, that field might be used. Otherwise the field will be augmented. - * <p> - * This code assumes that for any combination of {@code type} and {@code name} this method is only called once. - * Otherwise, whether state is shared is undefined. - * - * @param type to augment - * @param name of the field - * @throws NullPointerException if {@code type} or {@code name} is {@code null} - */ - public static <T> BooleanFieldAugment<T> augment(Class<T> type, String name) { - checkNotNull(type, "type"); - checkNotNull(name, "name"); - Field booleanField = getBooleanField(type, name); - if (booleanField == null) { - return new MapFieldAugment<T>(); - } - return new ExistingFieldAugment<T>(booleanField); - } - - private BooleanFieldAugment() { - // prevent external instantiation - } - - private static Field getBooleanField(Class<?> type, String name) { - try { - Field result = type.getDeclaredField(name); - if (Modifier.isStatic(result.getModifiers()) || result.getType() != boolean.class) { - return null; - } - result.setAccessible(true); - return result; - } catch (Throwable t) { - return null; - } - } - - /** - * Sets the field to {@code true}. - * @returns the previous value - * @throws NullPointerException if {@code object} is {@code null} - */ - public abstract boolean set(T object); - - /** - * Sets the field to {@code false}. - * @returns the previous value - * @throws NullPointerException if {@code object} is {@code null} - */ - public abstract boolean clear(T object); - - /** - * @eturn {code true} if the field is set, otherwise {@code false}. - * @throws NullPointerException if {@code object} is {@code null} - */ - public abstract boolean get(T object); - - private static class MapFieldAugment<T> extends BooleanFieldAugment<T> { - private static final Object MARKER = new Object(); - - private final Map<T, Object> values = new WeakHashMap<T,Object>(); - - public boolean set(T object) { - checkNotNull(object, "object"); - synchronized (values) { - return values.put(object, MARKER) != null; - } - } - - public boolean clear(T object) { - checkNotNull(object, "object"); - synchronized (values) { - return values.remove(object) != null; - } - } - - public boolean get(T object) { - checkNotNull(object, "object"); - synchronized (values) { - return values.get(object) != null; - } - } - } - - private static class ExistingFieldAugment<T> extends BooleanFieldAugment<T> { - private final Object lock = new Object(); - private final Field booleanField; - - private ExistingFieldAugment(Field booleanField) { - this.booleanField = booleanField; - } - - @Override public boolean set(T object) { - checkNotNull(object, "object"); - try { - synchronized (lock) { - boolean result = booleanField.getBoolean(object); - booleanField.setBoolean(object, true); - return result; - } - } catch (IllegalAccessException e) { - throw sneakyThrow(e); - } - } - - @Override public boolean clear(T object) { - checkNotNull(object, "object"); - try { - synchronized (lock) { - boolean result = booleanField.getBoolean(object); - booleanField.setBoolean(object, false); - return result; - } - } catch (IllegalAccessException e) { - throw sneakyThrow(e); - } - } - - @Override public boolean get(T object) { - checkNotNull(object, "object"); - try { - synchronized (lock) { - return booleanField.getBoolean(object); - } - } catch (IllegalAccessException e) { - throw sneakyThrow(e); - } - } - } - - private static <T> T checkNotNull(T object, String name) { - if (object == null) throw new NullPointerException(name); - return object; - } - - private static RuntimeException sneakyThrow(Throwable t) { - if (t == null) throw new NullPointerException("t"); - BooleanFieldAugment.<RuntimeException>sneakyThrow0(t); - return null; - } - - @SuppressWarnings("unchecked") - private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T { - throw (T)t; - } -}
\ No newline at end of file diff --git a/src/utils/lombok/core/ReferenceFieldAugment.java b/src/utils/lombok/core/FieldAugment.java index 9f4b23f7..ee8acf4d 100644 --- a/src/utils/lombok/core/ReferenceFieldAugment.java +++ b/src/utils/lombok/core/FieldAugment.java @@ -27,12 +27,31 @@ import java.lang.reflect.Modifier; import java.util.Map; import java.util.WeakHashMap; -public abstract class ReferenceFieldAugment<T, F> { +public abstract class FieldAugment<T, F> { + private static Object getDefaultValue(Class<?> type) { + if (type == boolean.class) return false; + if (type == int.class) return 0; + if (!type.isPrimitive()) return null; + + if (type == long.class) return 0L; + if (type == short.class) return (short) 0; + if (type == byte.class) return (byte) 0; + if (type == char.class) return '\0'; + if (type == float.class) return 0.0F; + if (type == double.class) return 0.0D; + + // We can't get here unless java added some primitive types, but, hey. + return null; + } /** - * Augments a instance of a type with a reference field. + * (Virtually) adds a field to an existing type and returns an object that can be used to read and write this field. + * <p> + * If the type already declares a non-final instance field with the given name and a compatible field type, that field will be used. + * Otherwise the field will be provided virtually. * <p> - * If the type already declares an instance field with the given name and field type, that field might be used. Otherwise the field will be augmented. + * <em>WARNING</em>: The values put into the augment should NOT reference in any way the object you've added the augment to, or memory leaks may occur. + * If you do need to add such references, use {@link #circularSafeAugment(Class, Class, String, Object)} instead. * <p> * This code assumes that for any combination of {@code type} and {@code name} this method is only called once. * Otherwise, whether state is shared is undefined. @@ -40,63 +59,59 @@ public abstract class ReferenceFieldAugment<T, F> { * @param type to augment * @param fieldType type of the field * @param name of the field + * @param defaultValue the value of the augment if it hasn't been set yet. * @throws NullPointerException if {@code type}, {@code fieldType} or {@code name} is {@code null} */ - public static <T, F> ReferenceFieldAugment<T, F> augment(Class<T> type, Class<? super F> fieldType, String name) { - ReferenceFieldAugment<T, F> ret = tryCreateReflectionAugment(type, fieldType, name); - return ret != null ? ret : new MapFieldAugment<T, F>(); + public static <T, F> FieldAugment<T, F> augment(Class<T> type, Class<? super F> fieldType, String name) { + checkNotNull(type, "type"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(name, "name"); + + @SuppressWarnings("unchecked") + F defaultValue = (F) getDefaultValue(fieldType); + FieldAugment<T, F> ret = tryCreateReflectionAugment(type, fieldType, name, defaultValue); + return ret != null ? ret : new MapFieldAugment<T, F>(defaultValue); } /** - * Augments a instance of a type with a weak reference field. - * <p> - * If the type already declares an instance field with the given name and field type, that field might be used. Otherwise the field will be augmented. + * (Virtually) adds a field to an existing type and returns an object that can be used to read and write this field. * <p> - * This code assumes that for any combination of {@code type} and {@code name} this method is only called once. - * Otherwise, whether state is shared is undefined. - * - * @param type to augment - * @param fieldType type of the field - * @param name of the field - * @throws NullPointerException if {@code type}, {@code fieldType} or {@code name} is {@code null} + * This method does the same as {@link #augment(Class, Class, String, Object)}, except it is safe to set values that reference back to their containing object. */ - public static <T, F> ReferenceFieldAugment<T, F> augmentWeakField(Class<T> type, Class<? super F> fieldType, String name) { - ReferenceFieldAugment<T, F> ret = tryCreateReflectionAugment(type, fieldType, name); - return ret != null ? ret : new MapWeakFieldAugment<T, F>(); + public static <T, F> FieldAugment<T, F> circularSafeAugment(Class<T> type, Class<? super F> fieldType, String name) { + checkNotNull(type, "type"); + checkNotNull(fieldType, "fieldType"); + checkNotNull(name, "name"); + + @SuppressWarnings("unchecked") + F defaultValue = (F) getDefaultValue(fieldType); + FieldAugment<T, F> ret = tryCreateReflectionAugment(type, fieldType, name, defaultValue); + return ret != null ? ret : new MapWeakFieldAugment<T, F>(defaultValue); } /** * Creates a reflection-based augment which will directly access the listed field name. If this field does not exist or the field * is not capable of storing the requested type, {@code null} is returned instead. */ - private static <T, F> ReferenceFieldAugment<T, F> tryCreateReflectionAugment(Class<T> type, Class<? super F> fieldType, String name) { - Field f = findField(type, name); - if (f != null && typeIsAssignmentCompatible(f.getType(), fieldType)) return new ReflectionFieldAugment<T, F>(f, fieldType); + private static <T, F> FieldAugment<T, F> tryCreateReflectionAugment(Class<T> type, Class<? super F> fieldType, String name, F defaultValue) { + Field f = findField(type, fieldType, name); + if (f != null && typeIsAssignmentCompatible(f.getType(), fieldType)) return new ReflectionFieldAugment<T, F>(f, fieldType, defaultValue); return null; } - /** - * Finds the named <em>instance</em> field in the type, or in any of its supertypes, regardless of its access modifier. It's set as accessible and returned if found. - */ - private static Field findField(Class<?> type, String name) { - while (type != null) { - try { - Field f = type.getDeclaredField(name); - if (!Modifier.isStatic(f.getModifiers())) { - f.setAccessible(true); - return f; - } - } catch (NoSuchFieldException fallthrough) {} - type = type.getSuperclass(); + private static Field findField(Class<?> type, Class<?> wantedType, String name) { + try { + Field f = type.getDeclaredField(name); + if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) return null; + if (!typeIsAssignmentCompatible(f.getType(), wantedType)) return null; + f.setAccessible(true); + return f; + } catch (Exception e) { + return null; } - - return null; } private static boolean typeIsAssignmentCompatible(Class<?> fieldType, Class<?> wantedType) { - if (Modifier.isFinal(fieldType.getModifiers())) return false; - if (Modifier.isStatic(fieldType.getModifiers())) return false; - if (fieldType == java.lang.Object.class) return true; if (fieldType == wantedType) return true; @@ -113,7 +128,7 @@ public abstract class ReferenceFieldAugment<T, F> { return fieldType.isAssignableFrom(wantedType); } - private ReferenceFieldAugment() { + private FieldAugment() { // prevent external instantiation } @@ -159,20 +174,27 @@ public abstract class ReferenceFieldAugment<T, F> { */ public abstract F compareAndSet(T object, F expected, F value); - private static class ReflectionFieldAugment<T, F> extends ReferenceFieldAugment<T, F> { + private static class ReflectionFieldAugment<T, F> extends FieldAugment<T, F> { + private final Object lock = new Object(); private final Field field; private final Class<F> targetType; + private final F defaultValue; @SuppressWarnings("unchecked") - ReflectionFieldAugment(Field field, Class<? super F> targetType) { + ReflectionFieldAugment(Field field, Class<? super F> targetType, F defaultValue) { this.field = field; this.targetType = (Class<F>) targetType; + this.defaultValue = defaultValue; } @Override public F get(T object) { checkNotNull(object, "object"); try { - return targetType.cast(field.get(object)); + F value; + synchronized (lock) { + value = targetType.cast(field.get(object)); + } + return value == null ? defaultValue : value; } catch (IllegalAccessException e) { throw new IllegalStateException(e); } @@ -182,9 +204,12 @@ public abstract class ReferenceFieldAugment<T, F> { checkNotNull(object, "object"); checkNotNull(value, "value"); try { - F oldValue = targetType.cast(field.get(object)); - field.set(object, value); - return oldValue; + F oldValue; + synchronized (lock) { + oldValue = targetType.cast(field.get(object)); + field.set(object, value); + } + return oldValue == null ? defaultValue : oldValue; } catch (IllegalAccessException e) { throw new IllegalStateException(e); } @@ -193,9 +218,12 @@ public abstract class ReferenceFieldAugment<T, F> { @Override public F clear(T object) { checkNotNull(object, "object"); try { - F oldValue = targetType.cast(field.get(object)); - field.set(object, null); - return oldValue; + F oldValue; + synchronized (lock) { + oldValue = targetType.cast(field.get(object)); + field.set(object, defaultValue); + } + return oldValue == null ? defaultValue : oldValue; } catch (IllegalAccessException e) { throw new IllegalStateException(e); } @@ -205,11 +233,15 @@ public abstract class ReferenceFieldAugment<T, F> { checkNotNull(object, "object"); checkNotNull(expected, "expected"); try { - F result = targetType.cast(field.get(object)); - if (result == null) return null; - if (!expected.equals(result)) return result; - field.set(object, null); - return null; + F oldValue; + synchronized (lock) { + oldValue = targetType.cast(field.get(object)); + if (expected.equals(oldValue)) { + field.set(object, defaultValue); + return defaultValue; + } + } + return oldValue; } catch (IllegalAccessException e) { throw new IllegalStateException(e); } @@ -219,10 +251,12 @@ public abstract class ReferenceFieldAugment<T, F> { checkNotNull(object, "object"); checkNotNull(value, "value"); try { - F result = targetType.cast(field.get(object)); - if (result != null) return result; - field.set(object, value); - return value; + synchronized (lock) { + F oldValue = targetType.cast(field.get(object)); + if (oldValue != null && !oldValue.equals(defaultValue)) return oldValue; + field.set(object, value); + return value; + } } catch (IllegalAccessException e) { throw new IllegalStateException(e); } @@ -233,18 +267,25 @@ public abstract class ReferenceFieldAugment<T, F> { checkNotNull(expected, "expected"); checkNotNull(value, "value"); try { - F result = targetType.cast(field.get(object)); - if (!expected.equals(result)) return result; - field.set(object, value); - return value; + synchronized (lock) { + F oldValue = targetType.cast(field.get(object)); + if (!expected.equals(oldValue)) return oldValue == null ? defaultValue : oldValue; + field.set(object, value); + return value; + } } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } } - private static class MapFieldAugment<T, F> extends ReferenceFieldAugment<T, F> { + private static class MapFieldAugment<T, F> extends FieldAugment<T, F> { final Map<T, Object> values = new WeakHashMap<T, Object>(); + final F defaultValue; + + MapFieldAugment(F defaultValue) { + this.defaultValue = defaultValue; + } @Override public F get(T object) { @@ -323,7 +364,8 @@ public abstract class ReferenceFieldAugment<T, F> { @SuppressWarnings("unchecked") F read(T object) { - return (F)values.get(object); + F value = (F) values.get(object); + return value == null ? defaultValue : value; } void write(T object, F value) { @@ -332,14 +374,17 @@ public abstract class ReferenceFieldAugment<T, F> { } static class MapWeakFieldAugment<T, F> extends MapFieldAugment<T, F> { + MapWeakFieldAugment(F defaultValue) { + super(defaultValue); + } @SuppressWarnings("unchecked") F read(T object) { WeakReference<F> read = (WeakReference<F>)values.get(object); - if (read == null) return null; + if (read == null) return defaultValue; F result = read.get(); if (result == null) values.remove(object); - return result; + return result == null ? defaultValue : result; } void write(T object, F value) { diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index ff6be7a2..c32da68b 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -25,7 +25,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.Collections; import java.util.List; -import lombok.core.ReferenceFieldAugment; +import lombok.core.FieldAugment; import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; @@ -33,24 +33,22 @@ import com.sun.tools.javac.util.Context; public class CommentCatcher { private final JavaCompiler compiler; - private final ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; + public static final FieldAugment<JCCompilationUnit, List<CommentInfo>> JCCompilationUnit_comments = FieldAugment.augment(JCCompilationUnit.class, List.class, "lombok$comments"); public static CommentCatcher create(Context context) { registerCommentsCollectingScannerFactory(context); JavaCompiler compiler = new JavaCompiler(context); - ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> comments = ReferenceFieldAugment.augment(JCCompilationUnit.class, List.class, "lombok$comments"); - setInCompiler(compiler, context, comments); + setInCompiler(compiler, context); compiler.keepComments = true; compiler.genEndPos = true; - return new CommentCatcher(compiler, comments); + return new CommentCatcher(compiler); } - private CommentCatcher(JavaCompiler compiler, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + private CommentCatcher(JavaCompiler compiler) { this.compiler = compiler; - this.commentsField = commentsField; } public JavaCompiler getCompiler() { @@ -59,14 +57,14 @@ public class CommentCatcher { public void setComments(JCCompilationUnit ast, List<CommentInfo> comments) { if (comments != null) { - commentsField.set(ast, comments); + JCCompilationUnit_comments.set(ast, comments); } else { - commentsField.clear(ast); + JCCompilationUnit_comments.clear(ast); } } public List<CommentInfo> getComments(JCCompilationUnit ast) { - List<CommentInfo> list = commentsField.get(ast); + List<CommentInfo> list = JCCompilationUnit_comments.get(ast); return list == null ? Collections.<CommentInfo>emptyList() : list; } @@ -89,7 +87,7 @@ public class CommentCatcher { } } - private static void setInCompiler(JavaCompiler compiler, Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + private static void setInCompiler(JavaCompiler compiler, Context context) { try { Class<?> parserFactory; int javaCompilerVersion = Javac.getJavaCompilerVersion(); @@ -100,7 +98,7 @@ public class CommentCatcher { } else { parserFactory = Class.forName("lombok.javac.java8.CommentCollectingParserFactory"); } - parserFactory.getMethod("setInCompiler", JavaCompiler.class, Context.class, ReferenceFieldAugment.class).invoke(null, compiler, context, commentsField); + parserFactory.getMethod("setInCompiler", JavaCompiler.class, Context.class).invoke(null, compiler, context); } catch (InvocationTargetException e) { throw Javac.sneakyThrow(e.getCause()); } catch (Exception e) { diff --git a/src/utils/lombok/javac/java6/CommentCollectingParser.java b/src/utils/lombok/javac/java6/CommentCollectingParser.java index 215bf5b6..d5d3c256 100644 --- a/src/utils/lombok/javac/java6/CommentCollectingParser.java +++ b/src/utils/lombok/javac/java6/CommentCollectingParser.java @@ -21,7 +21,7 @@ */ package lombok.javac.java6; -import lombok.core.ReferenceFieldAugment; +import static lombok.javac.CommentCatcher.JCCompilationUnit_comments; import lombok.javac.CommentInfo; import com.sun.tools.javac.parser.EndPosParser; @@ -31,21 +31,18 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.List; class CommentCollectingParser extends EndPosParser { - - private final ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; private final Lexer lexer; - protected CommentCollectingParser(Parser.Factory fac, Lexer S, boolean keepDocComments, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + protected CommentCollectingParser(Parser.Factory fac, Lexer S, boolean keepDocComments) { super(fac, S, keepDocComments); lexer = S; - this.commentsField = commentsField; } @Override public JCCompilationUnit compilationUnit() { JCCompilationUnit result = super.compilationUnit(); if (lexer instanceof CommentCollectingScanner) { List<CommentInfo> comments = ((CommentCollectingScanner)lexer).getComments(); - commentsField.set(result, comments); + JCCompilationUnit_comments.set(result, comments); } return result; } diff --git a/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java b/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java index 124a05f7..0cf8fdcd 100644 --- a/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java +++ b/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java @@ -23,43 +23,35 @@ package lombok.javac.java6; import java.lang.reflect.Field; -import lombok.core.ReferenceFieldAugment; -import lombok.javac.CommentInfo; - import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.parser.Lexer; import com.sun.tools.javac.parser.Parser; -import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.List; public class CommentCollectingParserFactory extends Parser.Factory { - private final ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; - static Context.Key<Parser.Factory> key() { return parserFactoryKey; } - protected CommentCollectingParserFactory(Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + protected CommentCollectingParserFactory(Context context) { super(context); - this.commentsField = commentsField; } @Override public Parser newParser(Lexer S, boolean keepDocComments, boolean genEndPos) { - Object x = new CommentCollectingParser(this, S, true, commentsField); + Object x = new CommentCollectingParser(this, S, true); return (Parser) 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 javac7's EndPosParser which implements Parser. //Either way this will work out. } - public static void setInCompiler(JavaCompiler compiler, Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + public static void setInCompiler(JavaCompiler compiler, Context context) { context.put(CommentCollectingParserFactory.key(), (Parser.Factory)null); Field field; try { field = JavaCompiler.class.getDeclaredField("parserFactory"); field.setAccessible(true); - field.set(compiler, new CommentCollectingParserFactory(context, commentsField)); + field.set(compiler, new CommentCollectingParserFactory(context)); } catch (Exception e) { throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e); } diff --git a/src/utils/lombok/javac/java7/CommentCollectingParser.java b/src/utils/lombok/javac/java7/CommentCollectingParser.java index 27d731ba..de93e5b4 100644 --- a/src/utils/lombok/javac/java7/CommentCollectingParser.java +++ b/src/utils/lombok/javac/java7/CommentCollectingParser.java @@ -21,9 +21,10 @@ */ package lombok.javac.java7; +import static lombok.javac.CommentCatcher.JCCompilationUnit_comments; + import java.util.List; -import lombok.core.ReferenceFieldAugment; import lombok.javac.CommentInfo; import com.sun.tools.javac.parser.EndPosParser; @@ -32,21 +33,19 @@ import com.sun.tools.javac.parser.ParserFactory; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; class CommentCollectingParser extends EndPosParser { - private final ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; private final Lexer lexer; protected CommentCollectingParser(ParserFactory fac, Lexer S, - boolean keepDocComments, boolean keepLineMap, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + boolean keepDocComments, boolean keepLineMap) { super(fac, S, keepDocComments, keepLineMap); lexer = S; - this.commentsField = commentsField; } public JCCompilationUnit parseCompilationUnit() { JCCompilationUnit result = super.parseCompilationUnit(); if (lexer instanceof CommentCollectingScanner) { List<CommentInfo> comments = ((CommentCollectingScanner)lexer).getComments(); - commentsField.set(result, comments); + JCCompilationUnit_comments.set(result, comments); } return result; } diff --git a/src/utils/lombok/javac/java7/CommentCollectingParserFactory.java b/src/utils/lombok/javac/java7/CommentCollectingParserFactory.java index dba5a0fa..ffd68f55 100644 --- a/src/utils/lombok/javac/java7/CommentCollectingParserFactory.java +++ b/src/utils/lombok/javac/java7/CommentCollectingParserFactory.java @@ -22,50 +22,43 @@ package lombok.javac.java7; import java.lang.reflect.Field; -import java.util.List; - -import lombok.core.ReferenceFieldAugment; -import lombok.javac.CommentInfo; import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.parser.Lexer; import com.sun.tools.javac.parser.Parser; 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 ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; private final Context context; static Context.Key<ParserFactory> key() { return parserFactoryKey; } - protected CommentCollectingParserFactory(Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + protected CommentCollectingParserFactory(Context context) { super(context); this.context = context; - this.commentsField = commentsField; } public Parser 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, commentsField); + Object x = new CommentCollectingParser(this, lexer, true, keepLineMap); return (Parser) 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 javac7's EndPosParser which implements Parser. //Either way this will work out. } - public static void setInCompiler(JavaCompiler compiler, Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + public static void setInCompiler(JavaCompiler compiler, Context context) { context.put(CommentCollectingParserFactory.key(), (ParserFactory)null); Field field; try { field = JavaCompiler.class.getDeclaredField("parserFactory"); field.setAccessible(true); - field.set(compiler, new CommentCollectingParserFactory(context, commentsField)); + field.set(compiler, new CommentCollectingParserFactory(context)); } catch (Exception e) { throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e); } diff --git a/src/utils/lombok/javac/java8/CommentCollectingParser.java b/src/utils/lombok/javac/java8/CommentCollectingParser.java index 9a05267c..b49312cb 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingParser.java +++ b/src/utils/lombok/javac/java8/CommentCollectingParser.java @@ -21,9 +21,10 @@ */ package lombok.javac.java8; +import static lombok.javac.CommentCatcher.JCCompilationUnit_comments; + import java.util.List; -import lombok.core.ReferenceFieldAugment; import lombok.javac.CommentInfo; import com.sun.tools.javac.parser.JavacParser; @@ -32,21 +33,19 @@ import com.sun.tools.javac.parser.ParserFactory; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; class CommentCollectingParser extends JavacParser { - private final ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; private final Lexer lexer; protected CommentCollectingParser(ParserFactory fac, Lexer S, - boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions) { super(fac, S, keepDocComments, keepLineMap, keepEndPositions); lexer = S; - this.commentsField = commentsField; } public JCCompilationUnit parseCompilationUnit() { JCCompilationUnit result = super.parseCompilationUnit(); if (lexer instanceof CommentCollectingScanner) { List<CommentInfo> comments = ((CommentCollectingScanner)lexer).getComments(); - commentsField.set(result, comments); + JCCompilationUnit_comments.set(result, comments); } return result; } diff --git a/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java b/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java index 5bed46fc..45f865ad 100644 --- a/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java +++ b/src/utils/lombok/javac/java8/CommentCollectingParserFactory.java @@ -22,50 +22,43 @@ package lombok.javac.java8; import java.lang.reflect.Field; -import java.util.List; - -import lombok.core.ReferenceFieldAugment; -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 ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField; private final Context context; static Context.Key<ParserFactory> key() { return parserFactoryKey; } - protected CommentCollectingParserFactory(Context context, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { + protected CommentCollectingParserFactory(Context context) { super(context); this.context = context; - this.commentsField = commentsField; } 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, commentsField); + Object x = new CommentCollectingParser(this, lexer, true, keepLineMap, keepEndPos); 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, ReferenceFieldAugment<JCCompilationUnit, List<CommentInfo>> commentsField) { - context.put(CommentCollectingParserFactory.key(), (ParserFactory)null); + public static void setInCompiler(JavaCompiler compiler, Context context) { + context.put(CommentCollectingParserFactory.key(), (ParserFactory) null); Field field; try { field = JavaCompiler.class.getDeclaredField("parserFactory"); field.setAccessible(true); - field.set(compiler, new CommentCollectingParserFactory(context, commentsField)); + field.set(compiler, new CommentCollectingParserFactory(context)); } catch (Exception e) { throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e); } |