1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29 import java.util.StringJoiner;
30 import java.util.function.Predicate;
31
32 import org.apache.maven.api.JavaPathType;
33 import org.apache.maven.api.PathType;
34
35
36
37
38
39
40
41 class PathModularizationCache {
42
43
44
45
46
47
48 private final Map<Path, PathModularization> moduleInfo;
49
50
51
52
53
54
55
56 private final Map<Path, PathType> pathTypes;
57
58
59
60
61 PathModularizationCache() {
62 moduleInfo = new HashMap<>();
63 pathTypes = new HashMap<>();
64 }
65
66
67
68
69
70 PathModularization getModuleInfo(Path path) throws IOException {
71 PathModularization info = moduleInfo.get(path);
72 if (info == null) {
73 info = new PathModularization(path, true);
74 moduleInfo.put(path, info);
75 pathTypes.put(path, info.getPathType());
76 }
77 return info;
78 }
79
80
81
82
83
84
85 private PathType getPathType(Path path) throws IOException {
86 PathType type = pathTypes.get(path);
87 if (type == null) {
88 type = new PathModularization(path, false).getPathType();
89 pathTypes.put(path, type);
90 }
91 return type;
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 Optional<PathType> selectPathType(Set<PathType> types, Predicate<PathType> filter, Path path) throws IOException {
110 PathType selected = null;
111 boolean classes = false;
112 boolean modules = false;
113 boolean unknown = false;
114 boolean processorClasses = false;
115 boolean processorModules = false;
116 for (PathType type : types) {
117 if (filter.test(type)) {
118 if (JavaPathType.CLASSES.equals(type)) {
119 classes = true;
120 } else if (JavaPathType.MODULES.equals(type)) {
121 modules = true;
122 } else if (JavaPathType.PROCESSOR_CLASSES.equals(type)) {
123 processorClasses = true;
124 } else if (JavaPathType.PROCESSOR_MODULES.equals(type)) {
125 processorModules = true;
126 } else {
127 unknown = true;
128 }
129 if (selected == null) {
130 selected = type;
131 } else if (unknown) {
132
133
134 return Optional.empty();
135 }
136 }
137 }
138
139
140
141
142
143 if (classes | modules) {
144 if (classes & modules) {
145 selected = getPathType(path);
146 } else if (classes) {
147 selected = JavaPathType.CLASSES;
148 } else {
149 selected = JavaPathType.MODULES;
150 }
151 } else if (processorClasses & processorModules) {
152 selected = getPathType(path);
153 if (JavaPathType.CLASSES.equals(selected)) {
154 selected = JavaPathType.PROCESSOR_CLASSES;
155 } else if (JavaPathType.MODULES.equals(selected)) {
156 selected = JavaPathType.PROCESSOR_MODULES;
157 }
158 }
159 return Optional.ofNullable(selected);
160 }
161
162
163
164
165
166
167
168
169
170 Optional<String> warningForFilenameBasedAutomodules(Collection<Path> modulePaths) throws IOException {
171 if (modulePaths == null) {
172 return Optional.empty();
173 }
174 var automodulesDetected = new ArrayList<String>();
175 for (Path p : modulePaths) {
176 getModuleInfo(p).addIfFilenameBasedAutomodules(automodulesDetected);
177 }
178 if (automodulesDetected.isEmpty()) {
179 return Optional.empty();
180 }
181 String lineSeparator = System.lineSeparator();
182 var joiner = new StringJoiner(
183 lineSeparator + " - ",
184 "Filename-based automodules detected on the module-path: " + lineSeparator + " - ",
185 lineSeparator + "Please don't publish this project to a public artifact repository.");
186 automodulesDetected.forEach(joiner::add);
187 return Optional.of(joiner.toString());
188 }
189 }