Skip to content

Commit 14b147c

Browse files
committed
Add TypeReference implementation for generated code
This commit adds a TypeReference implementation that is suitable for creating a reference to generated code. Closes gh-28148
1 parent c541bde commit 14b147c

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.generator;
18+
19+
import org.springframework.aot.hint.AbstractTypeReference;
20+
import org.springframework.aot.hint.TypeReference;
21+
import org.springframework.javapoet.ClassName;
22+
import org.springframework.lang.Nullable;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* A {@link TypeReference} for a generated {@linkplain ClassName type}.
27+
*
28+
* @author Stephane Nicoll
29+
* @since 6.0
30+
*/
31+
public final class GeneratedTypeReference extends AbstractTypeReference {
32+
33+
private final ClassName className;
34+
35+
@Nullable
36+
private final TypeReference enclosingType;
37+
38+
private GeneratedTypeReference(ClassName className) {
39+
this.className = className;
40+
this.enclosingType = (className.enclosingClassName() != null
41+
? new GeneratedTypeReference(className.enclosingClassName())
42+
: null);
43+
}
44+
45+
public static GeneratedTypeReference of(ClassName className) {
46+
Assert.notNull(className, "ClassName must not be null");
47+
return new GeneratedTypeReference(className);
48+
}
49+
50+
@Override
51+
public String getCanonicalName() {
52+
return this.className.canonicalName();
53+
}
54+
55+
@Override
56+
public String getPackageName() {
57+
return this.className.packageName();
58+
}
59+
60+
@Override
61+
public String getSimpleName() {
62+
return this.className.simpleName();
63+
}
64+
65+
@Override
66+
public TypeReference getEnclosingType() {
67+
return this.enclosingType;
68+
}
69+
70+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.generator;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.TypeReference;
22+
import org.springframework.javapoet.ClassName;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link GeneratedTypeReference}.
28+
*
29+
* @author Stephane Nicoll
30+
*/
31+
class GeneratedTypeReferenceTests {
32+
33+
@Test
34+
void createWithClassName() {
35+
GeneratedTypeReference typeReference = GeneratedTypeReference.of(
36+
ClassName.get("com.example", "Test"));
37+
assertThat(typeReference.getPackageName()).isEqualTo("com.example");
38+
assertThat(typeReference.getSimpleName()).isEqualTo("Test");
39+
assertThat(typeReference.getCanonicalName()).isEqualTo("com.example.Test");
40+
assertThat(typeReference.getEnclosingType()).isNull();
41+
}
42+
43+
@Test
44+
void createWithClassNameAndParent() {
45+
GeneratedTypeReference typeReference = GeneratedTypeReference.of(
46+
ClassName.get("com.example", "Test").nestedClass("Nested"));
47+
assertThat(typeReference.getPackageName()).isEqualTo("com.example");
48+
assertThat(typeReference.getSimpleName()).isEqualTo("Nested");
49+
assertThat(typeReference.getCanonicalName()).isEqualTo("com.example.Test.Nested");
50+
assertThat(typeReference.getEnclosingType()).satisfies(parentTypeReference -> {
51+
assertThat(parentTypeReference.getPackageName()).isEqualTo("com.example");
52+
assertThat(parentTypeReference.getSimpleName()).isEqualTo("Test");
53+
assertThat(parentTypeReference.getCanonicalName()).isEqualTo("com.example.Test");
54+
assertThat(parentTypeReference.getEnclosingType()).isNull();
55+
});
56+
}
57+
58+
@Test
59+
void equalsWithIdenticalCanonicalNameIsTrue() {
60+
assertThat(GeneratedTypeReference.of(ClassName.get("java.lang", "String")))
61+
.isEqualTo(TypeReference.of(String.class));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)