第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > java中enhancer试什么_Java Enhancer類代碼示例

java中enhancer试什么_Java Enhancer類代碼示例

时间:2023-07-10 03:35:18

相关推荐

java中enhancer试什么_Java Enhancer類代碼示例

import org.mockito.cglib.proxy.Enhancer; //導入依賴的package包/類

public Class createProxyClass(Class> mockedType, Class>... interfaces) {

if (mockedType == Object.class) {

mockedType = ClassWithSuperclassToWorkAroundCglibBug.class;

}

Enhancer enhancer = new Enhancer() {

@Override

@SuppressWarnings("unchecked")

protected void filterConstructors(Class sc, List constructors) {

// Don't filter

}

};

Class>[] allMockedTypes = prepend(mockedType, interfaces);

enhancer.setClassLoader(bineLoadersOf(allMockedTypes));

enhancer.setUseFactory(true);

if (mockedType.isInterface()) {

enhancer.setSuperclass(Object.class);

enhancer.setInterfaces(allMockedTypes);

} else {

enhancer.setSuperclass(mockedType);

enhancer.setInterfaces(interfaces);

}

enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class, NoOp.class});

enhancer.setCallbackFilter(IGNORE_BRIDGE_METHODS);

if (mockedType.getSigners() != null) {

enhancer.setNamingPolicy(NAMING_POLICY_THAT_ALLOWS_IMPOSTERISATION_OF_CLASSES_IN_SIGNED_PACKAGES);

} else {

enhancer.setNamingPolicy(MockitoNamingPolicy.INSTANCE);

}

enhancer.setSerialVersionUID(42L);

try {

return enhancer.createClass();

} catch (CodeGenerationException e) {

if (Modifier.isPrivate(mockedType.getModifiers())) {

throw new MockitoException("\n"

+ "Mockito cannot mock this class: " + mockedType

+ ".\n"

+ "Most likely it is a private class that is not visible by Mockito");

}

throw new MockitoException("\n"

+ "Mockito cannot mock this class: " + mockedType

+ "\n"

+ "Mockito can only mock visible & non-final classes."

+ "\n"

+ "If you're not sure why you're getting this error, please report to the mailing list.", e);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。