Skip to content

Commit 970bf26

Browse files
committed
Merge branch 'di-implement-scopes' into MNG-8230
2 parents 7dfc53d + c0d5f54 commit 970bf26

File tree

22 files changed

+991
-464
lines changed

22 files changed

+991
-464
lines changed

api/maven-api-di/src/main/java/org/apache/maven/di/tool/DiIndexProcessor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import javax.annotation.processing.SupportedSourceVersion;
2525
import javax.lang.model.SourceVersion;
2626
import javax.lang.model.element.Element;
27+
import javax.lang.model.element.PackageElement;
2728
import javax.lang.model.element.TypeElement;
2829
import javax.tools.Diagnostic;
2930
import javax.tools.FileObject;
@@ -71,14 +72,19 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
7172
}
7273

7374
private String getFullClassName(TypeElement typeElement) {
74-
String className = typeElement.getQualifiedName().toString();
75+
StringBuilder className = new StringBuilder(typeElement.getSimpleName());
7576
Element enclosingElement = typeElement.getEnclosingElement();
76-
if (enclosingElement instanceof TypeElement) {
77-
String enclosingClassName =
78-
((TypeElement) enclosingElement).getQualifiedName().toString();
79-
className = enclosingClassName + "$" + typeElement.getSimpleName();
77+
78+
while (enclosingElement instanceof TypeElement) {
79+
className.insert(0, "$").insert(0, ((TypeElement) enclosingElement).getSimpleName());
80+
enclosingElement = enclosingElement.getEnclosingElement();
81+
}
82+
83+
if (enclosingElement instanceof PackageElement) {
84+
className.insert(0, ".").insert(0, ((PackageElement) enclosingElement).getQualifiedName());
8085
}
81-
return className;
86+
87+
return className.toString();
8288
}
8389

8490
private void updateFileIfChanged() throws IOException {

maven-api-impl/pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ under the License.
7171
<groupId>org.apache.maven</groupId>
7272
<artifactId>maven-api-settings</artifactId>
7373
</dependency>
74+
<dependency>
75+
<groupId>org.apache.maven</groupId>
76+
<artifactId>maven-di</artifactId>
77+
</dependency>
7478
<dependency>
7579
<groupId>org.apache.maven.resolver</groupId>
7680
<artifactId>maven-resolver-api</artifactId>
@@ -132,11 +136,6 @@ under the License.
132136
<groupId>org.assertj</groupId>
133137
<artifactId>assertj-core</artifactId>
134138
</dependency>
135-
<dependency>
136-
<groupId>org.apache.maven</groupId>
137-
<artifactId>maven-di</artifactId>
138-
<scope>test</scope>
139-
</dependency>
140139
<dependency>
141140
<groupId>org.apache.maven.resolver</groupId>
142141
<artifactId>maven-resolver-named-locks</artifactId>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.internal.impl.di;
20+
21+
import java.util.Collection;
22+
import java.util.HashMap;
23+
import java.util.LinkedList;
24+
import java.util.Map;
25+
import java.util.function.Supplier;
26+
27+
import org.apache.maven.api.annotations.Nonnull;
28+
import org.apache.maven.di.Key;
29+
import org.apache.maven.di.Scope;
30+
import org.apache.maven.di.impl.DIException;
31+
32+
/**
33+
* MojoExecutionScope
34+
*/
35+
public class MojoExecutionScope implements Scope {
36+
37+
protected static final class ScopeState {
38+
private final Map<Key<?>, Supplier<?>> seeded = new HashMap<>();
39+
40+
private final Map<Key<?>, Object> provided = new HashMap<>();
41+
42+
public <T> void seed(Class<T> clazz, Supplier<T> value) {
43+
seeded.put(Key.of(clazz), value);
44+
}
45+
46+
public Collection<Object> provided() {
47+
return provided.values();
48+
}
49+
}
50+
51+
private final ThreadLocal<LinkedList<ScopeState>> values = new ThreadLocal<>();
52+
53+
public MojoExecutionScope() {}
54+
55+
public static <T> Supplier<T> seededKeySupplier(Class<? extends T> clazz) {
56+
return () -> {
57+
throw new IllegalStateException(
58+
"No instance of " + clazz.getName() + " is bound to the mojo execution scope.");
59+
};
60+
}
61+
62+
public void enter() {
63+
LinkedList<ScopeState> stack = values.get();
64+
if (stack == null) {
65+
stack = new LinkedList<>();
66+
values.set(stack);
67+
}
68+
stack.addFirst(new ScopeState());
69+
}
70+
71+
protected ScopeState getScopeState() {
72+
LinkedList<ScopeState> stack = values.get();
73+
if (stack == null || stack.isEmpty()) {
74+
throw new IllegalStateException();
75+
}
76+
return stack.getFirst();
77+
}
78+
79+
public void exit() {
80+
final LinkedList<ScopeState> stack = values.get();
81+
if (stack == null || stack.isEmpty()) {
82+
throw new IllegalStateException();
83+
}
84+
stack.removeFirst();
85+
if (stack.isEmpty()) {
86+
values.remove();
87+
}
88+
}
89+
90+
public <T> void seed(Class<T> clazz, Supplier<T> value) {
91+
getScopeState().seed(clazz, value);
92+
}
93+
94+
public <T> void seed(Class<T> clazz, final T value) {
95+
seed(clazz, (Supplier<T>) () -> value);
96+
}
97+
98+
@SuppressWarnings("unchecked")
99+
@Nonnull
100+
public <T> Supplier<T> scope(@Nonnull Key<T> key, @Nonnull Supplier<T> unscoped) {
101+
return () -> {
102+
LinkedList<ScopeState> stack = values.get();
103+
if (stack == null || stack.isEmpty()) {
104+
throw new DIException("Cannot access " + key + " outside of a scoping block");
105+
}
106+
107+
ScopeState state = stack.getFirst();
108+
109+
Supplier<?> seeded = state.seeded.get(key);
110+
111+
if (seeded != null) {
112+
return (T) seeded.get();
113+
}
114+
115+
T provided = (T) state.provided.get(key);
116+
if (provided == null && unscoped != null) {
117+
provided = unscoped.get();
118+
state.provided.put(key, provided);
119+
}
120+
121+
return provided;
122+
};
123+
}
124+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.internal.impl.di;
20+
21+
import org.apache.maven.di.impl.DIException;
22+
23+
public class OutOfScopeException extends DIException {
24+
public OutOfScopeException(String message) {
25+
super(message);
26+
}
27+
28+
public OutOfScopeException(String message, Throwable cause) {
29+
super(message, cause);
30+
}
31+
}

0 commit comments

Comments
 (0)