-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Instead of using a Spring-based interceptor, the new integration uses the standard JGiven interceptor using ByteBuddy. - The implementation is based on using a BeanFactoryPostProcessor that replaces the original Stage bean class with the one created by ByteBuddy Fixes #259
- Loading branch information
Jan Schäfer
committed
Dec 11, 2016
1 parent
ac7b0b8
commit 80183a0
Showing
14 changed files
with
191 additions
and
191 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
43 changes: 24 additions & 19 deletions
43
jgiven-core/src/main/java/com/tngtech/jgiven/impl/ByteBuddyStageCreator.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 |
---|---|---|
@@ -1,47 +1,52 @@ | ||
package com.tngtech.jgiven.impl; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.any; | ||
|
||
import com.tngtech.jgiven.impl.intercept.ByteBuddyMethodInterceptor; | ||
import com.tngtech.jgiven.impl.intercept.StageInterceptorInternal; | ||
import com.tngtech.jgiven.impl.intercept.StepInterceptor; | ||
|
||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | ||
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy; | ||
import net.bytebuddy.implementation.MethodDelegation; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.any; | ||
|
||
public class ByteBuddyStageCreator implements StageCreator { | ||
|
||
@SuppressWarnings( "unchecked" ) | ||
@Override | ||
public <T> T createStage(Class<T> stageClass, StepInterceptor stepInterceptor) { | ||
public <T> T createStage( Class<T> stageClass, StepInterceptor stepInterceptor ) { | ||
try { | ||
T result = new ByteBuddy() | ||
.subclass(stageClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING) | ||
.method(any()) | ||
.intercept(MethodDelegation.to(new ByteBuddyMethodInterceptor(stepInterceptor))) | ||
.make() | ||
.load(getClassLoader(stageClass), | ||
getClassLoadingStrategy(stageClass)) | ||
.getLoaded() | ||
.newInstance(); | ||
T result = createStageClass( stageClass, stepInterceptor ) | ||
.newInstance(); | ||
return result; | ||
} catch (Error e) { | ||
} catch( Error e ) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error while trying to create an instance of class "+stageClass, e); | ||
} catch( Exception e ) { | ||
throw new RuntimeException( "Error while trying to create an instance of class " + stageClass, e ); | ||
} | ||
} | ||
|
||
protected ClassLoadingStrategy getClassLoadingStrategy(Class<?> stageClass) { | ||
return getClassLoader(stageClass) == null | ||
public <T> Class<? extends T> createStageClass( Class<T> stageClass, StepInterceptor stepInterceptor ) { | ||
return new ByteBuddy() | ||
.subclass( stageClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING ) | ||
.implement( StageInterceptorInternal.class ) | ||
.method( any() ) | ||
.intercept( MethodDelegation.to( new ByteBuddyMethodInterceptor( stepInterceptor ) ) ) | ||
.make() | ||
.load( getClassLoader( stageClass ), | ||
getClassLoadingStrategy( stageClass ) ) | ||
.getLoaded(); | ||
} | ||
|
||
protected ClassLoadingStrategy getClassLoadingStrategy( Class<?> stageClass ) { | ||
return getClassLoader( stageClass ) == null | ||
? ClassLoadingStrategy.Default.WRAPPER | ||
: ClassLoadingStrategy.Default.INJECTION; | ||
} | ||
|
||
protected ClassLoader getClassLoader(Class<?> stageClass) { | ||
protected ClassLoader getClassLoader( Class<?> stageClass ) { | ||
return Thread.currentThread().getContextClassLoader(); | ||
} | ||
|
||
|
||
} |
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
5 changes: 5 additions & 0 deletions
5
jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/StageInterceptorInternal.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,5 @@ | ||
package com.tngtech.jgiven.impl.intercept; | ||
|
||
public interface StageInterceptorInternal { | ||
void setStepInterceptor( StepInterceptor stepInterceptor ); | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...g/src/main/java/com/tngtech/jgiven/integration/spring/JGivenBeanFactoryPostProcessor.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,46 @@ | ||
package com.tngtech.jgiven.integration.spring; | ||
|
||
import com.tngtech.jgiven.impl.ByteBuddyStageCreator; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.FatalBeanException; | ||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
|
||
import java.util.Iterator; | ||
|
||
|
||
public class JGivenBeanFactoryPostProcessor implements BeanFactoryPostProcessor { | ||
|
||
private final ByteBuddyStageCreator buddyStageCreator = new ByteBuddyStageCreator(); | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
Iterator<String> beanNames = beanFactory.getBeanNamesIterator(); | ||
while (beanNames.hasNext()) { | ||
String beanName = beanNames.next(); | ||
if (beanFactory.containsBeanDefinition(beanName)) { | ||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); | ||
if (beanDefinition instanceof AnnotatedBeanDefinition) { | ||
AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition; | ||
if (annotatedBeanDefinition.getMetadata().hasAnnotation(JGivenStage.class.getName())) { | ||
String className = beanDefinition.getBeanClassName(); | ||
Class<?> stageClass = createStageClass(beanName, className); | ||
beanDefinition.setBeanClassName(stageClass.getName()); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
private Class<?> createStageClass(String beanName, String className) { | ||
try { | ||
Class<?> aClass = Thread.currentThread().getContextClassLoader().loadClass(className); | ||
return buddyStageCreator.createStageClass(aClass, null); | ||
} catch (ClassNotFoundException e) { | ||
throw new FatalBeanException("Error while trying to create JGiven stage for bean "+beanName, e); | ||
} | ||
} | ||
} |
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
37 changes: 0 additions & 37 deletions
37
...ring/src/main/java/com/tngtech/jgiven/integration/spring/JGivenStageAutoProxyCreator.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.