Просмотр исходного кода

explicitly type Token::file to type File instead of String

Jan Wittler 4 лет назад
Родитель
Сommit
f056eb2871
23 измененных файлов с 113 добавлено и 116 удалено
  1. 8 2
      core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java
  2. 6 4
      language-api/src/main/java/de/jplag/Token.java
  3. 5 8
      language-api/src/main/java/de/jplag/TokenPrinter.java
  4. 23 22
      language-api/src/test/java/de/jplag/TokenPrinterTest.java
  5. 7 6
      language-testutils/src/test/java/de/jplag/testutils/TokenUtils.java
  6. 2 2
      languages/cpp/src/main/java/de/jplag/cpp/Scanner.java
  7. 3 3
      languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java
  8. 3 2
      languages/emf-metamodel-dynamic/src/main/java/de/jplag/emf/dynamic/DynamicMetamodelToken.java
  9. 5 5
      languages/emf-metamodel-dynamic/src/test/java/de/jplag/emf/dynamic/MinimalDynamicMetamodelTest.java
  10. 7 6
      languages/emf-metamodel/src/main/java/de/jplag/emf/MetamodelToken.java
  11. 2 2
      languages/emf-metamodel/src/main/java/de/jplag/emf/parser/EcoreParser.java
  12. 5 5
      languages/emf-metamodel/src/test/java/de/jplag/emf/MinimalMetamodelTest.java
  13. 3 3
      languages/golang/src/main/java/de/jplag/golang/GoParserAdapter.java
  14. 4 17
      languages/java/src/main/java/de/jplag/java/JavacAdapter.java
  15. 2 2
      languages/java/src/main/java/de/jplag/java/Parser.java
  16. 7 5
      languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java
  17. 3 3
      languages/kotlin/src/main/java/de/jplag/kotlin/KotlinParserAdapter.java
  18. 5 6
      languages/python-3/src/main/java/de/jplag/python3/Parser.java
  19. 3 3
      languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java
  20. 3 3
      languages/rust/src/main/java/de/jplag/rust/RustParserAdapter.java
  21. 2 2
      languages/scala/src/main/scala/de/jplag/scala/Parser.scala
  22. 2 2
      languages/scheme/src/main/java/de/jplag/scheme/Parser.java
  23. 3 3
      languages/text/src/main/java/de/jplag/text/ParserAdapter.java

+ 8 - 2
core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java

@@ -1,5 +1,6 @@
 package de.jplag.reporting.jsonfactory;
 
+import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -97,8 +98,13 @@ public class ComparisonReportWriter {
         Token startOfSecond = tokensSecond.get(match.startOfSecond());
         Token endOfSecond = tokensSecond.get(match.startOfSecond() + match.length() - 1);
 
-        return new Match(startOfFirst.getFile(), startOfSecond.getFile(), startOfFirst.getLine(), endOfFirst.getLine(), startOfSecond.getLine(),
-                endOfSecond.getLine(), match.length());
+        return new Match(relativizedFilePath(startOfFirst.getFile(), comparison.firstSubmission()),
+                relativizedFilePath(startOfSecond.getFile(), comparison.secondSubmission()), startOfFirst.getLine(), endOfFirst.getLine(),
+                startOfSecond.getLine(), endOfSecond.getLine(), match.length());
+    }
+
+    private String relativizedFilePath(File file, Submission submission) {
+        return submission.getRoot().toPath().relativize(file.toPath()).toString();
     }
 
 }

+ 6 - 4
language-api/src/main/java/de/jplag/Token.java

@@ -1,5 +1,7 @@
 package de.jplag;
 
