8042441: sjavac does not track dependencies
authorohrstrom
Thu, 08 May 2014 00:22:31 +0200
changeset 24296 296e59a84663
parent 24295 130c062a5087
child 24297 84baad0ac4d7
8042441: sjavac does not track dependencies Summary: Add support for tracking fully qualified references. Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/sjavac/comp/AttrWithDeps.java
langtools/src/share/classes/com/sun/tools/sjavac/server/CompilerThread.java
langtools/test/tools/sjavac/SJavac.java
langtools/test/tools/sjavac/SJavacWrapper.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed May 07 23:55:41 2014 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu May 08 00:22:31 2014 +0200
@@ -3214,6 +3214,14 @@
         result = checkId(tree, env1.enclClass.sym.type, sym, env, resultInfo);
     }
 
+    /** Report dependencies.
+     * @param from The enclosing class sym
+     * @param to   The found identifier that the class depends on.
+     */
+    public void reportDependence(Symbol from, Symbol to) {
+        // Override if you want to collect the reported dependencies.
+    }
+
     public void visitSelect(JCFieldAccess tree) {
         // Determine the expected kind of the qualifier expression.
         int skind = 0;
@@ -3341,6 +3349,10 @@
 
         env.info.selectSuper = selectSuperPrev;
         result = checkId(tree, site, sym, env, resultInfo);
+
+        if ((tree.sym.kind & TYP) != 0) {
+            reportDependence(env.enclClass.sym, tree.sym);
+        }
     }
     //where
         /** Determine symbol referenced by a Select expression,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/src/share/classes/com/sun/tools/sjavac/comp/AttrWithDeps.java	Thu May 08 00:22:31 2014 +0200
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.sun.tools.sjavac.comp;
+
+import com.sun.tools.javac.comp.Attr;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.code.Symbol;
+
+/** Subclass to Attr that overrides reportDepedence.
+ *
+ * <p><b>This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own
+ * risk.  This code and its internal interfaces are subject to change
+ * or deletion without notice.</b></p>
+ */
+public class AttrWithDeps extends Attr {
+
+    /** The dependency database
+     */
+    protected Dependencies deps;
+
+    protected AttrWithDeps(Context context) {
+        super(context);
+        deps = Dependencies.instance(context);
+    }
+
+    public static void preRegister(Context context) {
+        context.put(attrKey, new Context.Factory<Attr>() {
+            public Attr make(Context c) {
+                Attr instance = new AttrWithDeps(c);
+                c.put(Attr.class, instance);
+                return instance;
+            }
+        });
+    }
+
+    /** Collect dependencies in the enclosing class
+     * @param from The enclosing class sym
+     * @param to   The enclosing classes references this sym.
+     * */
+    @Override
+    public void reportDependence(Symbol from, Symbol to) {
+        // Capture dependencies between the packages.
+        deps.collect(from.packge().fullname, to.packge().fullname);
+    }
+}
--- a/langtools/src/share/classes/com/sun/tools/sjavac/server/CompilerThread.java	Wed May 07 23:55:41 2014 +0200
+++ b/langtools/src/share/classes/com/sun/tools/sjavac/server/CompilerThread.java	Thu May 08 00:22:31 2014 +0200
@@ -51,6 +51,7 @@
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.BaseFileManager;
 import com.sun.tools.javac.util.StringUtils;
+import com.sun.tools.sjavac.comp.AttrWithDeps;
 import com.sun.tools.sjavac.comp.Dependencies;
 import com.sun.tools.sjavac.comp.JavaCompilerWithDeps;
 import com.sun.tools.sjavac.comp.SmartFileManager;
@@ -131,6 +132,7 @@
         context = new Context();
         context.put(JavaFileManager.class, smartFileManager);
         ResolveWithDeps.preRegister(context);
+        AttrWithDeps.preRegister(context);
         JavaCompilerWithDeps.preRegister(context, this);
         subTasks = new ArrayList<>();
     }
--- a/langtools/test/tools/sjavac/SJavac.java	Wed May 07 23:55:41 2014 +0200
+++ b/langtools/test/tools/sjavac/SJavac.java	Thu May 08 00:22:31 2014 +0200
@@ -83,6 +83,7 @@
         compileWithInvisibleSources();
         compileCircularSources();
         compileExcludingDependency();
+        incrementalCompileTestFullyQualifiedRef();
 
         delete(gensrc);
         delete(gensrc2);
@@ -410,6 +411,43 @@
                                      "bin/javac_state");
     }
 
+    void incrementalCompileTestFullyQualifiedRef() throws Exception {
+        System.out.println("Verify that \"alfa.omega.A a;\" does create a proper dependency.");
+        System.out.println("----------------------------------------------------------------");
+
+        populate(gensrc,
+                 "alfa/omega/A.java",
+                 "package alfa.omega; public class A { "+
+                 "  public final static int DEFINITION = 18; "+
+                 "  public void hello() { }"+
+                 "}",
+                 "beta/B.java",
+                 "package beta; public class B { "+
+                 "  public void world() { alfa.omega.A a; }"+
+                 "}");
+
+        compile("gensrc", "-d", "bin", "-j", "1",
+                "--server:portfile=testserver,background=false", "--log=debug");
+        Map<String,Long> previous_bin_state = collectState(bin);
+
+        // Change pubapi of A, this should trigger a recompile of B.
+        populate(gensrc,
+                 "alfa/omega/A.java",
+                 "package alfa.omega; public class A { "+
+                 "  public final static int DEFINITION = 19; "+
+                 "  public void hello() { }"+
+                 "}");
+
+        compile("gensrc", "-d", "bin", "-j", "1",
+                "--server:portfile=testserver,background=false", "--log=debug");
+        Map<String,Long> new_bin_state = collectState(bin);
+
+        verifyNewerFiles(previous_bin_state, new_bin_state,
+                         "bin/alfa/omega/A.class",
+                         "bin/beta/B.class",
+                         "bin/javac_state");
+    }
+
     void removeFrom(Path dir, String... args) throws IOException {
         for (String filename : args) {
             Path p = dir.resolve(filename);
--- a/langtools/test/tools/sjavac/SJavacWrapper.java	Wed May 07 23:55:41 2014 +0200
+++ b/langtools/test/tools/sjavac/SJavacWrapper.java	Thu May 08 00:22:31 2014 +0200
@@ -26,6 +26,7 @@
  * @summary Test all aspects of sjavac.
  *
  * @bug 8004658
+ * @bug 8042441
  * @bug 8042699
  * @summary Add internal smart javac wrapper to solve JEP 139
  *