8040822: Duplicated notifications can be sent to TaskListener

Fri, 09 May 2014 09:36:35 +0200

author
jlahoda
date
Fri, 09 May 2014 09:36:35 +0200
changeset 2386
f8e84de96252
parent 2385
856d94394294
child 2387
cf626fb754aa

8040822: Duplicated notifications can be sent to TaskListener
Summary: The analyze finished event must only be sent on the first call to JavaCompiler.flow for a given class.
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/main/JavaCompiler.java file | annotate | diff | comparison | revisions
test/tools/javac/api/taskListeners/EventsBalancedTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Fri May 09 19:42:25 2014 -0600
     1.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Fri May 09 09:36:35 2014 +0200
     1.3 @@ -35,9 +35,6 @@
     1.4  import java.util.Queue;
     1.5  import java.util.ResourceBundle;
     1.6  import java.util.Set;
     1.7 -import java.util.logging.Handler;
     1.8 -import java.util.logging.Level;
     1.9 -import java.util.logging.Logger;
    1.10  
    1.11  import javax.annotation.processing.Processor;
    1.12  import javax.lang.model.SourceVersion;
    1.13 @@ -1304,11 +1301,16 @@
    1.14       * Perform dataflow checks on an attributed parse tree.
    1.15       */
    1.16      protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
    1.17 +        if (compileStates.isDone(env, CompileState.FLOW)) {
    1.18 +            results.add(env);
    1.19 +            return;
    1.20 +        }
    1.21 +
    1.22          try {
    1.23              if (shouldStop(CompileState.FLOW))
    1.24                  return;
    1.25  
    1.26 -            if (relax || compileStates.isDone(env, CompileState.FLOW)) {
    1.27 +            if (relax) {
    1.28                  results.add(env);
    1.29                  return;
    1.30              }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/api/taskListeners/EventsBalancedTest.java	Fri May 09 09:36:35 2014 +0200
     2.3 @@ -0,0 +1,127 @@
     2.4 +/*
     2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug     8040822
    2.30 + * @summary Check that all TaskEvents are balanced.
    2.31 + */
    2.32 +
    2.33 +import java.io.*;
    2.34 +import java.net.URI;
    2.35 +import java.util.*;
    2.36 +import java.util.Map.Entry;
    2.37 +
    2.38 +import javax.tools.*;
    2.39 +
    2.40 +import com.sun.source.util.*;
    2.41 +import com.sun.source.util.TaskEvent.Kind;
    2.42 +import com.sun.tools.javac.api.JavacTool;
    2.43 +import com.sun.tools.javac.comp.CompileStates.CompileState;
    2.44 +
    2.45 +public class EventsBalancedTest {
    2.46 +    JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
    2.47 +    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    2.48 +
    2.49 +    public static void main(String... args) throws IOException {
    2.50 +        new EventsBalancedTest().test();
    2.51 +    }
    2.52 +
    2.53 +    void test() throws IOException {
    2.54 +        TestSource a = new TestSource("B", "class B extends A { }");
    2.55 +        TestSource b = new TestSource("A", "abstract class A { }");
    2.56 +
    2.57 +        test(null, Arrays.asList(a, b));
    2.58 +        test(null, Arrays.asList(b, a));
    2.59 +        test(Arrays.asList("-XD-relax"), Arrays.asList(a, b));
    2.60 +        test(Arrays.asList("-XD-relax"), Arrays.asList(b, a));
    2.61 +
    2.62 +        for (CompileState stop : CompileState.values()) {
    2.63 +            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
    2.64 +                               "-XDshouldStopPolicyIfError=" + stop),
    2.65 +                 Arrays.asList(a, b));
    2.66 +            test(Arrays.asList("-XDshouldStopPolicyIfNoError=" + stop,
    2.67 +                               "-XDshouldStopPolicyIfError=" + stop),
    2.68 +                 Arrays.asList(b, a));
    2.69 +        }
    2.70 +    }
    2.71 +
    2.72 +    void test(Iterable<String> options, Iterable<JavaFileObject> files) throws IOException {
    2.73 +        StringWriter sw = new StringWriter();
    2.74 +        PrintWriter pw = new PrintWriter(sw);
    2.75 +        TestListener listener = new TestListener();
    2.76 +        JavacTask task = tool.getTask(pw, fm, null, options, null, files);
    2.77 +
    2.78 +        task.setTaskListener(listener);
    2.79 +
    2.80 +        task.call();
    2.81 +
    2.82 +        for (Entry<Kind, Integer> e : listener.kind2Count.entrySet()) {
    2.83 +            if (e.getValue() != null && e.getValue() != 0) {
    2.84 +                throw new IllegalStateException("Not balanced event: " + e.getKey());
    2.85 +            }
    2.86 +        }
    2.87 +    }
    2.88 +
    2.89 +    static class TestListener implements TaskListener {
    2.90 +        final Map<Kind, Integer> kind2Count = new HashMap<>();
    2.91 +
    2.92 +        int get(Kind k) {
    2.93 +            Integer count = kind2Count.get(k);
    2.94 +
    2.95 +            if (count == null)
    2.96 +                kind2Count.put(k, count = 0);
    2.97 +
    2.98 +            return count;
    2.99 +        }
   2.100 +
   2.101 +        @Override
   2.102 +        public void started(TaskEvent e) {
   2.103 +            kind2Count.put(e.getKind(), get(e.getKind()) + 1);
   2.104 +        }
   2.105 +
   2.106 +        @Override
   2.107 +        public void finished(TaskEvent e) {
   2.108 +            int count = get(e.getKind());
   2.109 +
   2.110 +            if (count <= 0)
   2.111 +                throw new IllegalStateException("count<=0 for: " + e.getKind());
   2.112 +
   2.113 +            kind2Count.put(e.getKind(), count - 1);
   2.114 +        }
   2.115 +
   2.116 +    }
   2.117 +    static class TestSource extends SimpleJavaFileObject {
   2.118 +        final String content;
   2.119 +        public TestSource(String fileName, String content) {
   2.120 +            super(URI.create("myfo:/" + fileName + ".java"), JavaFileObject.Kind.SOURCE);
   2.121 +            this.content = content;
   2.122 +        }
   2.123 +
   2.124 +        @Override
   2.125 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   2.126 +            return content;
   2.127 +        }
   2.128 +    }
   2.129 +
   2.130 +}

mercurial