+import java.io.File;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -16,14 +18,14 @@ public class Token {
     private int line;
     private int column;
     private int length;
-    private String file;
+    private File file;
     private TokenType type;
 
     /**
      * Creates a token of type {@link SharedTokenType#FILE_END FILE_END} without information about line, column, and length.
      * @param file is the name of the source code file.
      */
-    public static Token fileEnd(String file) {
+    public static Token fileEnd(File file) {
         return new Token(SharedTokenType.FILE_END, file, NO_VALUE, NO_VALUE, NO_VALUE);
     }
 
@@ -35,7 +37,7 @@ public class Token {
      * @param column is the column index, meaning where the token starts in the line. Index is 1-based.
      * @param length is the length of the token in the source code.
      */
-    public Token(TokenType type, String file, int line, int column, int length) {
+    public Token(TokenType type, File file, int line, int column, int length) {
         if (line == 0) {
             logger.warn("Creating a token with line index 0 while index is 1-based");
         }
@@ -60,7 +62,7 @@ public class Token {
     /**
      * @return the name of the file where the source code that the token represents is located in.
      */
-    public String getFile() {
+    public File getFile() {
         return file;
     }
 

+ 5 - 8
language-api/src/main/java/de/jplag/TokenPrinter.java

@@ -65,10 +65,10 @@ public final class TokenPrinter {
      */
     public static String printTokens(List<Token> tokenList, File rootDirectory, Optional<String> suffix) {
         PrinterOutputBuilder builder = new PrinterOutputBuilder();
-        Map<String, List<Token>> fileToTokens = groupTokensByFile(tokenList);
+        Map<File, List<Token>> fileToTokens = groupTokensByFile(tokenList);
 
-        fileToTokens.forEach((String fileName, List<Token> fileTokens) -> {
-            builder.append(fileName);
+        fileToTokens.forEach((File file, List<Token> fileTokens) -> {
+            builder.append(rootDirectory.toPath().relativize(file.toPath()).toString());
 
             List<LineData> lineDatas = getLineData(fileTokens, rootDirectory, suffix);
             lineDatas.forEach(lineData -> {
@@ -115,10 +115,7 @@ public final class TokenPrinter {
 
     private static List<LineData> getLineData(List<Token> fileTokens, File root, Optional<String> suffix) {
         // We expect that all fileTokens share the same Token.file!
-
-        String fileName = fileTokens.get(0).getFile();
-        // handle 'files as submissions' mode
-        File file = fileName.isEmpty() ? root : new File(root, fileName);
+        File file = fileTokens.get(0).getFile();
         if (suffix.isPresent()) {
             file = new File(file.getPath() + suffix.get());
         }
@@ -144,7 +141,7 @@ public final class TokenPrinter {
                 .toList();
     }
 
-    private static Map<String, List<Token>> groupTokensByFile(List<Token> tokens) {
+    private static Map<File, List<Token>> groupTokensByFile(List<Token> tokens) {
         return tokens.stream().collect(Collectors.groupingBy(Token::getFile));
     }
 

+ 23 - 22
language-api/src/test/java/de/jplag/TokenPrinterTest.java

@@ -1,9 +1,9 @@
 package de.jplag;
 
-import static de.jplag.SharedTokenType.FILE_END;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.File;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.List;
@@ -28,37 +28,38 @@ class TokenPrinterTest {
 
         // See TokenPrinterTest.txt for the intended behaviour
         List<Token> tokens = new ArrayList<>();
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 1, 1, "STRING".length()));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 2, 1, "STRING".length() + 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 3, 1, "STRING".length() + 2));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 4, 1, "STRING".length() + 10));
+        File testFile = new File(TEST_FILE_LOCATION.toFile(), TEST_FILE_NAME);
+        tokens.add(new Token(TestTokenType.STRING, testFile, 1, 1, "STRING".length()));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 2, 1, "STRING".length() + 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 3, 1, "STRING".length() + 2));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 4, 1, "STRING".length() + 10));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 6, 3, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 7, 9, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 6, 3, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 7, 9, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 9, 1, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 9, 10, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 9, 1, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 9, 10, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 10, 1, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 10, 5, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 10, 1, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 10, 5, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 12, 1, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 12, 5, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 12, 10, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 12, 1, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 12, 5, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 12, 10, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 14, 10, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 14, 5, 1));
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 14, 1, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 14, 10, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 14, 5, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 14, 1, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 16, -5, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 16, -5, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 19, 100, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 19, 100, 1));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 22, 1, 100));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 22, 1, 100));
 
-        tokens.add(new Token(FILE_END, TEST_FILE_NAME, Token.NO_VALUE, Token.NO_VALUE, Token.NO_VALUE));
+        tokens.add(Token.fileEnd(testFile));
 
-        tokens.add(new Token(TestTokenType.STRING, TEST_FILE_NAME, 100, 1, 1));
+        tokens.add(new Token(TestTokenType.STRING, testFile, 100, 1, 1));
 
         String output = TokenPrinter.printTokens(tokens, TEST_FILE_LOCATION.toFile());
         logger.info(output); // no additional newline required

+ 7 - 6
language-testutils/src/test/java/de/jplag/testutils/TokenUtils.java

@@ -1,5 +1,6 @@
 package de.jplag.testutils;
 
+import java.io.File;
 import java.util.List;
 
 import de.jplag.Token;
@@ -14,21 +15,21 @@ public final class TokenUtils {
     /**
      * Returns the type of all tokens that belong to a certain file.
      * @param tokens is the list of {@link Token Tokens}.
-     * @param name is the name of the target file.
+     * @param file is the target file.
      * @return the immutable list of token types.
      */
-    public static List<TokenType> tokenTypesByFile(List<Token> tokens, String name) {
-        return tokensByFile(tokens, name).stream().map(Token::getType).toList();
+    public static List<TokenType> tokenTypesByFile(List<Token> tokens, File file) {
+        return tokensByFile(tokens, file).stream().map(Token::getType).toList();
     }
 
     /**
      * Returns the tokens that belong to a certain file.
      * @param tokens is the list of {@link Token Tokens}.
-     * @param name is the name of the target file.
+     * @param file is the target file.
      * @return the immutable list of tokens.
      */
-    public static List<Token> tokensByFile(List<Token> tokens, String name) {
-        return tokens.stream().filter(it -> it.getFile().startsWith(name)).toList();
+    public static List<Token> tokensByFile(List<Token> tokens, File file) {
+        return tokens.stream().filter(it -> it.getFile().equals(file)).toList();
     }
 
 }

+ 2 - 2
languages/cpp/src/main/java/de/jplag/cpp/Scanner.java

@@ -9,7 +9,7 @@ import de.jplag.AbstractParser;
 import de.jplag.Token;
 
 public class Scanner extends AbstractParser {
-    private String currentFile;
+    private File currentFile;
 
     private List<Token> tokens;
 
@@ -24,7 +24,7 @@ public class Scanner extends AbstractParser {
         tokens = new ArrayList<>();
         errors = 0;
         for (File file : files) {
-            this.currentFile = file.getName();
+            this.currentFile = file;
             logger.trace("Scanning file {}", currentFile);
             if (!CPPScanner.scanFile(file, this)) {
                 errors++;

+ 3 - 3
languages/csharp/src/main/java/de/jplag/csharp/CSharpParserAdapter.java

@@ -26,7 +26,7 @@ import de.jplag.csharp.grammar.CSharpParser;
  */
 public class CSharpParserAdapter extends AbstractParser {
     private List<Token> tokens;
-    private String currentFile;
+    private File currentFile;
 
     /**
      * Creates the parser adapter.
@@ -47,14 +47,14 @@ public class CSharpParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
         try (FileInputStream inputStream = new FileInputStream(file)) {
-            currentFile = file.getName();
+            currentFile = file;
 
             // create a lexer, a parser and a buffer between them.
             CSharpLexer lexer = new CSharpLexer(CharStreams.fromStream(inputStream));

+ 3 - 2
languages/emf-metamodel-dynamic/src/main/java/de/jplag/emf/dynamic/DynamicMetamodelToken.java

@@ -1,5 +1,6 @@
 package de.jplag.emf.dynamic;
 
+import java.io.File;
 import java.util.Optional;
 
 import org.eclipse.emf.ecore.EObject;
@@ -13,11 +14,11 @@ import de.jplag.emf.MetamodelToken;
  */
 public class DynamicMetamodelToken extends MetamodelToken {
 
-    public DynamicMetamodelToken(TokenType type, String file, EObject eObject) {
+    public DynamicMetamodelToken(TokenType type, File file, EObject eObject) {
         super(type, file, NO_VALUE, NO_VALUE, NO_VALUE, Optional.of(eObject));
     }
 
-    public DynamicMetamodelToken(TokenType type, String file) {
+    public DynamicMetamodelToken(TokenType type, File file) {
         super(type, file);
     }
 }

+ 5 - 5
languages/emf-metamodel-dynamic/src/test/java/de/jplag/emf/dynamic/MinimalDynamicMetamodelTest.java

@@ -10,7 +10,6 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -42,16 +41,17 @@ class MinimalDynamicMetamodelTest {
 
     @Test
     void testBookstoreMetamodels() {
-        List<Token> result = language.parse(Arrays.stream(TEST_SUBJECTS).map(path -> new File(BASE_PATH.toFile(), path)).collect(Collectors.toSet()));
+        List<File> testFiles = Arrays.stream(TEST_SUBJECTS).map(path -> new File(BASE_PATH.toFile(), path)).toList();
+        List<Token> result = language.parse(new HashSet<>(testFiles));
         List<TokenType> tokenTypes = result.stream().map(Token::getType).toList();
         logger.debug(TokenPrinter.printTokens(result, baseDirectory, Optional.of(Language.VIEW_FILE_SUFFIX)));
         logger.info("parsed token types: " + tokenTypes.stream().map(TokenType::getDescription).toList().toString());
         assertEquals(64, tokenTypes.size());
         assertEquals(7, new HashSet<>(tokenTypes.stream().filter(DynamicMetamodelTokenType.class::isInstance).toList()).size());
 
-        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[0]);
-        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[2]);
-        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[1]);
+        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(0));
+        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(2));
+        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(1));
         assertTrue(bookstoreTokens.size() < bookstoreExtendedTokens.size());
         assertIterableEquals(bookstoreTokens, bookstoreRenamedTokens);
     }

+ 7 - 6
languages/emf-metamodel/src/main/java/de/jplag/emf/MetamodelToken.java

@@ -1,5 +1,6 @@
 package de.jplag.emf;
 
+import java.io.File;
 import java.util.Optional;
 
 import org.eclipse.emf.ecore.EObject;
@@ -18,32 +19,32 @@ public class MetamodelToken extends Token {
     /**
      * Creates an Ecore metamodel token that corresponds to an EObject.
      * @param type is the type of the token.
-     * @param file is the name of the source model file.
+     * @param file is the source model file.
      * @param eObject is the corresponding eObject in the model from which this token was extracted.
      */
-    public MetamodelToken(MetamodelTokenType type, String file, EObject eObject) {
+    public MetamodelToken(MetamodelTokenType type, File file, EObject eObject) {
         this(type, file, NO_VALUE, NO_VALUE, NO_VALUE, Optional.of(eObject));
     }
 
     /**
      * Creates an Ecore metamodel token.
      * @param type is the type of the token.
-     * @param file is the name of the source model file.
+     * @param file is the source model file.
      */
-    public MetamodelToken(TokenType type, String file) {
+    public MetamodelToken(TokenType type, File file) {
         this(type, file, NO_VALUE, NO_VALUE, NO_VALUE, Optional.empty());
     }
 
     /**
      * Creates a token with column and length information.
      * @param type is the token type.
-     * @param file is the name of the source code file.
+     * @param file is the source code file.
      * @param line is the line index in the source code where the token resides. Cannot be smaller than 1.
      * @param column is the column index, meaning where the token starts in the line.
      * @param length is the length of the token in the source code.
      * @param eObject is the corresponding eObject in the model from which this token was extracted
      */
-    public MetamodelToken(TokenType type, String file, int line, int column, int length, Optional<EObject> eObject) {
+    public MetamodelToken(TokenType type, File file, int line, int column, int length, Optional<EObject> eObject) {
         super(type, file, line, column, length);
         this.eObject = eObject;
     }

+ 2 - 2
languages/emf-metamodel/src/main/java/de/jplag/emf/parser/EcoreParser.java

@@ -22,7 +22,7 @@ import de.jplag.emf.util.MetamodelTreeView;
  */
 public class EcoreParser extends AbstractParser {
     protected List<Token> tokens;
-    protected String currentFile;
+    protected File currentFile;
     protected MetamodelTreeView treeView;
     protected AbstractMetamodelVisitor visitor;
 
@@ -42,7 +42,7 @@ public class EcoreParser extends AbstractParser {
         errors = 0;
         tokens = new ArrayList<>();
         for (File file : files) {
-            currentFile = file.getName();
+            currentFile = file;
             parseModelFile(file);
         }
         return tokens;

+ 5 - 5
languages/emf-metamodel/src/test/java/de/jplag/emf/MinimalMetamodelTest.java

@@ -10,7 +10,6 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -42,7 +41,8 @@ class MinimalMetamodelTest {
 
     @Test
     void testBookstoreMetamodels() {
-        List<Token> result = language.parse(Arrays.stream(TEST_SUBJECTS).map(path -> new File(BASE_PATH.toFile(), path)).collect(Collectors.toSet()));
+        List<File> testFiles = Arrays.stream(TEST_SUBJECTS).map(path -> new File(BASE_PATH.toFile(), path)).toList();
+        List<Token> result = language.parse(new HashSet<>(testFiles));
 
         logger.debug(TokenPrinter.printTokens(result, baseDirectory, Optional.of(Language.VIEW_FILE_SUFFIX)));
         List<TokenType> tokenTypes = result.stream().map(Token::getType).toList();
@@ -50,9 +50,9 @@ class MinimalMetamodelTest {
         assertEquals(43, tokenTypes.size());
         assertEquals(10, new HashSet<>(tokenTypes).size());
 
-        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[0]);
-        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[2]);
-        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, TEST_SUBJECTS[1]);
+        var bookstoreTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(0));
+        var bookstoreRenamedTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(2));
+        var bookstoreExtendedTokens = TokenUtils.tokenTypesByFile(result, testFiles.get(1));
         assertTrue(bookstoreTokens.size() < bookstoreExtendedTokens.size());
         assertIterableEquals(bookstoreTokens, bookstoreRenamedTokens);
     }

+ 3 - 3
languages/golang/src/main/java/de/jplag/golang/GoParserAdapter.java

@@ -20,7 +20,7 @@ import de.jplag.golang.grammar.GoLexer;
 import de.jplag.golang.grammar.GoParser;
 
 public class GoParserAdapter extends AbstractParser {
-    private String currentFile;
+    private File currentFile;
     private List<Token> tokens;
 
     public List<Token> parse(Set<File> files) {
@@ -29,14 +29,14 @@ public class GoParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
         try (FileInputStream inputStream = new FileInputStream(file)) {
-            currentFile = file.getName();
+            currentFile = file;
 
             GoLexer lexer = new GoLexer(CharStreams.fromStream(inputStream));
             CommonTokenStream tokenStream = new CommonTokenStream(lexer);

+ 4 - 17
languages/java/src/main/java/de/jplag/java/JavacAdapter.java

@@ -40,10 +40,11 @@ public class JavacAdapter {
             final Trees trees = Trees.instance(task);
             final SourcePositions positions = trees.getSourcePositions();
             for (final CompilationUnitTree ast : executeCompilationTask(task, parser.logger)) {
-                final String filename = relativeFileName(ast, files);
+                final String filename = ast.getSourceFile().getName();
+                File file = new File(ast.getSourceFile().toUri());
                 final LineMap map = ast.getLineMap();
-                ast.accept(new TokenGeneratingTreeScanner(filename, parser, map, positions, ast), null);
-                parser.add(Token.fileEnd(filename));
+                ast.accept(new TokenGeneratingTreeScanner(file, parser, map, positions, ast), null);
+                parser.add(Token.fileEnd(file));
             }
         } catch (IOException exception) {
             parser.logger.error(exception.getMessage(), exception);
@@ -61,20 +62,6 @@ public class JavacAdapter {
         return abstractSyntaxTrees;
     }
 
-    private String relativeFileName(final CompilationUnitTree ast, Set<File> files) {
-        String fullFilePath = ast.getSourceFile().toUri().toString();
-        var matchingFile = files.stream().filter(file -> {
-            try {
-                if (file.getCanonicalPath().contains(fullFilePath)) {
-                    return true;
-                }
-            } catch (IOException e) {
-            }
-            return false;
-        }).map(File::getName).findFirst();
-        return matchingFile.orElse(ast.getSourceFile().getName());
-    }
-
     private int processErrors(Logger logger, DiagnosticCollector<Object> listener) {
         int errors = 0;
         for (Diagnostic<?> diagnosticItem : listener.getDiagnostics()) {

+ 2 - 2
languages/java/src/main/java/de/jplag/java/Parser.java

@@ -26,8 +26,8 @@ public class Parser extends AbstractParser {
         return tokens;
     }
 
-    public void add(TokenType type, String filename, long line, long column, long length) {
-        add(new Token(type, filename, (int) line, (int) column, (int) length));
+    public void add(TokenType type, File file, long line, long column, long length) {
+        add(new Token(type, file, (int) line, (int) column, (int) length));
     }
 
     public void add(Token token) {

+ 7 - 5
languages/java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java

@@ -1,5 +1,7 @@
 package de.jplag.java;
 
+import java.io.File;
+
 import com.sun.source.tree.AnnotationTree;
 import com.sun.source.tree.AssertTree;
 import com.sun.source.tree.AssignmentTree;
@@ -43,14 +45,14 @@ import com.sun.source.util.SourcePositions;
 import com.sun.source.util.TreeScanner;
 
 final class TokenGeneratingTreeScanner extends TreeScanner<Object, Object> {
-    private final String filename;
+    private final File file;
     private final Parser parser;
     private final LineMap map;
     private final SourcePositions positions;
     private final CompilationUnitTree ast;
 
-    public TokenGeneratingTreeScanner(String filename, Parser parser, LineMap map, SourcePositions positions, CompilationUnitTree ast) {
-        this.filename = filename;
+    public TokenGeneratingTreeScanner(File file, Parser parser, LineMap map, SourcePositions positions, CompilationUnitTree ast) {
+        this.file = file;
         this.parser = parser;
         this.map = map;
         this.positions = positions;
@@ -64,7 +66,7 @@ final class TokenGeneratingTreeScanner extends TreeScanner<Object, Object> {
      * @param length is the length of the token.
      */
     private void addToken(JavaTokenType tokenType, long position, int length) {
-        parser.add(tokenType, filename, map.getLineNumber(position), map.getColumnNumber(position), length);
+        parser.add(tokenType, file, map.getLineNumber(position), map.getColumnNumber(position), length);
     }
 
     /**
@@ -74,7 +76,7 @@ final class TokenGeneratingTreeScanner extends TreeScanner<Object, Object> {
      * @param end is the end position of the token for the calculation of the length.
      */
     private void addToken(JavaTokenType tokenType, long start, long end) {
-        parser.add(tokenType, filename, map.getLineNumber(start), map.getColumnNumber(start), (end - start));
+        parser.add(tokenType, file, map.getLineNumber(start), map.getColumnNumber(start), (end - start));
     }
 
     @Override

+ 3 - 3
languages/kotlin/src/main/java/de/jplag/kotlin/KotlinParserAdapter.java

@@ -19,7 +19,7 @@ import de.jplag.kotlin.grammar.KotlinLexer;
 import de.jplag.kotlin.grammar.KotlinParser;
 
 public class KotlinParserAdapter extends AbstractParser {
-    private String currentFile;
+    private File currentFile;
     private List<Token> tokens;
 
     /**
@@ -40,14 +40,14 @@ public class KotlinParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
         try (FileInputStream inputStream = new FileInputStream(file)) {
-            currentFile = file.getName();
+            currentFile = file;
 
             KotlinLexer lexer = new KotlinLexer(CharStreams.fromStream(inputStream));
             CommonTokenStream tokenStream = new CommonTokenStream(lexer);

+ 5 - 6
languages/python-3/src/main/java/de/jplag/python3/Parser.java

@@ -24,7 +24,7 @@ import de.jplag.python3.grammar.Python3Parser.File_inputContext;
 public class Parser extends AbstractParser {
 
     private List<Token> tokens;
-    private String currentFile;
+    private File currentFile;
 
     /**
      * Creates the parser.
@@ -41,7 +41,7 @@ public class Parser extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
@@ -52,7 +52,7 @@ public class Parser extends AbstractParser {
         CharStream input;
         try {
             inputStream = new BufferedInputStream(new FileInputStream(file));
-            currentFile = file.getName();
+            currentFile = file;
             input = CharStreams.fromStream(inputStream);
 
             // create a lexer that feeds off of input CharStream
@@ -80,11 +80,10 @@ public class Parser extends AbstractParser {
     }
 
     public void add(TokenType type, org.antlr.v4.runtime.Token token) {
-        tokens.add(new Token(type, (currentFile == null ? "null" : currentFile), token.getLine(), token.getCharPositionInLine() + 1,
-                token.getText().length()));
+        tokens.add(new Token(type, currentFile, token.getLine(), token.getCharPositionInLine() + 1, token.getText().length()));
     }
 
     public void addEnd(TokenType type, org.antlr.v4.runtime.Token token) {
-        tokens.add(new Token(type, (currentFile == null ? "null" : currentFile), token.getLine(), tokens.get(tokens.size() - 1).getColumn() + 1, 0));
+        tokens.add(new Token(type, currentFile, token.getLine(), tokens.get(tokens.size() - 1).getColumn() + 1, 0));
     }
 }

+ 3 - 3
languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java

@@ -26,7 +26,7 @@ import de.jplag.rlang.grammar.RParser;
  */
 public class RParserAdapter extends AbstractParser {
 
-    private String currentFile;
+    private File currentFile;
     private List<Token> tokens;
 
     /**
@@ -48,14 +48,14 @@ public class RParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
         try (FileInputStream inputStream = new FileInputStream(file)) {
-            currentFile = file.getName();
+            currentFile = file;
 
             // create a lexer, a parser and a buffer between them.
             RLexer lexer = new RLexer(CharStreams.fromStream(inputStream));

+ 3 - 3
languages/rust/src/main/java/de/jplag/rust/RustParserAdapter.java

@@ -20,7 +20,7 @@ import de.jplag.rust.grammar.RustParser;
 
 public class RustParserAdapter extends AbstractParser {
 
-    private String currentFile;
+    private File currentFile;
     private List<Token> tokens;
 
     /**
@@ -35,14 +35,14 @@ public class RustParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
         try (FileInputStream inputStream = new FileInputStream(file)) {
-            currentFile = file.getName();
+            currentFile = file;
 
             // create a lexer, a parser and a buffer between them.
             RustLexer lexer = new RustLexer(CharStreams.fromStream(inputStream));

+ 2 - 2
languages/scala/src/main/scala/de/jplag/scala/Parser.scala

@@ -10,7 +10,7 @@ import scala.meta._
 
 
 class Parser extends AbstractParser {
-    private var currentFile: String = _
+    private var currentFile: File = _
 
     private var tokens: ListBuffer[Token] = _
 
@@ -345,7 +345,7 @@ class Parser extends AbstractParser {
     }
 
     private def parseFile(file: File): Boolean = {
-        currentFile = file.getName
+        currentFile = file
 
         try {
             val bytes = java.nio.file.Files.readAllBytes(file.toPath)

+ 2 - 2
languages/scheme/src/main/java/de/jplag/scheme/Parser.java

@@ -10,7 +10,7 @@ import de.jplag.Token;
 import de.jplag.TokenType;
 
 public class Parser extends AbstractParser {
-    private String currentFile;
+    private File currentFile;
 
     private List<Token> tokens;
 
@@ -25,7 +25,7 @@ public class Parser extends AbstractParser {
         tokens = new ArrayList<>();
         errors = 0;
         for (File file : files) {
-            currentFile = file.getName();
+            currentFile = file;
             logger.trace("Parsing file {}", file.getName());
             if (!SchemeParser.parseFile(file, null, this)) {
                 errors++;

+ 3 - 3
languages/text/src/main/java/de/jplag/text/ParserAdapter.java

@@ -24,7 +24,7 @@ public class ParserAdapter extends AbstractParser {
     private final StanfordCoreNLP pipeline;
 
     private List<Token> tokens;
-    private String currentFile;
+    private File currentFile;
     private int currentLine;
     /**
      * The position of the current line break in the content string
@@ -45,13 +45,13 @@ public class ParserAdapter extends AbstractParser {
             if (!parseFile(file)) {
                 errors++;
             }
-            tokens.add(Token.fileEnd(file.getName()));
+            tokens.add(Token.fileEnd(file));
         }
         return tokens;
     }
 
     private boolean parseFile(File file) {
-        this.currentFile = file.getName();
+        this.currentFile = file;
         this.currentLine = 1; // lines start at 1
         this.currentLineBreakIndex = 0;
         String content = readFile(file);