@@ -44,7 +44,7 @@ public class FileSource implements FileSystemSource {
44
44
* @param file the source file; must not be {@code null}
45
45
*/
46
46
public static FileSource from (File file ) {
47
- return new FileSource (file );
47
+ return canonicalized (file , null );
48
48
}
49
49
50
50
/**
@@ -55,27 +55,45 @@ public static FileSource from(File file) {
55
55
* @param filePosition the position in the source file; may be {@code null}
56
56
*/
57
57
public static FileSource from (File file , @ Nullable FilePosition filePosition ) {
58
- return new FileSource (file , filePosition );
58
+ return canonicalized (file , filePosition );
59
+ }
60
+
61
+ /**
62
+ * Create a new {@code FileSource} from an existing instance but with a different
63
+ * {@link FilePosition}. This avoids redundant canonical path resolution.
64
+ *
65
+ * @param source the existing {@code FileSource}; must not be {@code null}
66
+ * @param filePosition the new {@code FilePosition}; may be {@code null}
67
+ * @return a new {@code FileSource} with same file and updated position
68
+ */
69
+ public static FileSource withPosition (FileSource source , @ Nullable FilePosition filePosition ) {
70
+ Preconditions .notNull (source , "source must not be null" );
71
+ return direct (source .file , filePosition );
59
72
}
60
73
61
74
private final File file ;
62
75
63
76
@ Nullable
64
77
private final FilePosition filePosition ;
65
78
66
- private FileSource (File file ) {
67
- this (file , null );
68
- }
69
-
70
79
private FileSource (File file , @ Nullable FilePosition filePosition ) {
80
+ this .file = file ;
81
+ this .filePosition = filePosition ;
82
+ }
83
+
84
+ private static FileSource canonicalized (File file , @ Nullable FilePosition filePosition ) {
71
85
Preconditions .notNull (file , "file must not be null" );
72
86
try {
73
- this . file = file .getCanonicalFile ();
87
+ return new FileSource ( file .getCanonicalFile (), filePosition );
74
88
}
75
89
catch (IOException ex ) {
76
90
throw new JUnitException ("Failed to retrieve canonical path for file: " + file , ex );
77
91
}
78
- this .filePosition = filePosition ;
92
+ }
93
+
94
+ private static FileSource direct (File file , @ Nullable FilePosition filePosition ) {
95
+ Preconditions .notNull (file , "file must not be null" );
96
+ return new FileSource (file , filePosition );
79
97
}
80
98
81
99
/**
0 commit comments