Skip to content

Commit

Permalink
JDK-8264664: use text blocks in javac module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-gibbons committed Apr 3, 2021
1 parent eb0ac86 commit 064110a
Show file tree
Hide file tree
Showing 16 changed files with 618 additions and 491 deletions.
43 changes: 24 additions & 19 deletions test/langtools/tools/javac/modules/AddLimitMods.java
Expand Up @@ -361,22 +361,24 @@ public void testRuntime2Compile(Path base) throws Exception {

StringBuilder testClassNamed = new StringBuilder();

testClassNamed.append("package test;\n" +
"public class Test {\n" +
" public static void main(String... args) throws Exception {\n");
testClassNamed.append("""
package test;
public class Test {
public static void main(String... args) throws Exception {
""");

for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
testClassNamed.append(" System.err.println(\"visible:" + e.getKey() + ":\" + ModuleLayer.boot().findModule(\"" + e.getKey() + "\").isPresent());\n");
}

testClassNamed.append(" Class<?> cp = Class.forName(Test.class.getClassLoader().getUnnamedModule(), \"cp.CP\");\n");
testClassNamed.append(" cp.getDeclaredMethod(\"runMe\").invoke(null);\n");

testClassNamed.append(" Class<?> automatic = Class.forName(ModuleLayer.boot().findModule(\"automatic\").get(), \"automatic.Automatic\");\n");
testClassNamed.append(" automatic.getDeclaredMethod(\"runMe\").invoke(null);\n");

testClassNamed.append(" }\n" +
"}");
testClassNamed.append("""
Class<?> cp = Class.forName(Test.class.getClassLoader().getUnnamedModule(), "cp.CP");
cp.getDeclaredMethod("runMe").invoke(null);
Class<?> automatic = Class.forName(ModuleLayer.boot().findModule("automatic").get(), "automatic.Automatic");
automatic.getDeclaredMethod("runMe").invoke(null);
}
}
""");

tb.writeJavaFiles(m2Runtime, moduleInfo, testClassNamed.toString());

Expand Down Expand Up @@ -415,8 +417,10 @@ public void testRuntime2Compile(Path base) throws Exception {

tb.writeJavaFiles(m2,
moduleInfo,
"package test;\n" +
"public class Test {}\n");
"""
package test;
public class Test {}
""");

List<String> auxOptions = success ? Arrays.asList(
"--processor-path", System.getProperty("test.class.path"),
Expand Down Expand Up @@ -448,12 +452,13 @@ private String generateCheckAccessibleClass(String fqn) {
"public class " + simpleName + " {" +
" public static void runMe() throws Exception {");
for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
checkClassesAccessible.append("try {");
checkClassesAccessible.append("Class.forName(\"" + e.getValue() + "\").newInstance();");
checkClassesAccessible.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":true\");");
checkClassesAccessible.append("} catch (Exception ex) {");
checkClassesAccessible.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":false\");");
checkClassesAccessible.append("}");
checkClassesAccessible
.append("try {")
.append("Class.forName(\"" + e.getValue() + "\").newInstance();")
.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":true\");")
.append("} catch (Exception ex) {")
.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":false\");")
.append("}");
}

