8325362: Allow to create a simple in-memory input JavaFileObject

Reviewed-by: jlaskey, darcy
This commit is contained in:
Jan Lahoda 2024-03-19 09:35:25 +00:00
parent c59c41aa6e
commit 5b6b514441
23 changed files with 268 additions and 196 deletions

View File

@ -223,4 +223,48 @@ public class SimpleJavaFileObject implements JavaFileObject {
public String toString() {
return getClass().getName() + "[" + toUri() + "]";
}
/**
* Creates a {@link JavaFileObject} which represents the given source content.
*
* <p>The provided {@code uri} will be returned from {@link #toUri()}.
* The provided {@code content} will be returned from {@link #getCharContent(boolean)}.
* The {@link #getKind()} method will return {@link Kind#SOURCE}.
*
* <p>All other methods will behave as described in the documentation in this class,
* as if the constructor is called with {@code uri} and {@code Kind.SOURCE}.
*
* <p>This method can be, for example, used to compile an in-memory String
* to a set of classfile in a target directory:
* {@snippet lang="java":
* var code = """
* public class CompiledCode {}
* """;
* var compiler = ToolProvider.getSystemJavaCompiler();
* var targetDirectory = "...";
* var task = compiler.getTask(null,
* null,
* null,
* List.of("-d", targetDirectory),
* null,
* List.of(SimpleJavaFileObject.forSource(URI.create("CompiledCode.java"), code)));
* if (!task.call()) {
* throw new IllegalStateException("Compilation failed!");
* }
* }
*
* @param uri that should be used for the resulting {@code JavaFileObject}
* @param content the content of the {@code JavaFileObject}
* @return a {@code JavaFileObject} representing the given source content.
* @since 23
*/
public static JavaFileObject forSource(URI uri, String content) {
return new SimpleJavaFileObject(uri, Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return content;
}
};
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, 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
@ -209,12 +209,8 @@ public class StringTemplateTest {
// System.out.println(source);
if (ToolProvider.getSystemJavaCompiler().getTask(null, fileManager, null,
List.of("--enable-preview", "-source", String.valueOf(Runtime.version().feature())), null,
List.of(new SimpleJavaFileObject(URI.create("StringTemplateTest$.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return source;
}
})).call()) {
List.of(SimpleJavaFileObject.forSource(URI.create("StringTemplateTest$.java"), source))
).call()) {
return fileManager.getClassLoader(CLASS_OUTPUT).loadClass("StringTemplateTest$");
} else {
throw new AssertionError("compilation failed");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, 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
@ -115,13 +115,8 @@ class APITest {
}
protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) {
return new SimpleJavaFileObject(
URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncoding) {
return content;
}
};
return SimpleJavaFileObject.forSource(
URI.create("myfo:///" + binaryName + ".java"), content);
}
protected void checkFiles(File dir, Set<String> expectFiles) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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
@ -188,12 +188,8 @@ public class RunTest {
}
JavaFileObject createFile(String name, final String body) {
return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return body;
}
};
return SimpleJavaFileObject.forSource(URI.create(name),
body);
}
void error(String msg) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2024, 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
@ -75,12 +75,9 @@ public class Test {
// verify the generated code is valid Java by compiling it
JavacTool tool2 = JavacTool.create();
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return out;
}
};
JavaFileObject fo =
SimpleJavaFileObject.forSource(URI.create("output"),
out);
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
boolean ok = t2.call();
if (!ok)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024, 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
@ -53,13 +53,9 @@ public class TestCircularClassfile {
this.sourceStr = sourceStr;
}
SimpleJavaFileObject getSource() {
return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return sourceStr;
}
};
JavaFileObject getSource() {
return SimpleJavaFileObject.forSource(URI.create("myfo:/Test.java"),
sourceStr);
}
}

View File

