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.IOException;
|
||||
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
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
|
||||
*/
|
||||
@ -48,18 +47,26 @@ public class DiagnosticDescriptor {
|
||||
}
|
||||
|
||||
public static DiagnosticDescriptor forDiagnostic(String sourceDir, javax.tools.Diagnostic<? extends JavaFileObject> diagnostic) {
|
||||
try
|
||||
{
|
||||
String sourceName = new File(diagnostic.getSource().toUri()).getCanonicalPath();
|
||||
return new DiagnosticDescriptor(
|
||||
(sourceName.length() > sourceDir.length() ? sourceName.substring( sourceDir.length() + 1 ) : sourceName),
|
||||
diagnostic.getKind(),
|
||||
diagnostic.getLineNumber(),
|
||||
""
|
||||
);
|
||||
return new DiagnosticDescriptor(
|
||||
getSourceName( sourceDir, diagnostic ),
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user