diff options
author | Roel Spilker <r.spilker@gmail.com> | 2015-12-06 01:11:30 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2015-12-06 01:11:30 +0100 |
commit | 8037350c70a1172f88cd1f050336b326adabb64d (patch) | |
tree | 8918fb7aeaac4eb207a1b56534d361a83b2a7d63 /src/core/lombok/javac | |
parent | 50716864f684261313bd385dc20c86b7141c47d6 (diff) | |
download | lombok-8037350c70a1172f88cd1f050336b326adabb64d.tar.gz lombok-8037350c70a1172f88cd1f050336b326adabb64d.tar.bz2 lombok-8037350c70a1172f88cd1f050336b326adabb64d.zip |
[issue #942] let `@Data` work on jdk9, first steps.
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r-- | src/core/lombok/javac/JavacAST.java | 36 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacImportList.java | 27 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 34 |
3 files changed, 74 insertions, 23 deletions
diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 4e553063..da61361d 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2015 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 @@ -22,6 +22,7 @@ package lombok.javac; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -34,16 +35,14 @@ 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.code.Symtab; import com.sun.tools.javac.model.JavacElements; import com.sun.tools.javac.model.JavacTypes; import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.JCTree.JCCatch; -import com.sun.tools.javac.tree.JCTree.JCTry; -import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; +import com.sun.tools.javac.tree.JCTree.JCCatch; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; @@ -51,11 +50,13 @@ import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCStatement; +import com.sun.tools.javac.tree.JCTree.JCTry; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Name; -import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; /** * Wraps around javac's internal AST view to add useful features as well as the ability to visit parents from children, @@ -78,7 +79,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { * @param top The compilation unit, which serves as the top level node in the tree to be built. */ public JavacAST(Messager messager, Context context, JCCompilationUnit top) { - super(sourceName(top), packageDeclaration(top), new JavacImportList(top)); + super(sourceName(top), PackageName.getPackageName(top), new JavacImportList(top)); setTop(buildCompilationUnit(top)); this.context = context; this.messager = messager; @@ -103,8 +104,25 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { return cu.sourcefile == null ? null : cu.sourcefile.toString(); } - private static String packageDeclaration(JCCompilationUnit cu) { - return (cu.pid instanceof JCFieldAccess || cu.pid instanceof JCIdent) ? cu.pid.toString() : null; + // jdk9 support, types have changed, names stay the same + static class PackageName { + private static final Method packageNameMethod; + + static { + Method m = null; + try { + m = JCCompilationUnit.class.getDeclaredMethod("getPackageName"); + } catch (Exception e) {} + packageNameMethod = m; + } + + static String getPackageName(JCCompilationUnit cu) { + try { + Object pkg = packageNameMethod.invoke(cu); + return (pkg instanceof JCFieldAccess || pkg instanceof JCIdent) ? pkg.toString() : null; + } catch (Exception e) {} + return null; + } } public Context getContext() { diff --git a/src/core/lombok/javac/JavacImportList.java b/src/core/lombok/javac/JavacImportList.java index 2665ca7c..0f789f45 100644 --- a/src/core/lombok/javac/JavacImportList.java +++ b/src/core/lombok/javac/JavacImportList.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2014 The Project Lombok Authors. + * Copyright (C) 2013-2015 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 @@ -24,22 +24,22 @@ package lombok.javac; import java.util.ArrayList; import java.util.Collection; +import lombok.core.ImportList; +import lombok.core.LombokInternalAliasing; +import lombok.javac.JavacAST.PackageName; + import com.sun.tools.javac.tree.JCTree; 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.JCImport; import com.sun.tools.javac.util.List; -import lombok.core.ImportList; -import lombok.core.LombokInternalAliasing; - public class JavacImportList implements ImportList { - private final JCExpression pkg; + private final String pkgStr; private final List<JCTree> defs; public JavacImportList(JCCompilationUnit cud) { - this.pkg = cud.pid; + this.pkgStr = PackageName.getPackageName(cud); this.defs = cud.defs; } @@ -58,7 +58,6 @@ public class JavacImportList implements ImportList { } @Override public boolean hasStarImport(String packageName) { - String pkgStr = pkg == null ? null : pkg.toString(); if (pkgStr != null && pkgStr.equals(packageName)) return true; if ("java.lang".equals(packageName)) return true; @@ -86,7 +85,7 @@ public class JavacImportList implements ImportList { @Override public Collection<String> applyNameToStarImports(String startsWith, String name) { ArrayList<String> out = new ArrayList<String>(); - if (pkg != null && topLevelName(pkg).equals(startsWith)) out.add(pkg.toString() + "." + name); + if (pkgStr != null && topLevelName(pkgStr).equals(startsWith)) out.add(pkgStr + "." + name); for (JCTree def : defs) { if (!(def instanceof JCImport)) continue; @@ -110,8 +109,14 @@ public class JavacImportList implements ImportList { return tree.toString(); } + private String topLevelName(String packageName) { + int idx = packageName.indexOf("."); + if (idx == -1) return packageName; + return packageName.substring(0, idx); + } + @Override public String applyUnqualifiedNameToPackage(String unqualified) { - if (pkg == null) return unqualified; - return pkg.toString() + "." + unqualified; + if (pkgStr == null) return unqualified; + return pkgStr + "." + unqualified; } } diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 4e9be00b..fdc8a262 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -27,6 +27,7 @@ import static lombok.javac.Javac.*; import static lombok.javac.JavacAugments.JCTree_generatedNode; import java.lang.annotation.Annotation; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -56,6 +57,9 @@ import lombok.javac.JavacTreeMaker; import com.sun.tools.javac.code.BoundKind; import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.code.Scope; +import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.parser.Tokens.Comment; import com.sun.tools.javac.tree.DocCommentTable; import com.sun.tools.javac.tree.JCTree; @@ -871,6 +875,32 @@ public class JavacHandlerUtil { return (field.mods.flags & Flags.ENUM) != 0; } + // jdk9 support, types have changed, names stay the same + static class ClassSymbolMembersField { + private static final Field membersField; + private static final Method removeMethod; + + static { + Field f = null; + Method m = null; + try { + f = ClassSymbol.class.getField("members_field"); + m = f.getType().getMethod("remove", Symbol.class); + } catch (Exception e) {} + membersField = f; + removeMethod = m; + } + + static void remove(ClassSymbol from, Symbol toRemove) { + if (from == null) return; + try { + Scope scope = (Scope) membersField.get(from); + if (scope == null) return; + removeMethod.invoke(scope, toRemove); + } catch (Exception e) {} + } + } + /** * Adds the given new method declaration to the provided type AST Node. * Can also inject constructors. @@ -889,9 +919,7 @@ public class JavacHandlerUtil { JavacNode tossMe = typeNode.getNodeFor(def); if (tossMe != null) tossMe.up().removeChild(tossMe); type.defs = addAllButOne(type.defs, idx); - if (type.sym != null && type.sym.members_field != null) { - type.sym.members_field.remove(((JCMethodDecl)def).sym); - } + ClassSymbolMembersField.remove(type.sym, ((JCMethodDecl)def).sym); break; } } |