diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-05-23 22:58:34 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2013-05-23 22:58:34 +0200 |
commit | 9c1e29842e65bf20895db9e19336b2ca948236ad (patch) | |
tree | 396adccb0d61e049526622956c5ff59b5a32710c | |
parent | 4152eee126bcb6a0403d3e7a79fe336944031268 (diff) | |
download | lombok-9c1e29842e65bf20895db9e19336b2ca948236ad.tar.gz lombok-9c1e29842e65bf20895db9e19336b2ca948236ad.tar.bz2 lombok-9c1e29842e65bf20895db9e19336b2ca948236ad.zip |
Added methods to obtain JLS support-level version information from AST/LombokNode. Tests updates to honour these with //version X at the top
of any test file (now also in eclipse, which until now always said it was v6)
-rw-r--r-- | src/core/lombok/core/AST.java | 10 | ||||
-rw-r--r-- | src/core/lombok/core/LombokNode.java | 9 | ||||
-rw-r--r-- | src/core/lombok/eclipse/EclipseAST.java | 14 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacAST.java | 10 | ||||
-rw-r--r-- | src/utils/lombok/eclipse/Eclipse.java | 19 | ||||
-rw-r--r-- | test/core/src/lombok/DirectoryRunner.java | 3 |
6 files changed, 64 insertions, 1 deletions
diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java index a2279efe..30297418 100644 --- a/src/core/lombok/core/AST.java +++ b/src/core/lombok/core/AST.java @@ -147,8 +147,18 @@ public abstract class AST<A extends AST<A, L, N>, L extends LombokNode<A, L, N>, } /** + * Returns the JLS spec version that the compiler uses to parse and compile this AST. + * For example, if -source 1.6 is on the command line, this will return {@code 6}. + */ + public int getSourceVersion() { + return 6; + } + + /** * Returns the latest version of the java language specification supported by the host compiler. * For example, if compiling with javac v1.7, this returns {@code 7}. + * + * NB: Even if -source (lower than maximum) is specified, this method still returns the maximum supported number. */ public int getLatestJavaSpecSupported() { return 6; diff --git a/src/core/lombok/core/LombokNode.java b/src/core/lombok/core/LombokNode.java index aa161a8d..30bacc56 100644 --- a/src/core/lombok/core/LombokNode.java +++ b/src/core/lombok/core/LombokNode.java @@ -190,6 +190,15 @@ public abstract class LombokNode<A extends AST<A, L, N>, L extends LombokNode<A, } /** + * Convenient shortcut to the owning ast object's getSourceVersion method. + * + * @see AST#getSourceVersion() + */ + public int getSourceVersion() { + return ast.getSourceVersion(); + } + + /** * Convenient shortcut to the owning ast object's top method. * * @see AST#top() diff --git a/src/core/lombok/eclipse/EclipseAST.java b/src/core/lombok/eclipse/EclipseAST.java index d1f4ff06..612dcff7 100644 --- a/src/core/lombok/eclipse/EclipseAST.java +++ b/src/core/lombok/eclipse/EclipseAST.java @@ -68,6 +68,20 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> { return pkg == null ? null : Eclipse.toQualifiedName(pkg.getImportName()); } + @Override public int getSourceVersion() { + long sl = compilationUnitDeclaration.problemReporter.options.sourceLevel; + long cl = compilationUnitDeclaration.problemReporter.options.complianceLevel; + sl >>= 16; + cl >>= 16; + if (sl == 0) sl = cl; + if (cl == 0) cl = sl; + return Math.min((int)(sl - 44), (int)(cl - 44)); + } + + @Override public int getLatestJavaSpecSupported() { + return Eclipse.getEcjCompilerVersion(); + } + /** * Runs through the entire AST, starting at the compilation unit, calling the provided visitor's visit methods * for each node, depth first. diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 9e15516e..8197dae6 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -34,6 +34,7 @@ import javax.tools.JavaFileObject; import lombok.core.AST; import com.sun.tools.javac.code.Symtab; +import com.sun.tools.javac.code.Source; import com.sun.tools.javac.model.JavacElements; import com.sun.tools.javac.model.JavacTypes; import com.sun.tools.javac.tree.JCTree; @@ -113,6 +114,15 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { for (JavacNode child : node.down()) child.traverse(visitor); } + @Override public int getSourceVersion() { + try { + String nm = Source.instance(context).name(); + int underscoreIdx = nm.indexOf('_'); + if (underscoreIdx > -1) return Integer.parseInt(nm.substring(underscoreIdx + 1)); + } catch (Exception ignore) {} + return 6; + } + @Override public int getLatestJavaSpecSupported() { return Javac.getJavaCompilerVersion(); } diff --git a/src/utils/lombok/eclipse/Eclipse.java b/src/utils/lombok/eclipse/Eclipse.java index 150f3a96..edfe1536 100644 --- a/src/utils/lombok/eclipse/Eclipse.java +++ b/src/utils/lombok/eclipse/Eclipse.java @@ -21,6 +21,7 @@ */ package lombok.eclipse; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -38,6 +39,7 @@ import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; public class Eclipse { @@ -177,4 +179,21 @@ public class Eclipse { return null; } + + private static int ecjCompilerVersionCached = -1; + + public static int getEcjCompilerVersion() { + if (ecjCompilerVersionCached >= 0) return ecjCompilerVersionCached; + + for (Field f : CompilerOptions.class.getDeclaredFields()) { + try { + if (f.getName().startsWith("VERSION_1_")) { + ecjCompilerVersionCached = Math.max(ecjCompilerVersionCached, Integer.parseInt(f.getName().substring("VERSION_1_".length()))); + } + } catch (Exception ignore) {} + } + + if (ecjCompilerVersionCached < 5) ecjCompilerVersionCached = 5; + return ecjCompilerVersionCached; + } } diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 855db023..5325c1e3 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -32,6 +32,7 @@ import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.eclipse.Eclipse; import lombok.javac.Javac; import org.junit.runner.Description; @@ -53,7 +54,7 @@ public class DirectoryRunner extends Runner { }, ECJ { @Override public int getVersion() { - return 6; + return Eclipse.getEcjCompilerVersion(); } }; |