Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8198317: Enhance JavacTool.getTask for flexibility #1896

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -33,15 +33,23 @@
* @run main T8198317
*/

import java.io.*;
import java.lang.reflect.*;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URI;
import java.util.List;
import java.util.Arrays;
import javax.tools.ToolProvider;
import javax.tools.SimpleJavaFileObject;
import javax.tools.JavaFileObject;

import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.util.Log;

import toolbox.ToolBox;
import toolbox.TestRunner;
import static toolbox.ToolBox.lineSeparator;

public class T8198317 extends TestRunner{
ToolBox tb;
@@ -58,17 +66,61 @@ public static void main(String[] args) throws Exception {

@Test
public void testLogSettingInJavacTool() throws Exception {
// TODO Situation: out is null and the value is not set in the context.
// TODO Situation: out is not null and out is not a PrintWriter.
String code = """
import java.io.Serializable;
class Test implements Serializable {
public static final int serialVersionUID = 1;
}""";

List<String> expected = Arrays.asList(
"Test.java:3:29: compiler.warn.long.SVUID: Test",
"1 warning");

List<? extends JavaFileObject> files = Arrays.asList(new MemFile("Test.java", code));

// Situation: out is null and the value is not set in the context.
ByteArrayOutputStream bais = new ByteArrayOutputStream();

This comment has been minimized.

@jonathan-gibbons

jonathan-gibbons Dec 29, 2020
Contributor

The variable name bais is "surprising". Is it a cut-n-paste from elsewhere? I was expecting to see baos as an acronym for "byte array output stream", bais suggests "byte array input stream".

This comment has been minimized.

@lgxbslgx

lgxbslgx Dec 29, 2020
Author Contributor

Fixed. I used ByteArrayInputStream bais = new ByteArrayInputStream(); at the beginning. Then I identified the ByteArrayInputStream is not right and changed to use ByteArrayOutputStream. But I forgot to revise the variable name bais. Sorry for wasting the time at this careless detail.

PrintStream printStream = new PrintStream(bais);
PrintStream prev = System.err;
System.setErr(printStream);
ToolProvider.getSystemJavaCompiler()
.getTask(null, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files)
.call();
tb.checkEqual(expected, Arrays.asList(bais.toString().split(lineSeparator)));
System.setErr(prev);
Comment on lines +86 to +90

This comment has been minimized.

@jonathan-gibbons

jonathan-gibbons Dec 29, 2020
Contributor

Although not strictly necessary in this case, it is good practice to use try ... finally to reset values back to some previous value. In other words, the System.setErr(prev); should be in the finally clause.

This comment has been minimized.

@lgxbslgx

lgxbslgx Dec 29, 2020
Author Contributor

Fixed.


// Situation: out is not null and out is a PrintWriter.
PrintWriter expectedPW = new PrintWriter(System.out);
JavacTaskImpl task2 = (JavacTaskImpl) ToolProvider
.getSystemJavaCompiler()
.getTask(expectedPW, null, null, null, null, null);
PrintWriter writer2 = task2.getContext().get(Log.errKey);
if (!expectedPW.equals(writer2)) {
throw new Error("The PrintWriter is set uncorrectly.");
StringWriter stringWriter2 = new StringWriter();
PrintWriter expectedPW2 = new PrintWriter(stringWriter2);
ToolProvider.getSystemJavaCompiler()
.getTask(expectedPW2, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files)
.call();
tb.checkEqual(expected, Arrays.asList(stringWriter2.toString().split(lineSeparator)));

// Situation: out is not null and out is not a PrintWriter.
StringWriter stringWriter3 = new StringWriter();
ToolProvider.getSystemJavaCompiler()
.getTask(stringWriter3, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files)
.call();
tb.checkEqual(expected, Arrays.asList(stringWriter3.toString().split(lineSeparator)));
}

class MemFile extends SimpleJavaFileObject {
public final String text;

MemFile(String name, String text) {
super(URI.create(name), JavaFileObject.Kind.SOURCE);
this.text = text;
}

@Override
public String getName() {
return uri.toString();
}

@Override
public String getCharContent(boolean ignoreEncodingErrors) {
return text;
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.