本文整理汇总了Java中javax.tools.Diagnostic.getPosition方法的典型用法代码示例。如果您正苦于以下问题:Java Diagnostic.getPosition方法的具体用法?Java Diagnostic.getPosition怎么用?Java Diagnostic.getPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.tools.Diagnostic
的用法示例。
在下文中一共展示了Diagnostic.getPosition方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPrefferedPosition
import javax.tools.Diagnostic; //导入方法依赖的package包/类
public static long getPrefferedPosition(CompilationInfo info, Diagnostic d) throws IOException {
if ("compiler.err.doesnt.exist".equals(d.getCode()) || "compiler.err.try.with.resources.expr.needs.var".equals(d.getCode())) {
return d.getStartPosition();
}
if ("compiler.err.cant.resolve.location".equals(d.getCode()) || "compiler.err.cant.resolve.location.args".equals(d.getCode())) {
int[] span = findUnresolvedElementSpan(info, (int) d.getPosition());
if (span != null) {
return span[0];
} else {
return d.getPosition();
}
}
if ("compiler.err.not.stmt".equals(d.getCode())) {
//check for "Collections.":
TreePath path = findUnresolvedElement(info, (int) d.getStartPosition() - 1);
Element el = path != null ? info.getTrees().getElement(path) : null;
if (el == null || el.asType().getKind() == TypeKind.ERROR) {
return d.getStartPosition() - 1;
}
/*
if (el.asType().getKind() == TypeKind.PACKAGE) {
//check if the package does actually exist:
String s = ((PackageElement) el).getQualifiedName().toString();
if (info.getElements().getPackageElement(s) == null) {
//it does not:
return d.getStartPosition() - 1;
}
}
*/
return d.getStartPosition();
}
return d.getPosition();
}
示例2: diag
import javax.tools.Diagnostic; //导入方法依赖的package包/类
@Override
public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
return new Diag() {
@Override
public boolean isError() {
return d.getKind() == Diagnostic.Kind.ERROR;
}
@Override
public long getPosition() {
return d.getPosition();
}
@Override
public long getStartPosition() {
return d.getStartPosition();
}
@Override
public long getEndPosition() {
return d.getEndPosition();
}
@Override
public String getCode() {
return d.getCode();
}
@Override
public String getMessage(Locale locale) {
return expunge(d.getMessage(locale));
}
};
}
示例3: main
import javax.tools.Diagnostic; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException("can't get javax.tools.JavaCompiler!");
}
DiagnosticCollector<JavaFileObject> diagColl =
new DiagnosticCollector<JavaFileObject>();
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
List<String> options = new ArrayList<String>();
options.add("-processor");
options.add("MyProcessor");
options.add("-proc:only");
List<File> files = new ArrayList<File>();
files.add(new File(T6458823.class.getResource("TestClass.java").toURI()));
final CompilationTask task = compiler.getTask(null, fm, diagColl,
options, null, fm.getJavaFileObjectsFromFiles(files));
task.call();
int diagCount = 0;
for (Diagnostic<? extends JavaFileObject> diag : diagColl.getDiagnostics()) {
if (diag.getKind() != Diagnostic.Kind.WARNING) {
throw new AssertionError("Only warnings expected");
}
System.out.println(diag);
if (diag.getPosition() == Diagnostic.NOPOS) {
throw new AssertionError("No position info in message");
}
if (diag.getSource() == null) {
throw new AssertionError("No source info in message");
}
diagCount++;
}
if (diagCount != 2) {
throw new AssertionError("unexpected number of warnings: " +
diagCount + ", expected: 2");
}
}
}
示例4: brokenPlatform
import javax.tools.Diagnostic; //导入方法依赖的package包/类
public static void brokenPlatform(
@NonNull final Context ctx,
@NonNull final Iterable<? extends CompileTuple> files,
@NullAllowed final Diagnostic<JavaFileObject> diagnostic) {
if (diagnostic == null) {
return;
}
final Diagnostic<JavaFileObject> error = new Diagnostic<JavaFileObject>() {
@Override
public Kind getKind() {
return Kind.ERROR;
}
@Override
public JavaFileObject getSource() {
return diagnostic.getSource();
}
@Override
public long getPosition() {
return diagnostic.getPosition();
}
@Override
public long getStartPosition() {
return diagnostic.getStartPosition();
}
@Override
public long getEndPosition() {
return diagnostic.getEndPosition();
}
@Override
public long getLineNumber() {
return diagnostic.getLineNumber();
}
@Override
public long getColumnNumber() {
return diagnostic.getColumnNumber();
}
@Override
public String getCode() {
return diagnostic.getCode();
}
@Override
public String getMessage(Locale locale) {
return diagnostic.getMessage(locale);
}
};
for (CompileTuple file : files) {
if (!file.virtual) {
ErrorsCache.setErrors(
ctx.getRootURI(),
file.indexable,
Collections.<Diagnostic<JavaFileObject>>singleton(error),
ERROR_CONVERTOR);
}
}
}