mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Avoiding NPE for diagnostics without source
This commit is contained in:
parent
1d52cb434e
commit
787b7d0fb9
@ -17,14 +17,13 @@ package org.mapstruct.ap.testutil.compilation.model;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.tools.Diagnostic.Kind;
|
import javax.tools.Diagnostic.Kind;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a diagnostic ocurred during a compilation.
|
* Represents a diagnostic occurred during a compilation.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
@ -48,18 +47,26 @@ public class DiagnosticDescriptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DiagnosticDescriptor forDiagnostic(String sourceDir, javax.tools.Diagnostic<? extends JavaFileObject> diagnostic) {
|
public static DiagnosticDescriptor forDiagnostic(String sourceDir, javax.tools.Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||||
try
|
return new DiagnosticDescriptor(
|
||||||
{
|
getSourceName( sourceDir, diagnostic ),
|
||||||
String sourceName = new File(diagnostic.getSource().toUri()).getCanonicalPath();
|
diagnostic.getKind(),
|
||||||
return new DiagnosticDescriptor(
|
diagnostic.getLineNumber(),
|
||||||
(sourceName.length() > sourceDir.length() ? sourceName.substring( sourceDir.length() + 1 ) : sourceName),
|
""
|
||||||
diagnostic.getKind(),
|
);
|
||||||
diagnostic.getLineNumber(),
|
}
|
||||||
""
|
|
||||||
);
|
private static String getSourceName(String sourceDir, javax.tools.Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||||
|
if ( diagnostic.getSource() == null ) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
catch ( IOException e )
|
|
||||||
{
|
try {
|
||||||
|
String sourceName = new File( diagnostic.getSource().toUri() ).getCanonicalPath();
|
||||||
|
return sourceName.length() > sourceDir.length() ?
|
||||||
|
sourceName.substring( sourceDir.length() + 1 ) :
|
||||||
|
sourceName;
|
||||||
|
}
|
||||||
|
catch ( IOException e ) {
|
||||||
throw new RuntimeException( e );
|
throw new RuntimeException( e );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user