mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
8062676: Tests which leak lots of file managers should be fixed (group 2)
Reviewed-by: darcy
This commit is contained in:
parent
b96daffd2e
commit
a75d2dbd39
10 changed files with 286 additions and 218 deletions
|
@ -412,6 +412,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
|
||||||
EXIT_ABNORMAL = 4; // Compiler terminated abnormally
|
EXIT_ABNORMAL = 4; // Compiler terminated abnormally
|
||||||
|
|
||||||
int run(String[] args) {
|
int run(String[] args) {
|
||||||
|
try {
|
||||||
try {
|
try {
|
||||||
handleOptions(args);
|
handleOptions(args);
|
||||||
|
|
||||||
|
@ -423,7 +424,6 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
|
||||||
return EXIT_CMDERR;
|
return EXIT_CMDERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
return run();
|
return run();
|
||||||
} finally {
|
} finally {
|
||||||
if (defaultFileManager != null) {
|
if (defaultFileManager != null) {
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
package com.sun.tools.sjavac.comp;
|
package com.sun.tools.sjavac.comp;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
@ -76,7 +77,7 @@ public class SjavacImpl implements Sjavac {
|
||||||
Set<URI> sourcesToCompile,
|
Set<URI> sourcesToCompile,
|
||||||
Set<URI> visibleSources) {
|
Set<URI> visibleSources) {
|
||||||
JavacTool compiler = JavacTool.create();
|
JavacTool compiler = JavacTool.create();
|
||||||
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
||||||
SmartFileManager smartFileManager = new SmartFileManager(fileManager);
|
SmartFileManager smartFileManager = new SmartFileManager(fileManager);
|
||||||
Context context = new Context();
|
Context context = new Context();
|
||||||
|
|
||||||
|
@ -160,6 +161,9 @@ public class SjavacImpl implements Sjavac {
|
||||||
compilationResult.returnCode = rc.exitCode;
|
compilationResult.returnCode = rc.exitCode;
|
||||||
|
|
||||||
return compilationResult;
|
return compilationResult;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -245,7 +245,7 @@ public abstract class JavacTemplateTestBase {
|
||||||
|
|
||||||
private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
|
private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
|
||||||
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
|
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
|
||||||
StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null);
|
try (StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null)) {
|
||||||
if (classpaths.size() > 0)
|
if (classpaths.size() > 0)
|
||||||
fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
|
fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
|
||||||
JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
|
JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
|
||||||
|
@ -262,6 +262,7 @@ public abstract class JavacTemplateTestBase {
|
||||||
return nullDir;
|
return nullDir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Load the given class using the provided list of class paths */
|
/** Load the given class using the provided list of class paths */
|
||||||
protected Class<?> loadClass(String className, File... destDirs) {
|
protected Class<?> loadClass(String className, File... destDirs) {
|
||||||
|
|
|
@ -44,6 +44,7 @@ import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.DiagnosticListener;
|
import javax.tools.DiagnosticListener;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.SimpleJavaFileObject;
|
import javax.tools.SimpleJavaFileObject;
|
||||||
|
|
||||||
|
@ -52,7 +53,8 @@ public class VerifyErroneousAnnotationsAttributed {
|
||||||
new VerifyErroneousAnnotationsAttributed().run();
|
new VerifyErroneousAnnotationsAttributed().run();
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() {
|
void run() throws IOException {
|
||||||
|
try {
|
||||||
int failCount = 0;
|
int failCount = 0;
|
||||||
for (String ann : generateAnnotations()) {
|
for (String ann : generateAnnotations()) {
|
||||||
String code = PATTERN.replace("PLACEHOLDER", ann);
|
String code = PATTERN.replace("PLACEHOLDER", ann);
|
||||||
|
@ -69,6 +71,9 @@ public class VerifyErroneousAnnotationsAttributed {
|
||||||
if (failCount > 0) {
|
if (failCount > 0) {
|
||||||
throw new IllegalStateException("failed sub-tests: " + failCount);
|
throw new IllegalStateException("failed sub-tests: " + failCount);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
fm.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> generateAnnotations() {
|
List<String> generateAnnotations() {
|
||||||
|
@ -221,13 +226,14 @@ public class VerifyErroneousAnnotationsAttributed {
|
||||||
}
|
}
|
||||||
|
|
||||||
final JavacTool tool = JavacTool.create();
|
final JavacTool tool = JavacTool.create();
|
||||||
|
final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
|
||||||
final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
|
final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
|
||||||
@Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
|
@Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
void validate(String code) throws IOException, URISyntaxException {
|
void validate(String code) throws IOException, URISyntaxException {
|
||||||
JavacTask task = tool.getTask(null,
|
JavacTask task = tool.getTask(null,
|
||||||
null,
|
fm,
|
||||||
devNull,
|
devNull,
|
||||||
Arrays.asList("-XDshouldStopPolicy=FLOW"),
|
Arrays.asList("-XDshouldStopPolicy=FLOW"),
|
||||||
null,
|
null,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -34,19 +34,20 @@ import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.annotation.processing.AbstractProcessor;
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
import javax.annotation.processing.RoundEnvironment;
|
import javax.annotation.processing.RoundEnvironment;
|
||||||
|
import javax.annotation.processing.SupportedSourceVersion;
|
||||||
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.SimpleJavaFileObject;
|
import javax.tools.SimpleJavaFileObject;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
import com.sun.source.util.JavacTask;
|
import com.sun.source.util.JavacTask;
|
||||||
import javax.annotation.processing.SupportedSourceVersion;
|
|
||||||
import javax.lang.model.SourceVersion;
|
|
||||||
|
|
||||||
public class TestContainTypes {
|
public class TestContainTypes {
|
||||||
|
|
||||||
|
@ -127,7 +128,11 @@ public class TestContainTypes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
||||||
|
static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
|
||||||
|
|
||||||
public static void main(String... args) throws Exception {
|
public static void main(String... args) throws Exception {
|
||||||
|
try {
|
||||||
for (ClassType ctA : ClassType.values()) {
|
for (ClassType ctA : ClassType.values()) {
|
||||||
for (ParameterType ptA : ParameterType.values()) {
|
for (ParameterType ptA : ParameterType.values()) {
|
||||||
for (ClassType ctB : ClassType.values()) {
|
for (ClassType ctB : ClassType.values()) {
|
||||||
|
@ -137,12 +142,14 @@ public class TestContainTypes {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
fm.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
|
static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
|
||||||
JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
|
JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
|
||||||
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
JavacTask ct = (JavacTask)tool.getTask(null, fm, null,
|
||||||
JavacTask ct = (JavacTask)tool.getTask(null, null, null,
|
|
||||||
null, null, Arrays.asList(source));
|
null, null, Arrays.asList(source));
|
||||||
ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
|
ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
|
||||||
System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
|
System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
|
||||||
|
|
|
@ -100,6 +100,7 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
|
||||||
JavacTool tool = JavacTool.create();
|
JavacTool tool = JavacTool.create();
|
||||||
|
|
||||||
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
|
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
|
||||||
|
try {
|
||||||
if (fmOpts != null)
|
if (fmOpts != null)
|
||||||
fm = new FileManager(fm, fmOpts);
|
fm = new FileManager(fm, fmOpts);
|
||||||
|
|
||||||
|
@ -108,6 +109,9 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
|
||||||
Context c = initContext();
|
Context c = initContext();
|
||||||
JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
|
JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
|
||||||
return t.call();
|
return t.call();
|
||||||
|
} finally {
|
||||||
|
close(fm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,9 +140,14 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
|
||||||
Main main = new Main("javac", out);
|
Main main = new Main("javac", out);
|
||||||
Context c = initContext();
|
Context c = initContext();
|
||||||
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
||||||
|
|
||||||
|
try {
|
||||||
Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
|
Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
|
||||||
|
|
||||||
return result.isOK();
|
return result.isOK();
|
||||||
|
} finally {
|
||||||
|
close(c.get(JavaFileManager.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,10 +169,15 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
|
||||||
|
|
||||||
Context c = initContext();
|
Context c = initContext();
|
||||||
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
||||||
|
|
||||||
|
try {
|
||||||
Main m = new Main("javac", out);
|
Main m = new Main("javac", out);
|
||||||
Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
|
Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
|
||||||
|
|
||||||
return result.isOK();
|
return result.isOK();
|
||||||
|
} finally {
|
||||||
|
close(c.get(JavaFileManager.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -31,6 +31,7 @@ import javax.tools.Diagnostic;
|
||||||
import javax.tools.DiagnosticCollector;
|
import javax.tools.DiagnosticCollector;
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
import javax.tools.JavaCompiler.CompilationTask;
|
import javax.tools.JavaCompiler.CompilationTask;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.StandardJavaFileManager;
|
import javax.tools.StandardJavaFileManager;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
@ -351,6 +352,14 @@ class Example implements Comparable<Example> {
|
||||||
loader = cl;
|
loader = cl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void close(JavaFileManager fm) {
|
||||||
|
try {
|
||||||
|
fm.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected ClassLoader loader;
|
protected ClassLoader loader;
|
||||||
protected boolean verbose;
|
protected boolean verbose;
|
||||||
}
|
}
|
||||||
|
@ -399,6 +408,7 @@ class Example implements Comparable<Example> {
|
||||||
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
|
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
|
||||||
|
|
||||||
StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
|
StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
|
||||||
|
try {
|
||||||
if (fmOpts != null)
|
if (fmOpts != null)
|
||||||
fm = new FileManager(fm, fmOpts);
|
fm = new FileManager(fm, fmOpts);
|
||||||
|
|
||||||
|
@ -414,6 +424,9 @@ class Example implements Comparable<Example> {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
|
} finally {
|
||||||
|
close(fm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -526,6 +539,8 @@ class Example implements Comparable<Example> {
|
||||||
Context c = new Context();
|
Context c = new Context();
|
||||||
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
JavacFileManager.preRegister(c); // can't create it until Log has been set up
|
||||||
MessageTracker.preRegister(c, keys);
|
MessageTracker.preRegister(c, keys);
|
||||||
|
|
||||||
|
try {
|
||||||
Main m = new Main("javac", pw);
|
Main m = new Main("javac", pw);
|
||||||
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
|
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
|
||||||
|
|
||||||
|
@ -534,6 +549,9 @@ class Example implements Comparable<Example> {
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc.isOK();
|
return rc.isOK();
|
||||||
|
} finally {
|
||||||
|
close(c.get(JavaFileManager.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class MessageTracker extends JavacMessages {
|
static class MessageTracker extends JavacMessages {
|
||||||
|
|
|
@ -35,6 +35,7 @@ import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.SimpleJavaFileObject;
|
import javax.tools.SimpleJavaFileObject;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
@ -206,9 +207,8 @@ public class SamConversionComboTest {
|
||||||
String clientFileStr = clientSourceFile.toString();
|
String clientFileStr = clientSourceFile.toString();
|
||||||
System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
|
System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
|
||||||
|
|
||||||
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
|
||||||
DiagnosticChecker dc = new DiagnosticChecker();
|
DiagnosticChecker dc = new DiagnosticChecker();
|
||||||
JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
|
JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
|
||||||
try {
|
try {
|
||||||
ct.analyze();
|
ct.analyze();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -255,6 +255,9 @@ public class SamConversionComboTest {
|
||||||
ReturnValue returnValue;
|
ReturnValue returnValue;
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
|
|
||||||
|
static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
|
||||||
|
static JavaFileManager fm = comp.getStandardFileManager(null, null, null);
|
||||||
|
|
||||||
SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
|
SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
|
||||||
fInterface = f;
|
fInterface = f;
|
||||||
context = c;
|
context = c;
|
||||||
|
@ -264,6 +267,7 @@ public class SamConversionComboTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
try {
|
||||||
for(Context ct : Context.values()) {
|
for(Context ct : Context.values()) {
|
||||||
for (FInterface fi : FInterface.values()) {
|
for (FInterface fi : FInterface.values()) {
|
||||||
for (LambdaKind lk: LambdaKind.values()) {
|
for (LambdaKind lk: LambdaKind.values()) {
|
||||||
|
@ -276,5 +280,8 @@ public class SamConversionComboTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("total tests: " + count);
|
System.out.println("total tests: " + count);
|
||||||
|
} finally {
|
||||||
|
fm.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -67,17 +67,23 @@ import javax.tools.Diagnostic;
|
||||||
import javax.tools.DiagnosticCollector;
|
import javax.tools.DiagnosticCollector;
|
||||||
import javax.tools.DiagnosticListener;
|
import javax.tools.DiagnosticListener;
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.SimpleJavaFileObject;
|
import javax.tools.SimpleJavaFileObject;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
public class JavacParserTest extends TestCase {
|
public class JavacParserTest extends TestCase {
|
||||||
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
||||||
|
static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
|
||||||
|
|
||||||
private JavacParserTest(){}
|
private JavacParserTest(){}
|
||||||
|
|
||||||
public static void main(String... args) throws Exception {
|
public static void main(String... args) throws Exception {
|
||||||
|
try {
|
||||||
new JavacParserTest().run(args);
|
new JavacParserTest().run(args);
|
||||||
|
} finally {
|
||||||
|
fm.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyFileObject extends SimpleJavaFileObject {
|
class MyFileObject extends SimpleJavaFileObject {
|
||||||
|
@ -103,7 +109,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
CompilationUnitTree getCompilationUnitTree(String code) throws IOException {
|
CompilationUnitTree getCompilationUnitTree(String code) throws IOException {
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
return cut;
|
return cut;
|
||||||
|
@ -129,7 +135,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
String code = "package test; public class Test {public Test() {super();}}";
|
String code = "package test; public class Test {public Test() {super();}}";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
||||||
|
@ -168,7 +174,7 @@ public class JavacParserTest extends TestCase {
|
||||||
final String theString = "public";
|
final String theString = "public";
|
||||||
String code = "package test; " + theString + " enum Test {A;}";
|
String code = "package test; " + theString + " enum Test {A;}";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
||||||
|
@ -191,7 +197,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"class d {} private void method() { " +
|
"class d {} private void method() { " +
|
||||||
"Object o = " + theString + "; } }";
|
"Object o = " + theString + "; } }";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
SourcePositions pos = Trees.instance(ct).getSourcePositions();
|
||||||
|
@ -238,7 +244,7 @@ public class JavacParserTest extends TestCase {
|
||||||
final List<Diagnostic<? extends JavaFileObject>> errors =
|
final List<Diagnostic<? extends JavaFileObject>> errors =
|
||||||
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
|
||||||
new DiagnosticListener<JavaFileObject>() {
|
new DiagnosticListener<JavaFileObject>() {
|
||||||
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||||
errors.add(diagnostic);
|
errors.add(diagnostic);
|
||||||
|
@ -261,7 +267,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
String code = "\n@interface Test {}";
|
String code = "\n@interface Test {}";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
|
@ -300,7 +306,7 @@ public class JavacParserTest extends TestCase {
|
||||||
final List<Diagnostic<? extends JavaFileObject>> errors =
|
final List<Diagnostic<? extends JavaFileObject>> errors =
|
||||||
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
|
||||||
new DiagnosticListener<JavaFileObject>() {
|
new DiagnosticListener<JavaFileObject>() {
|
||||||
|
|
||||||
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||||
|
@ -443,7 +449,7 @@ public class JavacParserTest extends TestCase {
|
||||||
final List<Diagnostic<? extends JavaFileObject>> errors =
|
final List<Diagnostic<? extends JavaFileObject>> errors =
|
||||||
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
new LinkedList<Diagnostic<? extends JavaFileObject>>();
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
|
||||||
new DiagnosticListener<JavaFileObject>() {
|
new DiagnosticListener<JavaFileObject>() {
|
||||||
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||||
errors.add(diagnostic);
|
errors.add(diagnostic);
|
||||||
|
@ -482,7 +488,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
String code = "package t; class Test { <T> void t() {} }";
|
String code = "package t; class Test { <T> void t() {} }";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
|
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
|
||||||
|
@ -505,7 +511,7 @@ public class JavacParserTest extends TestCase {
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<JavaFileObject>();
|
new DiagnosticCollector<JavaFileObject>();
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
ct.parse();
|
ct.parse();
|
||||||
|
@ -529,7 +535,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"if (name != null) class X {} } }";
|
"if (name != null) class X {} } }";
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<JavaFileObject>();
|
new DiagnosticCollector<JavaFileObject>();
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
ct.parse();
|
ct.parse();
|
||||||
|
@ -552,7 +558,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"if (true) abstract class F {} }}";
|
"if (true) abstract class F {} }}";
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<JavaFileObject>();
|
new DiagnosticCollector<JavaFileObject>();
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
ct.parse();
|
ct.parse();
|
||||||
|
@ -575,7 +581,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"if (name != null) interface X {} } }";
|
"if (name != null) interface X {} } }";
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<JavaFileObject>();
|
new DiagnosticCollector<JavaFileObject>();
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
ct.parse();
|
ct.parse();
|
||||||
|
@ -598,7 +604,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"if (true) } }";
|
"if (true) } }";
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<JavaFileObject>();
|
new DiagnosticCollector<JavaFileObject>();
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
ct.parse();
|
ct.parse();
|
||||||
|
@ -620,7 +626,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
String code = "\nclass Test { { System.err.println(0e); } }";
|
String code = "\nclass Test { { System.err.println(0e); } }";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
assertNotNull(ct.parse().iterator().next());
|
assertNotNull(ct.parse().iterator().next());
|
||||||
|
@ -820,7 +826,7 @@ public class JavacParserTest extends TestCase {
|
||||||
+ " };\n"
|
+ " };\n"
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}";
|
+ "}";
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
|
||||||
null, null, Arrays.asList(new MyFileObject(code)));
|
null, null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
|
|
||||||
|
@ -861,7 +867,7 @@ public class JavacParserTest extends TestCase {
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}";
|
+ "}";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
|
||||||
null, null, Arrays.asList(new MyFileObject(code)));
|
null, null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
|
|
||||||
|
@ -886,7 +892,7 @@ public class JavacParserTest extends TestCase {
|
||||||
|
|
||||||
String code = "package t; enum Test { AAA; }";
|
String code = "package t; enum Test { AAA; }";
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
|
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
|
||||||
|
@ -905,7 +911,7 @@ public class JavacParserTest extends TestCase {
|
||||||
"}";
|
"}";
|
||||||
DiagnosticCollector<JavaFileObject> coll =
|
DiagnosticCollector<JavaFileObject> coll =
|
||||||
new DiagnosticCollector<>();
|
new DiagnosticCollector<>();
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
|
||||||
null, Arrays.asList(new MyFileObject(code)));
|
null, Arrays.asList(new MyFileObject(code)));
|
||||||
|
|
||||||
CompilationUnitTree cut = ct.parse().iterator().next();
|
CompilationUnitTree cut = ct.parse().iterator().next();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -40,6 +40,7 @@ import java.util.List;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
import javax.tools.JavaCompiler;
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaFileManager;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
import javax.tools.SimpleJavaFileObject;
|
import javax.tools.SimpleJavaFileObject;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
|
@ -114,7 +115,8 @@ public class TypeAnnotationsPretty {
|
||||||
code + "; }" +
|
code + "; }" +
|
||||||
postfix;
|
postfix;
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
|
||||||
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(src)));
|
null, Arrays.asList(new MyFileObject(src)));
|
||||||
|
|
||||||
for (CompilationUnitTree cut : ct.parse()) {
|
for (CompilationUnitTree cut : ct.parse()) {
|
||||||
|
@ -123,13 +125,15 @@ public class TypeAnnotationsPretty {
|
||||||
checkMatch(code, var);
|
checkMatch(code, var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void runMethod(String code) throws IOException {
|
private void runMethod(String code) throws IOException {
|
||||||
String src = prefix +
|
String src = prefix +
|
||||||
code + "}" +
|
code + "}" +
|
||||||
postfix;
|
postfix;
|
||||||
|
|
||||||
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
|
||||||
|
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
|
||||||
null, Arrays.asList(new MyFileObject(src)));
|
null, Arrays.asList(new MyFileObject(src)));
|
||||||
|
|
||||||
|
|
||||||
|
@ -139,6 +143,7 @@ public class TypeAnnotationsPretty {
|
||||||
checkMatch(code, meth);
|
checkMatch(code, meth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void checkMatch(String code, JCTree tree) {
|
void checkMatch(String code, JCTree tree) {
|
||||||
String expect = code.replace("\n", NL);
|
String expect = code.replace("\n", NL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue