File: ReusableContext.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (229 lines) | stat: -rw-r--r-- 8,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (c) 2015, 2017, 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.
 *
 * 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 combo;

import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskEvent.Kind;
import com.sun.source.util.TaskListener;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.api.MultiTaskListener;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.comp.CompileStates;
import com.sun.tools.javac.comp.Enter;
import com.sun.tools.javac.comp.Modules;
import com.sun.tools.javac.main.Arguments;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import java.util.HashSet;
import java.util.Set;

/**
 * A reusable context is a context that can be used safely across multiple compilation rounds
 * arising from execution of a combo test. It achieves reuse by replacing some components
 * (most notably JavaCompiler and Log) with reusable counterparts, and by exposing a method
 * to cleanup leftovers from previous compilation.
 * <p>
 * There are, however, situations in which reusing the context is not safe: (i) when different
 * compilations are using different sets of compiler options (as most option values are cached
 * inside components themselves) and (ii) when the compilation unit happens to redefine classes
 * in the java.* packages.
 */
class ReusableContext extends Context implements TaskListener {

    Set<CompilationUnitTree> roots = new HashSet<>();

    String opts;
    boolean polluted = false;

    ReusableContext() {
        super();
        put(Log.logKey, ReusableLog.factory);
        put(JavaCompiler.compilerKey, ReusableJavaCompiler.factory);
    }

    void clear() {
        drop(Arguments.argsKey);
        drop(DiagnosticListener.class);
        drop(Log.outKey);
        drop(Log.errKey);
        drop(JavaFileManager.class);
        drop(JavacTask.class);

        if (ht.get(Log.logKey) instanceof ReusableLog) {
            //log already inited - not first round
            ((ReusableLog)Log.instance(this)).clear();
            Enter.instance(this).newRound();
            ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
            Types.instance(this).newRound();
            Check.instance(this).newRound();
            Modules.instance(this).newRound();
            Annotate.instance(this).newRound();
            CompileStates.instance(this).clear();
            MultiTaskListener.instance(this).clear();

            //find if any of the roots have redefined java.* classes
            Symtab syms = Symtab.instance(this);
            pollutionScanner.scan(roots, syms);
            roots.clear();
        }
    }

    /**
     * This scanner detects as to whether the shared context has been polluted. This happens
     * whenever a compiled program redefines a core class (in 'java.*' package) or when
     * (typically because of cyclic inheritance) the symbol kind of a core class has been touched.
     */
    TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() {
        @Override
        public Void visitClass(ClassTree node, Symtab syms) {
            Symbol sym = ((JCClassDecl)node).sym;
            if (sym != null) {
                syms.removeClass(sym.packge().modle, sym.flatName());
                Type sup = supertype(sym);
                if (isCoreClass(sym) ||
                        (sup != null && isCoreClass(sup.tsym) && sup.tsym.kind != Kinds.Kind.TYP)) {
                    polluted = true;
                }
            }
            return super.visitClass(node, syms);
        }

        private boolean isCoreClass(Symbol s) {
            return s.flatName().toString().startsWith("java.");
        }

        private Type supertype(Symbol s) {
            if (s.type == null ||
                    !s.type.hasTag(TypeTag.CLASS)) {
                return null;
            } else {
                ClassType ct = (ClassType)s.type;
                return ct.supertype_field;
            }
        }
    };

    @Override
    public void finished(TaskEvent e) {
        if (e.getKind() == Kind.PARSE) {
            roots.add(e.getCompilationUnit());
        }
    }

    @Override
    public void started(TaskEvent e) {
        //do nothing
    }

    <T> void drop(Key<T> k) {
        ht.remove(k);
    }

    <T> void drop(Class<T> c) {
        ht.remove(key(c));
    }

    /**
     * Reusable JavaCompiler; exposes a method to clean up the component from leftovers associated with
     * previous compilations.
     */
    static class ReusableJavaCompiler extends JavaCompiler {

        static Factory<JavaCompiler> factory = ReusableJavaCompiler::new;

        ReusableJavaCompiler(Context context) {
            super(context);
        }

        @Override
        public void close() {
            //do nothing
        }

        void clear() {
            newRound();
        }

        @Override
        protected void checkReusable() {
            //do nothing - it's ok to reuse the compiler
        }
    }

    /**
     * Reusable Log; exposes a method to clean up the component from leftovers associated with
     * previous compilations.
     */
    static class ReusableLog extends Log {

        static Factory<Log> factory = ReusableLog::new;

        Context context;

        ReusableLog(Context context) {
            super(context);
            this.context = context;
        }

        void clear() {
            recorded.clear();
            sourceMap.clear();
            nerrors = 0;
            nwarnings = 0;
            //Set a fake listener that will lazily lookup the context for the 'real' listener. Since
            //this field is never updated when a new task is created, we cannot simply reset the field
            //or keep old value. This is a hack to workaround the limitations in the current infrastructure.
            diagListener = new DiagnosticListener<JavaFileObject>() {
                DiagnosticListener<JavaFileObject> cachedListener;

                @Override
                @SuppressWarnings("unchecked")
                public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                    if (cachedListener == null) {
                        cachedListener = context.get(DiagnosticListener.class);
                    }
                    cachedListener.report(diagnostic);
                }
            };
        }
    }
}