/
SyntheticPojoGenericTypeModel.java
98 lines (84 loc) · 3.21 KB
/
SyntheticPojoGenericTypeModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.model.spi;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.hibernate.search.util.common.impl.CollectionHelper;
/**
* An abstract base for implementations of {@link PojoGenericTypeModel}.
*/
public final class SyntheticPojoGenericTypeModel<T> extends AbstractPojoGenericTypeModel<T> {
public static <T> PojoGenericTypeModel<T[]> array(PojoRawTypeModel<? super T[]> rawTypeModel,
PojoGenericTypeModel<T> elementType) {
return new SyntheticPojoGenericTypeModel<>( rawTypeModel, elementType, Collections.emptyList() );
}
public static <T> PojoGenericTypeModel<T> genericType(PojoRawTypeModel<? super T> rawTypeModel,
PojoGenericTypeModel<?> firstTypeArgument, PojoGenericTypeModel<?> ... otherTypeArguments) {
return new SyntheticPojoGenericTypeModel<>( rawTypeModel, null,
CollectionHelper.asList( firstTypeArgument, otherTypeArguments ) );
}
// For types that should not report implementing an interface, even though they do
// Example: dynamic Map types, that should not be treated as maps when it comes to container extractors.
public static <T> PojoGenericTypeModel<T> opaqueType(PojoRawTypeModel<T> rawTypeModel) {
return new SyntheticPojoGenericTypeModel<>( rawTypeModel, null, Collections.emptyList() );
}
private final PojoGenericTypeModel<?> arrayElementType;
private final List<PojoGenericTypeModel<?>> genericTypeArguments;
private SyntheticPojoGenericTypeModel(PojoRawTypeModel<? super T> rawTypeModel,
PojoGenericTypeModel<?> arrayElementType,
List<PojoGenericTypeModel<?>> genericTypeArguments) {
super( rawTypeModel );
this.arrayElementType = arrayElementType;
this.genericTypeArguments = genericTypeArguments;
}
@Override
public String name() {
if ( arrayElementType != null ) {
return arrayElementType.name() + "[]";
}
if ( genericTypeArguments.isEmpty() ) {
return rawType().name();
}
StringBuilder builder = new StringBuilder();
builder.append( rawType().name() );
builder.append( '<' );
boolean first = true;
for ( PojoGenericTypeModel<?> genericTypeArgument : genericTypeArguments ) {
if ( first ) {
first = false;
}
else {
builder.append( ", " );
}
builder.append( genericTypeArgument.name() );
}
builder.append( '>' );
return builder.toString();
}
@Override
public <U> Optional<PojoTypeModel<? extends U>> castTo(Class<U> target) {
// Cannot cast synthetic types.
return Optional.empty();
}
@Override
public Optional<PojoGenericTypeModel<?>> arrayElementType() {
return Optional.ofNullable( arrayElementType );
}
@Override
public Optional<? extends PojoGenericTypeModel<?>> typeArgument(Class<?> rawSuperType,
int typeParameterIndex) {
if ( genericTypeArguments.isEmpty() ) {
// Raw type
return Optional.empty();
}
if ( rawSuperType.isAssignableFrom( rawType().typeIdentifier().javaClass() ) ) {
return Optional.of( genericTypeArguments.get( typeParameterIndex ) );
}
return Optional.empty();
}
}