@ -33,13 +33,8 @@ public class T8295024 {
}
""";
private static final SimpleJavaFileObject FILE = new SimpleJavaFileObject(
URI.create("string:///Cyclic.java"), JavaFileObject.Kind.SOURCE) {
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
return SOURCE;
}
};
private static final JavaFileObject FILE = SimpleJavaFileObject.forSource(
URI.create("string:///Cyclic.java"), SOURCE);
public static void main(String[] args) throws Exception {
@ -47,7 +42,7 @@ public class T8295024 {
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final StringWriter output = new StringWriter();
final Iterable<String> options = Collections.singleton("-XDrawDiagnostics");
final Iterable<SimpleJavaFileObject> files = Collections.singleton(FILE);
final Iterable<JavaFileObject> files = Collections.singleton(FILE);
for (int i = 0; i < NUM_RUNS; i++)
compiler.getTask(output, null, null, options, null, files).call();

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018, Google Inc. All rights reserved.
* Copyright (c) 2024, 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
@ -34,17 +35,14 @@
import java.lang.classfile.*;
import java.lang.classfile.attribute.MethodParameterInfo;
import java.lang.classfile.attribute.MethodParametersAttribute;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
@ -73,13 +71,8 @@ public class LegacyOutputTest {
List<String> getParameterNames(String release) throws Exception {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavaFileObject fileObject =
new SimpleJavaFileObject(URI.create("Test.java"), Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return "class Test { void f(int x, int y) {} }";
}
};
SimpleJavaFileObject.forSource(URI.create("Test.java"),
"class Test { void f(int x, int y) {} }");
CompilationTask task =
tool.getTask(
null,

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2024, 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
@ -38,15 +38,12 @@ import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import static javax.tools.JavaFileObject.Kind;
public class T6608214 {
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create(""),Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class Test<S> { <T extends S & Runnable> void test(){}}";
}
};
JavaFileObject sfo =
SimpleJavaFileObject.forSource(URI.create(""),
"class Test<S> { <T extends S & Runnable> void test(){}}");
List<? extends JavaFileObject> files = Arrays.asList(sfo);
List<String> opts = Arrays.asList("-Xjcov");
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2024, 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
@ -40,7 +40,6 @@ import java.util.Arrays;
import java.util.List;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import static javax.tools.JavaFileObject.Kind;
import com.sun.source.util.JavacTask;
public class T6733837 extends ToolTester {
@ -52,11 +51,9 @@ public class T6733837 extends ToolTester {
}
public void exec() {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "\tclass ErroneousWithTab";
}
};
JavaFileObject sfo =
SimpleJavaFileObject.forSource(URI.create("myfo:/Test.java"),
"\tclass ErroneousWithTab");
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
List<? extends JavaFileObject> files = Arrays.asList(sfo);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2024, 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
@ -44,15 +44,11 @@ import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.tree.JCTree.*;
import static javax.tools.JavaFileObject.Kind;
public class T6852595 {
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class BadName { Object o = j; }";
}
};
JavaFileObject sfo =
SimpleJavaFileObject.forSource(URI.create("myfo:/Test.java"),
"class BadName { Object o = j; }");
List<? extends JavaFileObject> files = Arrays.asList(sfo);
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, 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
@ -482,12 +482,8 @@ public class TestSearchPaths {
}
JavaFileObject getSource(final String source) {
return new SimpleJavaFileObject(getURIFromSource(source), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return source;
}
};
return SimpleJavaFileObject.forSource(getURIFromSource(source),
source);
}
void callTask(List<String> options, List<JavaFileObject> files) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, 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
@ -29,10 +29,8 @@
* @run main TestTypeElement
*/
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import javax.lang.model.element.TypeElement;
@ -150,19 +148,7 @@ public class TestTypeElement {
}
private JavaFileObject createFileObject(String name, String body) {
return createFileObject(name, JavaFileObject.Kind.SOURCE, body);
}
private JavaFileObject createFileObject(String name, JavaFileObject.Kind kind, String body) {
try {
return new SimpleJavaFileObject(new URI("myfo:///" + name), kind) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return body;
}
};
} catch (URISyntaxException e) {
throw new IllegalArgumentException(name, e);
}
return SimpleJavaFileObject.forSource(URI.create("myfo:///" + name),
body);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, 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
@ -114,12 +114,8 @@ public class DocLintTest {
fm = javac.getStandardFileManager(null, null, null);
try {
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncoding) {
return code;
}
};
file = SimpleJavaFileObject.forSource(URI.create("Test.java"),
code);
test(Collections.<String>emptyList(),
Main.Result.OK,

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, 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
@ -30,17 +30,11 @@
*/
import com.sun.source.util.JavacTask;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class Abort {
@ -85,13 +79,9 @@ public class Abort {
bw.close();
}
SimpleJavaFileObject asJFO(java.io.File dir) {
return new SimpleJavaFileObject(new java.io.File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return contents;
}
};
JavaFileObject asJFO(java.io.File dir) {
return SimpleJavaFileObject.forSource(new java.io.File(dir, filename).toURI(),
contents);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, 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
@ -99,13 +99,9 @@ public class CompletionFailure {
bw.close();
}
SimpleJavaFileObject asJFO(java.io.File dir) {
return new SimpleJavaFileObject(new File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return contents;
}
};
JavaFileObject asJFO(java.io.File dir) {
return SimpleJavaFileObject.forSource(new File(dir, filename).toURI(),
contents);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, 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
@ -29,11 +29,9 @@
* @summary Proper lexing of various token kinds.
*/
import java.io.IOException;
import java.net.URI;
import java.util.Objects;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import com.sun.tools.javac.parser.JavaTokenizer;
@ -125,12 +123,8 @@ public class JavaLexerTest {
Context ctx = new Context();
Log log = Log.instance(ctx);
log.useSource(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return test.input;
}
});
log.useSource(SimpleJavaFileObject.forSource(URI.create("mem://Test.java"),
test.input));
char[] inputArr = test.input.toCharArray();
JavaTokenizer tokenizer = new JavaTokenizer(ScannerFactory.instance(ctx), inputArr, inputArr.length) {};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, 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
@ -58,12 +58,8 @@ public class T8239544 {
CompileState.ATTR, CompileState.FLOW, CompileState.TRANSTYPES, CompileState.TRANSPATTERNS, CompileState.UNLAMBDA, CompileState.LOWER}; //everything except GENERATE
public static void main(String... args) throws IOException {
var f = new SimpleJavaFileObject(URI.create("TestLambdaClass.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return "@Deprecated public class TestLambdaClass {{new Thread(() -> {});}}";
}
};
var f = SimpleJavaFileObject.forSource(URI.create("TestLambdaClass.java"),
"@Deprecated public class TestLambdaClass {{new Thread(() -> {});}}");
for (String compilePolicy : TESTED_COMPILE_POLICIES) {
for (CompileState stop : TESTED_COMPILE_STATES) {
var ctx = new Context();

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Google LLC. All rights reserved.
* Copyright (c) 2024, 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
@ -63,15 +64,10 @@ public class StringFoldingPosTest {
}
private static JavaFileObject makeSource(String name, String code) {
return new SimpleJavaFileObject(
return SimpleJavaFileObject.forSource(
URI.create(
"file:/" + name.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
};
code);
}
private void run(

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, 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
@ -48,12 +48,9 @@ import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
@ -67,30 +64,27 @@ public class InferenceUnitTest {
new InferenceUnitTest().runAll();
}
void runAll() throws URISyntaxException {
void runAll() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, List.of(new SimpleJavaFileObject(new URI("mem://Test.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return """
interface A<T> {}
interface B<T> extends A<T> {}
interface C<X,Y> extends A<X> {}
interface D<X,Y> extends A<Y> {}
interface E<T> extends C<T,T> {}
interface F<T> extends A<B<T>> {}
interface G<T extends Number> extends A<T> {}
interface H extends A<String> {}
interface I<T> extends H {}
class Test<T1 extends CharSequence&Runnable, T2 extends Number> {
}
interface RecursiveTest1Interface<IB extends RecursiveTest1Interface<IB>> { }
interface RecursiveTest1Use<BB extends RecursiveTest1Use<BB>> extends RecursiveTest1Interface<BB> { }
interface RecursiveTest2Interface<X> { }
interface RecursiveTest2Use<X extends RecursiveTest2Use<X, Y>, Y> extends RecursiveTest2Interface<Y> { }
""";
}
}));
String source = """
interface A<T> {}
interface B<T> extends A<T> {}
interface C<X,Y> extends A<X> {}
interface D<X,Y> extends A<Y> {}
interface E<T> extends C<T,T> {}
interface F<T> extends A<B<T>> {}
interface G<T extends Number> extends A<T> {}
interface H extends A<String> {}
interface I<T> extends H {}
class Test<T1 extends CharSequence&Runnable, T2 extends Number> {
}
interface RecursiveTest1Interface<IB extends RecursiveTest1Interface<IB>> { }
interface RecursiveTest1Use<BB extends RecursiveTest1Use<BB>> extends RecursiveTest1Interface<BB> { }
interface RecursiveTest2Interface<X> { }
interface RecursiveTest2Use<X extends RecursiveTest2Use<X, Y>, Y> extends RecursiveTest2Interface<Y> { }
""";
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, null, null, List.of(SimpleJavaFileObject.forSource(URI.create("mem://Test.java"), source)));
task.enter();
context = task.getContext();
infer = Infer.instance(context);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, 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
@ -41,19 +41,14 @@ import com.sun.tools.javac.util.Assert;
import java.util.HashSet;
import java.util.Set;
import javax.lang.model.util.ElementFilter;
import javax.tools.JavaFileObject;
public class ElementFilterRecordComponentTest {
public static void main(String... args) throws IOException {
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
JavacTask t = (JavacTask) c.getTask(null, null, null, null, null,
List.of(new SimpleJavaFileObject(URI.create("TestClass.java"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return "record R(int val1, int val2) {}";
}
}));
List.of(SimpleJavaFileObject.forSource(URI.create("TestClass.java"),
"record R(int val1, int val2) {}")));
TypeElement record = (TypeElement) t.analyze().iterator().next();
Set<RecordComponentElement> recordSet = ElementFilter.recordComponentsIn(new HashSet<>(record.getEnclosedElements()));
Assert.check(recordSet.size() == 2);

View File

@ -0,0 +1,137 @@
/*
* Copyright (c) 2024, 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 8325362
* @summary Test SimpleJavaFileObject
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @run main TestSimpleJavaFileObject
*/
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import toolbox.TestRunner;
public class TestSimpleJavaFileObject extends TestRunner {
public TestSimpleJavaFileObject() {
super(System.err);
}
public static void main(String... args) throws Exception {
TestSimpleJavaFileObject t = new TestSimpleJavaFileObject();
t.runTests(m -> new Object[] { Paths.get(m.getName()) });
}
@Test
public void testForSource(Path p) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<String> errors = new ArrayList<>();
DiagnosticListener<JavaFileObject> noErrors = d -> {
if (d.getKind() == Kind.ERROR) {
errors.add(d.getSource().toUri().toString() + ":" +
d.getLineNumber() + ":" +
d.getColumnNumber() + ":" +
d.getCode());
}
};
try (JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
LoggingFileManager rfm = new LoggingFileManager(fm)) {
JavaFileObject src = SimpleJavaFileObject.forSource(URI.create("mem:///Test.java"),
"""
public class Test {}
""");
assertTrue("compilation didn't succeed!",
compiler.getTask(null, rfm, noErrors, null, null, List.of(src))
.call());
assertTrue("no compilation errors expected, but got: " + errors,
errors.isEmpty());
Set<String> expectedWrittenClasses = Set.of("Test");
assertTrue("compiled correct classes: " + rfm.writtenClasses,
expectedWrittenClasses.equals(rfm.writtenClasses));
}
errors.clear();
JavaFileObject src = SimpleJavaFileObject.forSource(URI.create("mem:///Test.java"),
"""
public class Test {
Unknown u;
}
""");
assertTrue("compilation succeeded unexpectedly!",
!compiler.getTask(null, null, noErrors, null, null, List.of(src))
.call());
List<String> expectedCompilationErrors = List.of(
"mem:///Test.java:2:5:compiler.err.cant.resolve.location"
);
assertTrue("incorrect compilation errors, expected: " + expectedCompilationErrors +
"actual: " + errors,
expectedCompilationErrors.equals(errors));
}
private static final class LoggingFileManager extends ForwardingJavaFileManager<JavaFileManager> {
private final Set<String> writtenClasses = new HashSet<>();
public LoggingFileManager(JavaFileManager fileManager) {
super(fileManager);
}
@Override
public JavaFileObject getJavaFileForOutput(Location location,
String className,
JavaFileObject.Kind kind,
FileObject sibling) throws IOException {
writtenClasses.add(className);
return super.getJavaFileForOutput(location, className, kind, sibling);
}
}
private static void assertTrue(String message, boolean c) {
if (!c) {
throw new AssertionError(message);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, 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
@ -24,7 +24,6 @@
package snippets;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
@ -35,7 +34,6 @@ import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -557,11 +555,7 @@ public class SnippetUtils {
}
}""".formatted(body);
};
JavaFileObject fo = new SimpleJavaFileObject(uri, JavaFileObject.Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return compUnit;
}
};
JavaFileObject fo = SimpleJavaFileObject.forSource(uri, compUnit);
JavaFileManager fm = compiler.getStandardFileManager(dl, null, null);