checkClassesAccessible.append(" }\n" +
Expand Down
50 changes: 27 additions & 23 deletions test/langtools/tools/javac/modules/AnnotationProcessing.java
Expand Up @@ -906,7 +906,8 @@ public void testGenerateSingleModule(Path base) throws Exception {
runCompiler(base,
m1,
classes,
"createSource(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, \"impl\", \"impl\"" + originating + "), \"impl\", \"impl\")",
"""
createSource(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "impl", "impl\"""" + originating + "), \"impl\", \"impl\")",
options);
assertFileExists(classes, modulePath, "impl", "impl");
}
Expand Down Expand Up @@ -1044,28 +1045,31 @@ private void runCompiler(Path base, Path src, Path classes,

private void compileAP(Path target, String code) {
String processorCode =
"import java.util.*;\n" +
"import javax.annotation.processing.*;\n" +
"import javax.lang.model.*;\n" +
"import javax.lang.model.element.*;\n" +
"import javax.lang.model.type.*;\n" +
"import javax.lang.model.util.*;\n" +
"import javax.tools.*;\n" +
"@SupportedAnnotationTypes(\"*\")\n" +
"public final class AP extends AnnotationProcessing.GeneratingAP {\n" +
"\n" +
" int round;\n" +
"\n" +
" @Override\n" +
" public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
" if (round++ != 0)\n" +
" return false;\n" +
" Filer filer = processingEnv.getFiler();\n" +
" TypeElement jlObject = processingEnv.getElementUtils().getTypeElement(\"java.lang.Object\");\n" +
code + ";\n" +
" return false;\n" +
" }\n" +
" }\n";
"""
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
import javax.tools.*;
@SupportedAnnotationTypes("*")
public final class AP extends AnnotationProcessing.GeneratingAP {
int round;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (round++ != 0)
return false;
Filer filer = processingEnv.getFiler();
TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
""" + code + """
;
return false;
}
}
""";
new JavacTask(tb)
.options("-classpath", System.getProperty("test.class.path"))
.sources(processorCode)
Expand Down
Expand Up @@ -46,72 +46,76 @@ public static void main(String... args) throws Exception {
}

private static final String annotationProcessorModule1 =
"module anno_proc1x {\n" +
" requires java.compiler;\n" +
"\n" +
" provides javax.annotation.processing.Processor\n" +
" with mypkg1.MyProcessor1;\n" +
"}";
"""
module anno_proc1x {
requires java.compiler;
provides javax.annotation.processing.Processor
with mypkg1.MyProcessor1;
}""";

private static final String annotationProcessorModule2 =
"module anno_proc2x {\n" +
" requires java.compiler;\n" +
"\n" +
" provides javax.annotation.processing.Processor\n" +
" with mypkg2.MyProcessor2;\n" +
"}";
"""
module anno_proc2x {
requires java.compiler;
provides javax.annotation.processing.Processor
with mypkg2.MyProcessor2;
}""";

private static final String annotationProcessor1 =
"package mypkg1;\n" +
"\n" +
"import javax.annotation.processing.AbstractProcessor;\n" +
"import javax.annotation.processing.RoundEnvironment;\n" +
"import javax.annotation.processing.SupportedAnnotationTypes;\n" +
"import javax.lang.model.SourceVersion;\n" +
"import javax.lang.model.element.*;\n" +
"\n" +
"import java.util.*;\n" +
"\n" +
"@SupportedAnnotationTypes(\"*\")\n" +
"public final class MyProcessor1 extends AbstractProcessor {\n" +
"\n" +
" @Override\n" +
" public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
" return false;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public SourceVersion getSupportedSourceVersion() {\n" +
" System.out.println(\"the annotation processor 1 is working!\");\n" +
" return SourceVersion.latest();\n" +
" }\n" +
"}";
"""
package mypkg1;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import java.util.*;
@SupportedAnnotationTypes("*")
public final class MyProcessor1 extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
System.out.println("the annotation processor 1 is working!");
return SourceVersion.latest();
}
}""";

private static final String annotationProcessor2 =
"package mypkg2;\n" +
"\n" +
"import javax.annotation.processing.AbstractProcessor;\n" +
"import javax.annotation.processing.RoundEnvironment;\n" +
"import javax.annotation.processing.SupportedAnnotationTypes;\n" +
"import javax.lang.model.SourceVersion;\n" +
"import javax.lang.model.element.*;\n" +
"\n" +
"import java.util.*;\n" +
"\n" +
"@SupportedAnnotationTypes(\"*\")\n" +
"public final class MyProcessor2 extends AbstractProcessor {\n" +
"\n" +
" @Override\n" +
" public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" +
" return false;\n" +
" }\n" +
"\n" +
" @Override\n" +
" public SourceVersion getSupportedSourceVersion() {\n" +
" System.out.println(\"the annotation processor 2 is working!\");\n" +
" return SourceVersion.latest();\n" +
" }\n" +
"}";
"""
package mypkg2;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import java.util.*;
@SupportedAnnotationTypes("*")
public final class MyProcessor2 extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
System.out.println("the annotation processor 2 is working!");
return SourceVersion.latest();
}
}""";

private static final String testClass = "class Test{}";

Expand Down
20 changes: 11 additions & 9 deletions test/langtools/tools/javac/modules/AnnotationsOnModules.java
Expand Up @@ -270,11 +270,12 @@ public void testExportsAndOpensToDeprecatedModule(Path base) throws Exception {
Path m1 = base.resolve("src1/A");

tb.writeJavaFiles(m1,
"module A { " +
"exports p1 to B; opens p1 to B;" +
"exports p2 to C; opens p2 to C;" +
"exports p3 to B,C; opens p3 to B,C;" +
"}",
"""
module A {
exports p1 to B; opens p1 to B;
exports p2 to C; opens p2 to C;
exports p3 to B,C; opens p3 to B,C;
}""",
"package p1; public class A { }",
"package p2; public class A { }",
"package p3; public class A { }");
Expand Down Expand Up @@ -547,10 +548,11 @@ public void testModuleDeprecation(Path base) throws Exception {
String DEPRECATED_JAVADOC = "/** @deprecated */";
for (String suppress : new String[] {"", DEPRECATED_JAVADOC, "@Deprecated ", "@SuppressWarnings(\"deprecation\") "}) {
tb.writeJavaFiles(m3,
suppress + "module m3x {\n" +
" requires m1x;\n" +
" exports api to m1x, m2x;\n" +
"}",
suppress + """
module m3x {
requires m1x;
exports api to m1x, m2x;
}""",
"package api; public class Api { }");
System.err.println("compile m3x");
actual = new JavacTask(tb)
Expand Down
31 changes: 17 additions & 14 deletions test/langtools/tools/javac/modules/AutomaticModules.java
Expand Up @@ -461,10 +461,11 @@ public void testLintRequireAutomatic(Path base) throws Exception {
Path src = base.resolve("src");

tb.writeJavaFiles(src,
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
"""
module m1x {
requires transitive automaticA;
requires automaticB;
}""");

Path classes = base.resolve("classes");

Expand Down Expand Up @@ -550,11 +551,12 @@ public void testLintRequireAutomatic(Path base) throws Exception {
.getOutputLines(Task.OutputKind.DIRECT);

tb.writeJavaFiles(src,
"@SuppressWarnings(\"requires-transitive-automatic\")\n" +
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
"""
@SuppressWarnings("requires-transitive-automatic")
module m1x {
requires transitive automaticA;
requires automaticB;
}""");

new JavacTask(tb)
.options("--source-path", src.toString(),
Expand Down Expand Up @@ -590,11 +592,12 @@ public void testLintRequireAutomatic(Path base) throws Exception {
}

tb.writeJavaFiles(src,
"@SuppressWarnings(\"requires-automatic\")\n" +
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
"""
@SuppressWarnings("requires-automatic")
module m1x {
requires transitive automaticA;
requires automaticB;
}""");

log = new JavacTask(tb)
.options("--source-path", src.toString(),
Expand Down
Expand Up @@ -577,13 +577,15 @@ public void testMissingKnownClass(Path base) throws Exception {
Path src_m2 = src.resolve("m2x");
tb.writeJavaFiles(src_m2,
"module m2x { requires m1x; }",
"package test;\n" +
"import api.Sub;\n" +
"import api.Base;\n" +
"public class Test {\n" +
" Sub a2;\n" +
" Base a;\n" +
"}\n");
"""
package test;
import api.Sub;
import api.Base;
public class Test {
Sub a2;
Base a;
}
""");
Path m2xClasses = classes.resolve("m2x");
tb.createDirectories(m2xClasses);
List<String> log = new JavacTask(tb)
Expand Down

0 comments on commit 064110a

Please sign in to comment.