From 0ecc23766f18418f28d09291455777d59537ccc3 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 18 Mar 2013 23:56:57 +0100 Subject: Added a //version option to test files to restrict them to specific versions. --- test/core/src/lombok/DirectoryRunner.java | 74 ++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'test/core/src/lombok/DirectoryRunner.java') diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 191a7b63..a3b4de00 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -29,15 +29,43 @@ import java.io.FileReader; import java.io.IOException; import java.util.Map; import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; +import com.sun.tools.javac.main.JavaCompiler; + public class DirectoryRunner extends Runner { public enum Compiler { - DELOMBOK, JAVAC, ECJ; + DELOMBOK { + @Override public int getVersion() { + Matcher m = VERSION_PARSER.matcher(JavaCompiler.version()); + if (m.matches()) { + int major = Integer.parseInt(m.group(1)); + int minor = Integer.parseInt(m.group(2)); + if (major == 1) return minor; + } + + return 6; + } + }, + JAVAC { + @Override public int getVersion() { + return DELOMBOK.getVersion(); + } + }, + ECJ { + @Override public int getVersion() { + return 6; + } + }; + + private static final Pattern VERSION_PARSER = Pattern.compile("^(\\d+)\\.(\\d+).*$"); + public abstract int getVersion(); } public static abstract class TestParams { @@ -46,10 +74,52 @@ public class DirectoryRunner extends Runner { public abstract File getBeforeDirectory(); public abstract File getAfterDirectory(); public abstract File getMessagesDirectory(); + /** Version of the JDK dialect that the compiler can understand; for example, if you return '7', you should know what try-with-resources is. */ + public int getVersion() { + return getCompiler().getVersion(); + } public boolean accept(File file) { return true; } + + private static final Pattern P1 = Pattern.compile("^(\\d+)$"); + private static final Pattern P2 = Pattern.compile("^\\:(\\d+)$"); + private static final Pattern P3 = Pattern.compile("^(\\d+):$"); + private static final Pattern P4 = Pattern.compile("^(\\d+):(\\d+)$"); + + public boolean shouldIgnoreBasedOnVersion(String firstLine) { + int thisVersion = getVersion(); + if (!firstLine.startsWith("//version ")) return false; + + String spec = firstLine.substring("//version ".length()); + + /* Single version: '5' */ { + Matcher m = P1.matcher(spec); + if (m.matches()) return Integer.parseInt(m.group(1)) != thisVersion; + } + + /* Upper bound: ':5' (inclusive) */ { + Matcher m = P2.matcher(spec); + if (m.matches()) return Integer.parseInt(m.group(1)) < thisVersion; + } + + /* Lower bound '5:' (inclusive) */ { + Matcher m = P3.matcher(spec); + if (m.matches()) return Integer.parseInt(m.group(1)) > thisVersion; + } + + /* Range '7:8' (inclusive) */ { + Matcher m = P4.matcher(spec); + if (m.matches()) { + if (Integer.parseInt(m.group(1)) < thisVersion) return true; + if (Integer.parseInt(m.group(2)) > thisVersion) return true; + return false; + } + } + + throw new IllegalArgumentException("Version validity spec not valid: " + spec); + } } private static final FileFilter JAVA_FILE_FILTER = new FileFilter() { @@ -135,6 +205,6 @@ public class DirectoryRunner extends Runner { BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); reader.close(); - return line != null && line.startsWith("//ignore"); + return line != null && (line.startsWith("//ignore") || params.shouldIgnoreBasedOnVersion(line)); } } -- cgit From 4152eee126bcb6a0403d3e7a79fe336944031268 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 24 Mar 2013 19:14:10 +0100 Subject: Added a method to obtain latest java language spec supported by host platform and implemented it for javac BUT NOT FOR ECJ! --- src/core/lombok/core/AST.java | 8 ++++++++ src/core/lombok/core/LombokNode.java | 9 +++++++++ src/core/lombok/javac/JavacAST.java | 4 ++++ src/utils/lombok/javac/CommentCatcher.java | 4 ++-- src/utils/lombok/javac/Javac.java | 20 +++++++++++++++++++- test/core/src/lombok/DirectoryRunner.java | 14 +++----------- 6 files changed, 45 insertions(+), 14 deletions(-) (limited to 'test/core/src/lombok/DirectoryRunner.java') diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java index 644d9449..a2279efe 100644 --- a/src/core/lombok/core/AST.java +++ b/src/core/lombok/core/AST.java @@ -146,6 +146,14 @@ public abstract class AST, L extends LombokNode, return nodeMap.get(node); } + /** + * 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}. + */ + public int getLatestJavaSpecSupported() { + return 6; + } + @SuppressWarnings({"unchecked", "rawtypes"}) L replaceNewWithExistingOld(Map oldNodes, L newNode) { L oldNode = oldNodes.get(newNode.get()); diff --git a/src/core/lombok/core/LombokNode.java b/src/core/lombok/core/LombokNode.java index 588adc55..aa161a8d 100644 --- a/src/core/lombok/core/LombokNode.java +++ b/src/core/lombok/core/LombokNode.java @@ -180,6 +180,15 @@ public abstract class LombokNode, L extends LombokNode { for (JavacNode child : node.down()) child.traverse(visitor); } + @Override public int getLatestJavaSpecSupported() { + return Javac.getJavaCompilerVersion(); + } + /** @return A Name object generated for the proper name table belonging to this AST. */ public Name toName(String name) { return elements.getName(name); diff --git a/src/utils/lombok/javac/CommentCatcher.java b/src/utils/lombok/javac/CommentCatcher.java index 474dc43d..2825cd30 100644 --- a/src/utils/lombok/javac/CommentCatcher.java +++ b/src/utils/lombok/javac/CommentCatcher.java @@ -62,7 +62,7 @@ public class CommentCatcher { private static void registerCommentsCollectingScannerFactory(Context context) { try { - if (JavaCompiler.version().startsWith("1.6")) { + if (Javac.getJavaCompilerVersion() <= 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); @@ -76,7 +76,7 @@ public class CommentCatcher { private static void setInCompiler(JavaCompiler compiler, Context context, Map> commentsMap) { try { - if (JavaCompiler.version().startsWith("1.6")) { + if (Javac.getJavaCompilerVersion() <= 6) { Class parserFactory = Class.forName("lombok.javac.java6.CommentCollectingParserFactory"); parserFactory.getMethod("setInCompiler",JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); } else { diff --git a/src/utils/lombok/javac/Javac.java b/src/utils/lombok/javac/Javac.java index 75bb2dbf..b4e58b8f 100644 --- a/src/utils/lombok/javac/Javac.java +++ b/src/utils/lombok/javac/Javac.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2012 The Project Lombok Authors. + * Copyright (C) 2009-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 @@ -21,9 +21,11 @@ */ package lombok.javac; +import java.util.regex.Matcher; import java.util.regex.Pattern; 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.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; @@ -42,6 +44,22 @@ public class Javac { private static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile( "^(boolean|byte|short|int|long|float|double|char)$"); + private static final Pattern VERSION_PARSER = Pattern.compile("^(\\d{1,6})\\.(\\d{1,6}).*$"); + + /** + * Returns the version of this java compiler, i.e. the JDK that it shipped in. For example, for javac v1.7, this returns {@code 7}. + */ + public static int getJavaCompilerVersion() { + Matcher m = VERSION_PARSER.matcher(JavaCompiler.version()); + if (m.matches()) { + int major = Integer.parseInt(m.group(1)); + int minor = Integer.parseInt(m.group(2)); + if (major == 1) return minor; + } + + return 6; + } + /** * Checks if the given expression (that really ought to refer to a type expression) represents a primitive type. */ diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index a3b4de00..855db023 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -32,25 +32,18 @@ import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.javac.Javac; + import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; -import com.sun.tools.javac.main.JavaCompiler; - public class DirectoryRunner extends Runner { public enum Compiler { DELOMBOK { @Override public int getVersion() { - Matcher m = VERSION_PARSER.matcher(JavaCompiler.version()); - if (m.matches()) { - int major = Integer.parseInt(m.group(1)); - int minor = Integer.parseInt(m.group(2)); - if (major == 1) return minor; - } - - return 6; + return Javac.getJavaCompilerVersion(); } }, JAVAC { @@ -64,7 +57,6 @@ public class DirectoryRunner extends Runner { } }; - private static final Pattern VERSION_PARSER = Pattern.compile("^(\\d+)\\.(\\d+).*$"); public abstract int getVersion(); } -- cgit From 9c1e29842e65bf20895db9e19336b2ca948236ad Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 23 May 2013 22:58:34 +0200 Subject: 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) --- src/core/lombok/core/AST.java | 10 ++++++++++ src/core/lombok/core/LombokNode.java | 9 +++++++++ src/core/lombok/eclipse/EclipseAST.java | 14 ++++++++++++++ src/core/lombok/javac/JavacAST.java | 10 ++++++++++ src/utils/lombok/eclipse/Eclipse.java | 19 +++++++++++++++++++ test/core/src/lombok/DirectoryRunner.java | 3 ++- 6 files changed, 64 insertions(+), 1 deletion(-) (limited to 'test/core/src/lombok/DirectoryRunner.java') 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 @@ -146,9 +146,19 @@ public abstract class AST, L extends LombokNode, return nodeMap.get(node); } + /** + * 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 @@ -189,6 +189,15 @@ public abstract class LombokNode, L extends LombokNode { 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 { 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(); } }; -- cgit