本文整理匯總了Java中javax.tools.DiagnosticCollector類的典型用法代碼示例。如果您正苦於以下問題:Java DiagnosticCollector類的具體用法?Java DiagnosticCollector怎麽用?Java DiagnosticCollector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DiagnosticCollector類屬於javax.tools包,在下文中一共展示了DiagnosticCollector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: compileClass
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
/**
* Compile the provided class. The className may have a package separated by /. For example:
* my/package/myclass
*
* @param className Name of the class to compile.
* @param classCode Plain text contents of the class
* @return The byte contents of the compiled class.
* @throws IOException
*/
public byte[] compileClass(final String className, final String classCode) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
OutputStreamJavaFileManager<JavaFileManager> fileManager =
new OutputStreamJavaFileManager<JavaFileManager>(
javaCompiler.getStandardFileManager(null, null, null), byteArrayOutputStream);
List<JavaFileObject> fileObjects = new ArrayList<JavaFileObject>();
fileObjects.add(new JavaSourceFromString(className, classCode));
List<String> options = Arrays.asList("-classpath", this.classPath);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
if (!javaCompiler.getTask(null, fileManager, diagnostics, options, null, fileObjects).call()) {
StringBuilder errorMsg = new StringBuilder();
for (Diagnostic d : diagnostics.getDiagnostics()) {
String err = String.format("Compilation error: Line %d - %s%n", d.getLineNumber(),
d.getMessage(null));
errorMsg.append(err);
System.err.print(err);
}
throw new IOException(errorMsg.toString());
}
return byteArrayOutputStream.toByteArray();
}
示例2: compile
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
public synchronized Class compile(final String className, final CharSequence javaSource,
final DiagnosticCollector<JavaFileObject> diagnosticsList)
throws JdkCompileException,
ClassCastException {
if (diagnosticsList != null) {
diagnostics = diagnosticsList;
} else {
diagnostics = new DiagnosticCollector<JavaFileObject>();
}
Map<String, CharSequence> classes = new HashMap<String, CharSequence>(1);
classes.put(className, javaSource);
Map<String, Class> compiled = compile(classes, diagnosticsList);
Class newClass = compiled.get(className);
return newClass;
}
示例3: compile
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
public static boolean compile(String[] args, File episode) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
JavacOptions options = JavacOptions.parse(compiler, fileManager, args);
List<String> unrecognizedOptions = options.getUnrecognizedOptions();
if (!unrecognizedOptions.isEmpty())
Logger.getLogger(SchemaGenerator.class.getName()).log(Level.WARNING, "Unrecognized options found: {0}", unrecognizedOptions);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(options.getFiles());
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
options.getRecognizedOptions(),
options.getClassNames(),
compilationUnits);
com.sun.tools.internal.jxc.ap.SchemaGenerator r = new com.sun.tools.internal.jxc.ap.SchemaGenerator();
if (episode != null)
r.setEpisodeFile(episode);
task.setProcessors(Collections.singleton(r));
return task.call();
}
示例4: validateExpression
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
/**
* This method validates the given expression and updates the expression-editor's datasturcture accordingly
*
* @param expressionText
* @param inputFields
* @param expressionEditorData
*/
public static void validateExpression(String expressionText,Map<String, Class<?>> inputFields,ExpressionEditorData expressionEditorData ) {
if(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject()!=null){
DiagnosticCollector<JavaFileObject> diagnosticCollector = null;
try {
inputFields.putAll(expressionEditorData.getExtraFieldDatatypeMap());
diagnosticCollector = ValidateExpressionToolButton
.compileExpresion(expressionText,inputFields,expressionEditorData.getComponentName());
if (diagnosticCollector != null && !diagnosticCollector.getDiagnostics().isEmpty()) {
for (Diagnostic<?> diagnostic : diagnosticCollector.getDiagnostics()) {
if (StringUtils.equals(diagnostic.getKind().name(), Diagnostic.Kind.ERROR.name())) {
expressionEditorData.setValid(false);
return;
}
}
}
} catch (JavaModelException | InvocationTargetException | ClassNotFoundException | MalformedURLException
| IllegalAccessException | IllegalArgumentException e) {
expressionEditorData.setValid(false);
return;
}
expressionEditorData.setValid(true);
}
}
示例5: run
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
private void run() {
File classToCheck = new File(System.getProperty("test.classes"),
getClass().getSimpleName() + ".class");
DiagnosticCollector<JavaFileObject> dc =
new DiagnosticCollector<JavaFileObject>();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, null,
Arrays.asList(classToCheck.getPath()));
if (t.run() != 0)
throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> d: diags) {
if (d.getKind() == Diagnostic.Kind.ERROR)
throw new AssertionError(d.getMessage(Locale.ENGLISH));
}
String lineSep = System.getProperty("line.separator");
String out = sw.toString().replace(lineSep, "\n");
if (!out.equals(expOutput)) {
throw new AssertionError("The output is not equal to the one expected");
}
}
示例6: javap
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
private String javap(List<String> args, List<String> classes) {
DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, args, classes);
if (t.run() != 0)
throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> d: diags) {
if (d.getKind() == Diagnostic.Kind.ERROR)
throw new Error(d.getMessage(Locale.ENGLISH));
}
return sw.toString();
}
示例7: parse
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
static List<? extends Tree> parse(String srcfile) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(srcfile);
String classPath = System.getProperty("java.class.path");
List<String> options = Arrays.asList("-classpath", classPath);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
Context context = new Context();
JavacTaskImpl task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, null,
diagnostics, options, null, fileObjects, context);
TrialParserFactory.instance(context);
Iterable<? extends CompilationUnitTree> asts = task.parse();
Iterator<? extends CompilationUnitTree> it = asts.iterator();
if (it.hasNext()) {
CompilationUnitTree cut = it.next();
return cut.getTypeDecls();
} else {
throw new AssertionError("Expected compilation unit");
}
}
示例8: testVariableInIfThen1
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
@Test
void testVariableInIfThen1() throws IOException {
String code = "package t; class Test { " +
"private static void t(String name) { " +
"if (name != null) String nn = name.trim(); } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen1",
Arrays.<String>asList("compiler.err.variable.not.allowed"),
codes);
}
示例9: testVariableInIfThen2
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
@Test
void testVariableInIfThen2() throws IOException {
String code = "package t; class Test { " +
"private static void t(String name) { " +
"if (name != null) class X {} } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen2",
Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
}
示例10: testVariableInIfThen3
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
@Test
void testVariableInIfThen3() throws IOException {
String code = "package t; class Test { "+
"private static void t() { " +
"if (true) abstract class F {} }}";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen3",
Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
}
示例11: testVariableInIfThen4
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
@Test
void testVariableInIfThen4() throws IOException {
String code = "package t; class Test { "+
"private static void t(String name) { " +
"if (name != null) interface X {} } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen4",
Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
}
示例12: testVariableInIfThen5
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
@Test
void testVariableInIfThen5() throws IOException {
String code = "package t; class Test { "+
"private static void t() { " +
"if (true) } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen5",
Arrays.<String>asList("compiler.err.illegal.start.of.stmt"),
codes);
}
示例13: compileWithJSR199
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
void compileWithJSR199() throws IOException {
String cpath = "C2.jar";
File clientJarFile = new File(cpath);
File sourceFileToCompile = new File("C3.java");
javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
try (StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null)) {
List<File> files = new ArrayList<>();
files.add(clientJarFile);
stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
throw new AssertionError("compilation failed");
}
}
}
示例14: compile
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
private <S> InMemoryFileManager compile(
List<String> options,
Function<S, ? extends JavaFileObject> src2JavaFileObject,
List<S> sources)
throws IOException, CompilationException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
List<? extends JavaFileObject> src = sources.stream()
.map(src2JavaFileObject)
.collect(Collectors.toList());
DiagnosticCollector<? super JavaFileObject> dc = new DiagnosticCollector<>();
try (InMemoryFileManager fileManager
= new InMemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, dc, options, null, src);
boolean success = task.call();
if (!success) {
String errorMessage = dc.getDiagnostics().stream()
.map(Object::toString)
.collect(Collectors.joining("\n"));
throw new CompilationException("Compilation Error\n\n" + errorMessage);
}
return fileManager;
}
}
示例15: getCompileResult
import javax.tools.DiagnosticCollector; //導入依賴的package包/類
private boolean getCompileResult(String contents, String className,
boolean shouldCompile) throws Exception{
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
boolean ok = compileCode(className, contents, diagnostics);
String expectedErrKey = "compiler.err.invalid.repeatable" +
".annotation.retention";
if (!shouldCompile && !ok) {
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
if (!((d.getKind() == Diagnostic.Kind.ERROR) &&
d.getCode().contains(expectedErrKey))) {
error("FAIL: Incorrect error given, expected = "
+ expectedErrKey + ", Actual = " + d.getCode()
+ " for className = " + className, contents);
}
}
}
return (shouldCompile == ok);
}