|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025, Red Hat, Inc. and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.jdt.core.tests.javac; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertArrayEquals; |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +import java.io.CharArrayWriter; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import javax.tools.JavaCompiler; |
| 24 | +import javax.tools.JavaFileObject; |
| 25 | +import javax.tools.StandardJavaFileManager; |
| 26 | +import javax.tools.ToolProvider; |
| 27 | + |
| 28 | +import org.eclipse.core.runtime.ILog; |
| 29 | +import org.eclipse.core.runtime.ILogListener; |
| 30 | +import org.eclipse.core.runtime.IStatus; |
| 31 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 32 | +import org.eclipse.core.runtime.Platform; |
| 33 | +import org.eclipse.jdt.core.compiler.IProblem; |
| 34 | +import org.eclipse.jdt.core.dom.AST; |
| 35 | +import org.eclipse.jdt.core.dom.ASTParser; |
| 36 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 37 | +import org.eclipse.jdt.core.dom.JavacCompilationUnitResolver; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +public class CachingClassReaderTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testIntersectionType() throws Exception { |
| 44 | + Path dir = Files.createTempDirectory(getClass().getName()); |
| 45 | + Path sourceFile = dir.resolve("A.java"); |
| 46 | + Files.write(sourceFile, """ |
| 47 | + class A<T extends java.util.function.IntSupplier & java.util.function.LongSupplier> { |
| 48 | + T f; |
| 49 | + A(T f) { |
| 50 | + this.f = f; |
| 51 | + } |
| 52 | + } |
| 53 | + """.getBytes()); |
| 54 | + |
| 55 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 56 | + StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); |
| 57 | + Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromPaths(List.of(sourceFile)); |
| 58 | + CharArrayWriter writer = new CharArrayWriter(2000); |
| 59 | + assertTrue(compiler.getTask(writer, fileManager, null, List.of("--release", "17"), null, compilationUnits1).call()); |
| 60 | + Path Bclass = dir.resolve("A.class"); |
| 61 | + assertTrue(Files.exists(Bclass)); |
| 62 | + |
| 63 | + String source = """ |
| 64 | + class I { |
| 65 | + A<Sup> a = new A<>(new Sup()); |
| 66 | + } |
| 67 | + class Sup implements java.util.function.IntSupplier, java.util.function.LongSupplier { |
| 68 | + public int getAsInt() { return 0; } |
| 69 | + public long getAsLong() { return 0; } |
| 70 | + } |
| 71 | + """; |
| 72 | + // What we really want to test is calling CachingClassSymbolClassReader multiple times for A$B |
| 73 | + // but as we prefer avoiding references to internal Javac types in tests, we just load the AST multiple times. |
| 74 | + for (int i = 0; i < 2; i++) { |
| 75 | + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
| 76 | + parser.setSource(source.toCharArray()); |
| 77 | + parser.setUnitName("I.java"); |
| 78 | + parser.setEnvironment(new String[] { dir.toString() }, null, null, true); |
| 79 | + parser.setResolveBindings(true); |
| 80 | + try (var _ = withoutLoggedError()) { |
| 81 | + var node = (CompilationUnit)parser.createAST(new NullProgressMonitor()); |
| 82 | + assertArrayEquals(new IProblem[0], node.getProblems()); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private AutoCloseable withoutLoggedError() { |
| 88 | + ILog log = Platform.getLog(JavacCompilationUnitResolver.class); |
| 89 | + List<IStatus> errors = new ArrayList<>(); |
| 90 | + ILogListener listener = (status, bundle) -> { |
| 91 | + if (status.getSeverity() == IStatus.ERROR) { |
| 92 | + errors.add(status); |
| 93 | + } |
| 94 | + }; |
| 95 | + log.addLogListener(listener); |
| 96 | + return () -> { |
| 97 | + log.removeLogListener(listener); |
| 98 | + assertEquals(List.of(), errors); |
| 99 | + }; |
| 100 | + } |
| 101 | +} |
0 commit comments