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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* Copyright (c) 2014, 2015, 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.
*/
/**
* @test
* @bug 7101822
* @summary Verify that the processing of classes in TypeEnter runs in the correct order.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.code
* jdk.compiler/com.sun.tools.javac.file
* jdk.compiler/com.sun.tools.javac.tree
* jdk.compiler/com.sun.tools.javac.util
* @build annotations.TriggersComplete annotations.TriggersCompleteRepeat annotations.Phase
* @build DependenciesTest
* @run main DependenciesTest
*/
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;
import java.util.stream.Stream;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import annotations.*;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Context.Factory;
import com.sun.tools.javac.util.Dependencies;
public class DependenciesTest {
public static void main(String... args) throws IOException {
new DependenciesTest().run();
}
void run() throws IOException {
Path src = Paths.get(System.getProperty("test.src"), "tests");
try (Stream<Path> tests = Files.list(src)) {
tests.map(p -> Files.isRegularFile(p) ? Stream.of(p) : silentWalk(p))
.forEach(this :: runTest);
}
}
Stream<Path> silentWalk(Path src) {
try {
return Files.walk(src).filter(Files :: isRegularFile);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
void runTest(Stream<Path> inputs) {
JavacTool tool = JavacTool.create();
try (JavacFileManager fm = tool.getStandardFileManager(null, null, null)) {
Path classes = Paths.get(System.getProperty("test.classes"));
Iterable<? extends JavaFileObject> reconFiles =
fm.getJavaFileObjectsFromFiles(inputs.sorted().map(p -> p.toFile()) :: iterator);
List<String> options = Arrays.asList("-classpath", classes.toAbsolutePath().toString());
JavacTask reconTask = tool.getTask(null, fm, null, options, null, reconFiles);
Iterable<? extends CompilationUnitTree> reconUnits = reconTask.parse();
JavacTrees reconTrees = JavacTrees.instance(reconTask);
SearchAnnotations scanner = new SearchAnnotations(reconTrees,
reconTask.getElements());
List<JavaFileObject> validateFiles = new ArrayList<>();
reconTask.analyze();
scanner.scan(reconUnits, null);
for (CompilationUnitTree cut : reconUnits) {
validateFiles.add(ClearAnnotations.clearAnnotations(reconTrees, cut));
}
Context validateContext = new Context();
TestDependencies.preRegister(validateContext);
JavacTask validateTask =
tool.getTask(null, fm, null, options, null, validateFiles, validateContext);
validateTask.analyze();
TestDependencies deps = (TestDependencies) Dependencies.instance(validateContext);
if (!scanner.topLevel2Expected.equals(deps.topLevel2Completing)) {
throw new IllegalStateException( "expected=" + scanner.topLevel2Expected +
"; actual=" + deps.topLevel2Completing);
}
} catch (IOException ex) {
throw new IllegalStateException(ex);
} finally {
inputs.close();
}
}
static final class TestDependencies extends Dependencies {
public static void preRegister(Context context) {
context.put(dependenciesKey, (Factory<Dependencies>) TestDependencies :: new);
}
public TestDependencies(Context context) {
super(context);
}
final Stack<PhaseDescription> inProcess = new Stack<>();
String topLevelMemberEnter;
Map<String, Set<PhaseDescription>> topLevel2Completing = new HashMap<>();
@Override
public void push(ClassSymbol s, CompletionCause phase) {
String flatname = s.flatName().toString();
for (Phase p : Phase.values()) {
if (phase == p.cause) {
inProcess.push(new PhaseDescription(flatname, p));
return ;
}
}
if (phase == CompletionCause.MEMBER_ENTER) {
if (inProcess.isEmpty()) {
topLevelMemberEnter = flatname;
} else {
for (PhaseDescription running : inProcess) {
if (running == null)
continue;
Set<PhaseDescription> completing =
topLevel2Completing.computeIfAbsent(running.flatname, $ -> new HashSet<>());
completing.add(new PhaseDescription(flatname, running.phase));
}
}
}
inProcess.push(null);
}
@Override
public void pop() {
inProcess.pop();
}
}
static final class SearchAnnotations extends TreePathScanner<Void, Void> {
final Trees trees;
final Elements elements;
final TypeElement triggersCompleteAnnotation;
final TypeElement triggersCompleteRepeatAnnotation;
final Map<String, Set<PhaseDescription>> topLevel2Expected =
new HashMap<>();
public SearchAnnotations(Trees trees, Elements elements) {
this.trees = trees;
this.elements = elements;
this.triggersCompleteAnnotation =
elements.getTypeElement(TriggersComplete.class.getName());
this.triggersCompleteRepeatAnnotation =
elements.getTypeElement(TriggersCompleteRepeat.class.getName());
}
@Override
public Void visitClass(ClassTree node, Void p) {
TypeElement te = (TypeElement) trees.getElement(getCurrentPath());
Set<PhaseDescription> expected = new HashSet<>();
for (AnnotationMirror am : getTriggersCompleteAnnotation(te)) {
TypeMirror of = (TypeMirror) findAttribute(am, "of").getValue();
Name ofName = elements.getBinaryName((TypeElement) ((DeclaredType) of).asElement());
Element at = (Element) findAttribute(am, "at").getValue();
Phase phase = Phase.valueOf(at.getSimpleName().toString());
expected.add(new PhaseDescription(ofName.toString(), phase));
}
if (!expected.isEmpty())
topLevel2Expected.put(elements.getBinaryName(te).toString(), expected);
return super.visitClass(node, p);
}
Collection<AnnotationMirror> getTriggersCompleteAnnotation(TypeElement te) {
for (AnnotationMirror am : te.getAnnotationMirrors()) {
if (triggersCompleteAnnotation.equals(am.getAnnotationType().asElement())) {
return Collections.singletonList(am);
}
if (triggersCompleteRepeatAnnotation.equals(am.getAnnotationType().asElement())) {
return (Collection<AnnotationMirror>) findAttribute(am, "value").getValue();
}
}
return Collections.emptyList();
}
AnnotationValue findAttribute(AnnotationMirror mirror, String name) {
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> e :
mirror.getElementValues().entrySet()) {
if (e.getKey().getSimpleName().contentEquals(name)) {
return e.getValue();
}
}
throw new IllegalStateException("Could not find " + name + " in " + mirror);
}
}
static final class ClearAnnotations extends TreePathScanner<Void, Void> {
final SourcePositions positions;
final List<int[]> spans2Clear = new ArrayList<>();
ClearAnnotations(Trees trees) {
this.positions = trees.getSourcePositions();
}
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
removeCurrentNode();
return null;
}
@Override
public Void visitImport(ImportTree node, Void p) {
if (node.getQualifiedIdentifier().toString().startsWith("annotations.")) {
removeCurrentNode();
return null;
}
return super.visitImport(node, p);
}
void removeCurrentNode() {
CompilationUnitTree topLevel = getCurrentPath().getCompilationUnit();
Tree node = getCurrentPath().getLeaf();
spans2Clear.add(new int[] {(int) positions.getStartPosition(topLevel, node),
(int) positions.getEndPosition(topLevel, node)});
}
static JavaFileObject clearAnnotations(Trees trees, CompilationUnitTree cut)
throws IOException {
ClearAnnotations a = new ClearAnnotations(trees);
a.scan(cut, null);
Collections.sort(a.spans2Clear, (s1, s2) -> s2[0] - s1[0]);
StringBuilder result = new StringBuilder(cut.getSourceFile().getCharContent(true));
for (int[] toClear : a.spans2Clear) {
result.delete(toClear[0], toClear[1]);
}
return new TestJavaFileObject(cut.getSourceFile().toUri(), result.toString());
}
}
static final class PhaseDescription {
final String flatname;
final Phase phase;
public PhaseDescription(String flatname, Phase phase) {
this.flatname = flatname;
this.phase = phase;
}
@Override
public String toString() {
return "@annotations.TriggersComplete(of=" + flatname + ".class," +
"at=annotations.Phase." + phase + ')';
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.flatname);
hash = 89 * hash + Objects.hashCode(this.phase);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PhaseDescription other = (PhaseDescription) obj;
if (!Objects.equals(this.flatname, other.flatname)) {
return false;
}
if (this.phase != other.phase) {
return false;
}
return true;
}
}
static final class TestJavaFileObject extends SimpleJavaFileObject {
private final String content;
public TestJavaFileObject(URI uri, String content) {
super(uri, Kind.SOURCE);
this.content = content;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
}
}
|