-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #296: avoided reifying type arguments
- Loading branch information
Showing
10 changed files
with
520 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...untime-code-generation/src/main/java/pro/projo/internal/rcg/utilities/GenericVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// // | ||
// Copyright 2024 Mirko Raner // | ||
// // | ||
// Licensed under the Apache License, Version 2.0 (the "License"); // | ||
// you may not use this file except in compliance with the License. // | ||
// You may obtain a copy of the License at // | ||
// // | ||
// http://www.apache.org/licenses/LICENSE-2.0 // | ||
// // | ||
// Unless required by applicable law or agreed to in writing, software // | ||
// distributed under the License is distributed on an "AS IS" BASIS, // | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // | ||
// See the License for the specific language governing permissions and // | ||
// limitations under the License. // | ||
// // | ||
package pro.projo.internal.rcg.utilities; | ||
|
||
import java.util.stream.Stream; | ||
import net.bytebuddy.description.annotation.AnnotationList; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.description.type.TypeDescription.Generic; | ||
import net.bytebuddy.description.type.TypeDescription.Generic.Visitor; | ||
import net.bytebuddy.description.type.TypeList; | ||
import pro.projo.annotations.RawInterfaces; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* {@link GenericVisitor} implements a variation of {@link Visitor.Reifying#INITIATING} | ||
* that will strip type arguments from all interfaces extended by an interface that is | ||
* annotated with {@link RawInterfaces} (effectively causing the interface to extend the | ||
* corresponding raw types). | ||
* | ||
* @author Mirko Raner | ||
**/ | ||
public class GenericVisitor implements Visitor<Generic> | ||
{ | ||
private final Visitor<Generic> delegate = Generic.Visitor.Reifying.INITIATING; | ||
|
||
/** | ||
* Determines if use of the {@link GenericVisitor} is necessary. | ||
* | ||
* @param annotationNames the type names of the annotations present | ||
* @return {@code true} if the annotation names include {@link RawInterfaces}, | ||
* otherwise {@code false} | ||
**/ | ||
public static boolean isNecessaryFor(Stream<String> annotationNames) | ||
{ | ||
return annotationNames.anyMatch(RawInterfaces.class.getName()::equals); | ||
} | ||
|
||
@Override | ||
public Generic onGenericArray(Generic genericArray) | ||
{ | ||
return delegate.onGenericArray(genericArray); | ||
} | ||
|
||
@Override | ||
public Generic onWildcard(Generic wildcard) | ||
{ | ||
return delegate.onWildcard(wildcard); | ||
} | ||
|
||
@Override | ||
public Generic onParameterizedType(Generic parameterizedType) | ||
{ | ||
return delegate.onParameterizedType(parameterizedType); | ||
} | ||
|
||
@Override | ||
public Generic onTypeVariable(Generic typeVariable) | ||
{ | ||
return delegate.onTypeVariable(typeVariable); | ||
} | ||
|
||
@Override | ||
public Generic onNonGenericType(Generic typeDescription) | ||
{ | ||
// NOTE: typeDescription.getDeclaredAnnotations() will typically not return any | ||
// annotations for Generics corresponding to loaded types because | ||
// Generic.OfNonGenericType.ForLoadedType uses AnnotationReader.NoOp.INSTANCE. | ||
// This method will only work with custom Generics that retain annotations. | ||
// | ||
AnnotationList annotations = typeDescription.getDeclaredAnnotations(); | ||
if (typeDescription instanceof RawGenericLoadedType | ||
&& (isNecessaryFor(annotations.asTypeNames().stream()))) | ||
{ | ||
// Even though the type name is known from typeDescription.getActualName() | ||
// simply regenerating the Class via Class.forName(...) is tricky and prone | ||
// to failure because the correct class loader is not known. Unless, the | ||
// current class loader was also used to originally load the Class in question | ||
// the class may not be found in the current class loader, or, even worse, | ||
// a duplicate class is loaded. Therefore, the only way to obtain the Class | ||
// reliably is when the Generic type provided is a RawGenericLoadedType that | ||
// provides access to the encapsulated Class object: | ||
// | ||
Class<?> type = ((RawGenericLoadedType)typeDescription).getType(); | ||
return new Generic.OfNonGenericType.ForLoadedType(type) | ||
{ | ||
@Override | ||
public TypeList.Generic getInterfaces() | ||
{ | ||
Stream<Class<?>> interfaces = Stream.of(type.getInterfaces()); | ||
Stream<TypeDescription.ForLoadedType> rawInterfaces = interfaces.map | ||
( | ||
it -> new TypeDescription.ForLoadedType(it) | ||
{ | ||
private final static long serialVersionUID = 1L; | ||
|
||
@Override | ||
public TypeList.Generic getTypeVariables() | ||
{ | ||
return new TypeList.Generic.Empty(); | ||
} | ||
|
||
@Override | ||
public boolean isGenerified() | ||
{ | ||
return false; | ||
} | ||
} | ||
); | ||
return new TypeList.Generic.Explicit(rawInterfaces.collect(toList())); | ||
} | ||
}; | ||
} | ||
return delegate.onNonGenericType(typeDescription); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...-code-generation/src/main/java/pro/projo/internal/rcg/utilities/RawGenericLoadedType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// // | ||
// Copyright 2024 Mirko Raner // | ||
// // | ||
// Licensed under the Apache License, Version 2.0 (the "License"); // | ||
// you may not use this file except in compliance with the License. // | ||
// You may obtain a copy of the License at // | ||
// // | ||
// http://www.apache.org/licenses/LICENSE-2.0 // | ||
// // | ||
// Unless required by applicable law or agreed to in writing, software // | ||
// distributed under the License is distributed on an "AS IS" BASIS, // | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // | ||
// See the License for the specific language governing permissions and // | ||
// limitations under the License. // | ||
// // | ||
package pro.projo.internal.rcg.utilities; | ||
|
||
import net.bytebuddy.description.type.TypeDescription.Generic; | ||
|
||
/** | ||
* {@link RawGenericLoadedType} is a customization of | ||
* {@link Generic.OfNonGenericType.ForLoadedType} that provides the following | ||
* additional features: | ||
* <ul> | ||
* <li>it preserves annotations declared by the wrapped {@link Class}</li> | ||
* <li>it provides access to the original wrapped {@link Class} object</li> | ||
* </ul> | ||
* @author Mirko Raner | ||
**/ | ||
public class RawGenericLoadedType extends Generic.OfNonGenericType.ForLoadedType | ||
{ | ||
private final Class<?> type; | ||
|
||
public RawGenericLoadedType(Class<?> type) | ||
{ | ||
super(type, new AnnotationReader.Delegator.Simple(type)); | ||
this.type = type; | ||
} | ||
|
||
public Class<?> getType() | ||
{ | ||
return type; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
projo-runtime-code-generation/src/test/java/pro/projo/integration/tests/ProjoDecimalsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// // | ||
// Copyright 2024 Mirko Raner // | ||
// // | ||
// Licensed under the Apache License, Version 2.0 (the "License"); // | ||
// you may not use this file except in compliance with the License. // | ||
// You may obtain a copy of the License at // | ||
// // | ||
// http://www.apache.org/licenses/LICENSE-2.0 // | ||
// // | ||
// Unless required by applicable law or agreed to in writing, software // | ||
// distributed under the License is distributed on an "AS IS" BASIS, // | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // | ||
// See the License for the specific language governing permissions and // | ||
// limitations under the License. // | ||
// // | ||
package pro.projo.integration.tests; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Module; | ||
import com.google.inject.TypeLiteral; | ||
import pro.projo.Projo; | ||
import pro.projo.test.implementations.Ranges; | ||
import pro.projo.test.implementations.Utilities; | ||
import pro.projo.test.interfaces.Decimals; | ||
import pro.projo.test.interfaces.Literals; | ||
import pro.projo.test.interfaces.Provider; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ProjoDecimalsIT | ||
{ | ||
@BeforeClass | ||
public static void deleteDecimalClass() throws URISyntaxException | ||
{ | ||
ClassLoader classLoader = ProjoDecimalsIT.class.getClassLoader(); | ||
URL url = classLoader.getResource("pro/projo/test/interfaces/Decimal.class"); | ||
if (url != null) | ||
{ | ||
new File(url.toURI()).delete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void implementationOfDecimalsIsProperlyBound() throws Exception | ||
{ | ||
Literals literals = new Literals() {}; | ||
Injector injector = Guice.createInjector(module(literals)); | ||
Decimals<?> naturals = injector.getInstance(Decimals.class); | ||
Method literalsMethod = Provider.class.getMethod("literals"); | ||
Literals result = (Literals)literalsMethod.invoke(naturals); | ||
assertEquals(literals, result); | ||
} | ||
|
||
@Test | ||
public void interfaceMethodsAreProperlyInvoked() throws Exception | ||
{ | ||
Injector injector = Guice.createInjector(module(new Literals() {})); | ||
Decimals<?> decimals = injector.getInstance(Decimals.class); | ||
Method parseMethod = Provider.class.getMethod("parse", Object.class); | ||
parseMethod.invoke(decimals, ""); | ||
assertTrue(Utilities.nullValueCalled); | ||
} | ||
|
||
private Module module(Literals literals) | ||
{ | ||
return new AbstractModule() | ||
{ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
protected void configure() | ||
{ | ||
Class<?> classDecimals = (Class<?>)Decimals.class; | ||
TypeLiteral<Object> interfaceDecimals = TypeLiteral.get((Class<Object>)classDecimals); | ||
Class<?> implementationNaturals = Projo.getImplementationClass(pro.projo.test.implementations.Decimals.class); | ||
TypeLiteral<Object> implementation = TypeLiteral.get((Class<Object>)implementationNaturals); | ||
bind(interfaceDecimals).to(implementation).asEagerSingleton(); | ||
bind(Literals.class).toInstance(literals); | ||
bind(Ranges.class).toInstance(new Ranges() {}); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.