|
| 1 | +/** |
| 2 | + * Models the different ways to create paths. Either by using `java.io.File`-related APIs or `java.nio.file.Path`-related APIs. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | + |
| 7 | +/** Models the creation of a path. */ |
| 8 | +abstract class PathCreation extends Expr { |
| 9 | + /** |
| 10 | + * Gets an input that is used in the creation of this path. |
| 11 | + * This excludes inputs of type `File` and `Path`. |
| 12 | + */ |
| 13 | + abstract Expr getAnInput(); |
| 14 | +} |
| 15 | + |
| 16 | +/** Models the `java.nio.file.Paths.get` method. */ |
| 17 | +private class PathsGet extends PathCreation, MethodAccess { |
| 18 | + PathsGet() { |
| 19 | + exists(Method m | m = this.getMethod() | |
| 20 | + m.getDeclaringType() instanceof TypePaths and |
| 21 | + m.getName() = "get" |
| 22 | + ) |
| 23 | + } |
| 24 | + |
| 25 | + override Expr getAnInput() { result = this.getAnArgument() } |
| 26 | +} |
| 27 | + |
| 28 | +/** Models the `java.nio.file.FileSystem.getPath` method. */ |
| 29 | +private class FileSystemGetPath extends PathCreation, MethodAccess { |
| 30 | + FileSystemGetPath() { |
| 31 | + exists(Method m | m = this.getMethod() | |
| 32 | + m.getDeclaringType() instanceof TypeFileSystem and |
| 33 | + m.getName() = "getPath" |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + override Expr getAnInput() { result = this.getAnArgument() } |
| 38 | +} |
| 39 | + |
| 40 | +/** Models the `new java.io.File(...)` constructor. */ |
| 41 | +private class FileCreation extends PathCreation, ClassInstanceExpr { |
| 42 | + FileCreation() { this.getConstructedType() instanceof TypeFile } |
| 43 | + |
| 44 | + override Expr getAnInput() { |
| 45 | + result = this.getAnArgument() and |
| 46 | + // Relevant arguments include those that are not a `File`. |
| 47 | + not result.getType() instanceof TypeFile |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** Models the `java.nio.file.Path.resolveSibling` method. */ |
| 52 | +private class PathResolveSiblingCreation extends PathCreation, MethodAccess { |
| 53 | + PathResolveSiblingCreation() { |
| 54 | + exists(Method m | m = this.getMethod() | |
| 55 | + m.getDeclaringType() instanceof TypePath and |
| 56 | + m.getName() = "resolveSibling" |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override Expr getAnInput() { |
| 61 | + result = this.getAnArgument() and |
| 62 | + // Relevant arguments are those of type `String`. |
| 63 | + result.getType() instanceof TypeString |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** Models the `java.nio.file.Path.resolve` method. */ |
| 68 | +private class PathResolveCreation extends PathCreation, MethodAccess { |
| 69 | + PathResolveCreation() { |
| 70 | + exists(Method m | m = this.getMethod() | |
| 71 | + m.getDeclaringType() instanceof TypePath and |
| 72 | + m.getName() = "resolve" |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + override Expr getAnInput() { |
| 77 | + result = this.getAnArgument() and |
| 78 | + // Relevant arguments are those of type `String`. |
| 79 | + result.getType() instanceof TypeString |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** Models the `java.nio.file.Path.of` method. */ |
| 84 | +private class PathOfCreation extends PathCreation, MethodAccess { |
| 85 | + PathOfCreation() { |
| 86 | + exists(Method m | m = this.getMethod() | |
| 87 | + m.getDeclaringType() instanceof TypePath and |
| 88 | + m.getName() = "of" |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + override Expr getAnInput() { result = this.getAnArgument() } |
| 93 | +} |
| 94 | + |
| 95 | +/** Models the `new java.io.FileWriter(...)` constructor. */ |
| 96 | +private class FileWriterCreation extends PathCreation, ClassInstanceExpr { |
| 97 | + FileWriterCreation() { this.getConstructedType().hasQualifiedName("java.io", "FileWriter") } |
| 98 | + |
| 99 | + override Expr getAnInput() { |
| 100 | + result = this.getAnArgument() and |
| 101 | + // Relevant arguments are those of type `String`. |
| 102 | + result.getType() instanceof TypeString |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** Models the `new java.io.FileReader(...)` constructor. */ |
| 107 | +private class FileReaderCreation extends PathCreation, ClassInstanceExpr { |
| 108 | + FileReaderCreation() { this.getConstructedType().hasQualifiedName("java.io", "FileReader") } |
| 109 | + |
| 110 | + override Expr getAnInput() { |
| 111 | + result = this.getAnArgument() and |
| 112 | + // Relevant arguments are those of type `String`. |
| 113 | + result.getType() instanceof TypeString |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/** Models the `new java.io.FileInputStream(...)` constructor. */ |
| 118 | +private class FileInputStreamCreation extends PathCreation, ClassInstanceExpr { |
| 119 | + FileInputStreamCreation() { |
| 120 | + this.getConstructedType().hasQualifiedName("java.io", "FileInputStream") |
| 121 | + } |
| 122 | + |
| 123 | + override Expr getAnInput() { |
| 124 | + result = this.getAnArgument() and |
| 125 | + // Relevant arguments are those of type `String`. |
| 126 | + result.getType() instanceof TypeString |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** Models the `new java.io.FileOutputStream(...)` constructor. */ |
| 131 | +private class FileOutputStreamCreation extends PathCreation, ClassInstanceExpr { |
| 132 | + FileOutputStreamCreation() { |
| 133 | + this.getConstructedType().hasQualifiedName("java.io", "FileOutputStream") |
| 134 | + } |
| 135 | + |
| 136 | + override Expr getAnInput() { |
| 137 | + result = this.getAnArgument() and |
| 138 | + // Relevant arguments are those of type `String`. |
| 139 | + result.getType() instanceof TypeString |
| 140 | + } |
| 141 | +} |
0 commit comments