本文整理汇总了Java中org.objenesis.Objenesis类的典型用法代码示例。如果您正苦于以下问题:Java Objenesis类的具体用法?Java Objenesis怎么用?Java Objenesis使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Objenesis类属于org.objenesis包,在下文中一共展示了Objenesis类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getClient
import org.objenesis.Objenesis; //导入依赖的package包/类
public static Client getClient() {
if (client == null) {
Objenesis objenesis = new ObjenesisStd();
try {
client = (Client) objenesis.newInstance(Context.getInstance().getASMClassLoader().loadClass("oss/iIIiiiiIiI"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return client;
}
示例2: newInstance
import org.objenesis.Objenesis; //导入依赖的package包/类
/**
* Create a new instance of a class without invoking its constructor.
* <p>
* No byte-code manipulation is needed to perform this operation and thus
* it's not necessary use the <code>PowerMockRunner</code> or
* <code>PrepareForTest</code> annotation to use this functionality.
*
* @param <T>
* The type of the instance to create.
* @param classToInstantiate
* The type of the instance to create.
* @return A new instance of type T, created without invoking the
* constructor.
*/
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> classToInstantiate) {
int modifiers = classToInstantiate.getModifiers();
final Object object;
if (Modifier.isInterface(modifiers)) {
object = Proxy.newProxyInstance(WhiteboxImpl.class.getClassLoader(), new Class<?>[] { classToInstantiate },
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return TypeUtils.getDefaultValue(method.getReturnType());
}
});
} else if (classToInstantiate.isArray()) {
object = Array.newInstance(classToInstantiate.getComponentType(), 0);
} else if (Modifier.isAbstract(modifiers)) {
throw new IllegalArgumentException(
"Cannot instantiate an abstract class. Please use the ConcreteClassGenerator in PowerMock support to generate a concrete class first.");
} else {
Objenesis objenesis = new ObjenesisStd();
ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(classToInstantiate);
object = thingyInstantiator.newInstance();
}
return (T) object;
}
示例3: setup
import org.objenesis.Objenesis; //导入依赖的package包/类
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
// prepare mocks
this.noteStoreOperations = mock(NoteStoreOperations.class, withSettings().extraInterfaces(StoreClientHolder.class));
this.userStoreOperations = mock(UserStoreOperations.class, withSettings().extraInterfaces(StoreClientHolder.class));
// To work around getClass() method to return actual store-client class for parameter name discovery, use
// objenesis to create actual impl class instead of mocking.
// mockito cannot mock getClass() since this method is final.
Objenesis objenesis = new ObjenesisStd();
UserStoreClient userStoreClient = (UserStoreClient) objenesis.newInstance(UserStoreClient.class);
NoteStoreClient noteStoreClient = (NoteStoreClient) objenesis.newInstance(NoteStoreClient.class);
when(((StoreClientHolder) userStoreOperations).getStoreClient()).thenReturn(userStoreClient);
when(((StoreClientHolder) noteStoreOperations).getStoreClient()).thenReturn(noteStoreClient);
when(this.evernote.userStoreOperations()).thenReturn(userStoreOperations);
when(this.evernote.noteStoreOperations()).thenReturn(noteStoreOperations);
}
开发者ID:ttddyy,项目名称:evernote-rest-webapp,代码行数:21,代码来源:AbstractStoreOperationControllerIntegrationTest.java
示例4: createUnderlyingObjectProxy
import org.objenesis.Objenesis; //导入依赖的package包/类
private Object createUnderlyingObjectProxy(InvocationReporter invocationReporter) {
// use Cglib Enhancer inlined in spring3.2
// spring3.2's ProxyFactory cannot creates a proxy with constructor arguments.
// CglibAopProxy is package scoped and cannot call "setConstructorArguments" operationMethod.
// This is fixed in spring4 with ObjenesisCglibAopProxy: https://jira.springsource.org/browse/SPR-3150
// for now simulate what spring4 does with cglib and objenesis.
final Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(implClass);
enhancer.setInterfaces(implClass.getInterfaces());
Objenesis objenesis = new ObjenesisStd();
Callback[] callbacks = new Callback[]{invocationReporter};
Class<?>[] types = new Class[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
enhancer.setCallbackTypes(types);
Object client = objenesis.newInstance(enhancer.createClass());
((Factory) client).setCallbacks(callbacks);
return client;
}
示例5: main
import org.objenesis.Objenesis; //导入依赖的package包/类
public static void main(String[] args) {
Objenesis objenesis = new ObjenesisStd();
FinalBean o = objenesis.newInstance(FinalBean.class); // 能成功创建
Reflect.on(o).set("name", "bingoo");
System.out.println(o);
// 异常:com.alibaba.fastjson.JSONException: default constructor not found. class org.n3r.sandbox.objenesis.FinalClassCreate$FinalBean
FinalBean finalBean = JSON.parseObject("{name:\"dingoo\"}", FinalBean.class);
System.out.println(finalBean);
}
示例6: create
import org.objenesis.Objenesis; //导入依赖的package包/类
public T create()
{
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setCallbackType(MethodInterceptor.class);
Class<?> proxyType = enhancer.createClass();
Objenesis objenesis = new ObjenesisStd();
Factory proxy = (Factory) objenesis.newInstance(proxyType);
proxy.setCallbacks(new Callback[] {this});
return type.cast(proxy);
}
示例7: startTests
import org.objenesis.Objenesis; //导入依赖的package包/类
@Override
public void startTests(String platformDescription, Objenesis objenesisStandard, Objenesis objenesisSerializer) {
this.platformDescription = platformDescription;
this.objenesisStandard = objenesisStandard;
this.objenesisSerializer = objenesisSerializer;
this.currentCandidate = null;
this.errorCount = 0;
this.results.clear();
this.startTime = System.currentTimeMillis();
}
示例8: isCompliant
import org.objenesis.Objenesis; //导入依赖的package包/类
@Override
public boolean isCompliant(Objenesis objenesis) {
try {
objenesis.newInstance(NotSerializable.class);
}
catch(ObjenesisException e) {
return true;
}
return false;
}
示例9: TCK
import org.objenesis.Objenesis; //导入依赖的package包/类
/**
* @param objenesisStandard Objenesis instance used to instantiate classes the standard way (no constructor called)
* @param objenesisSerializer Objenesis instance used to instantiate classes in a serialization compliant way (first not serializable constructor called)
* @param reporter Where to report the results of the tests to
*/
public TCK(Objenesis objenesisStandard, Objenesis objenesisSerializer, Reporter reporter) {
this.objenesisStandard = objenesisStandard;
this.objenesisSerializer = objenesisSerializer;
this.reporter = reporter;
try {
loadCandidates();
}
catch(IOException e) {
throw new RuntimeException(e);
}
Collections.sort(candidates);
}
示例10: runTest
import org.objenesis.Objenesis; //导入依赖的package包/类
private void runTest(Reporter reporter, Class<?> candidate, Objenesis objenesis, Candidate.CandidateType type) {
if(Feature.class.isAssignableFrom(candidate)) {
runFeature(reporter, candidate, objenesis, type);
}
else {
runCandidate(reporter, candidate, objenesis, type);
}
}
示例11: runFeature
import org.objenesis.Objenesis; //导入依赖的package包/类
private void runFeature(Reporter reporter, Class<?> clazz, Objenesis objenesis, Candidate.CandidateType type) {
try {
@SuppressWarnings("unchecked") Constructor<Feature> constructor = (Constructor<Feature>) clazz.getConstructor();
Feature feature = constructor.newInstance();
boolean compliant = feature.isCompliant(objenesis);
reporter.result(type, compliant);
}
catch(Exception e) {
reporter.exception(type, e);
}
}
示例12: runCandidate
import org.objenesis.Objenesis; //导入依赖的package包/类
private void runCandidate(Reporter reporter, Class<?> candidate, Objenesis objenesis, Candidate.CandidateType type) {
try {
Object instance = objenesis.newInstance(candidate);
boolean success = instance != null && instance.getClass() == candidate;
reporter.result(type, success);
}
catch(Exception e) {
reporter.exception(type, e);
}
}
示例13: config
import org.objenesis.Objenesis; //导入依赖的package包/类
@Configuration
public Option[] config() {
String version = getImplementationVersion(Objenesis.class);
return options(
bundle("file:../main/target/objenesis-" + version + ".jar"),
junitBundles()
);
}
示例14: getFakeParameters
import org.objenesis.Objenesis; //导入依赖的package包/类
private Object[] getFakeParameters(Method method) {
Objenesis objenesis = new ObjenesisStd();
Class<?>[] paramTypes = method.getParameterTypes();
Object[] fakeParams = new Object[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
Class<?> paramType = paramTypes[i];
// TODO: better impl.
Object fakeParam;
if (ClassUtils.isPrimitiveArray(paramType)) {
// can objenesis handle primitive array? for now, create just empty array
fakeParam = Array.newInstance(paramType.getComponentType(), 0);
} else {
if (paramType.isPrimitive()) {
paramType = ClassUtils.resolvePrimitiveIfNecessary(paramType);
} else if (paramType.isInterface()) {
if (paramType.isAssignableFrom(List.class)) {
paramType = ArrayList.class;
}
}
fakeParam = objenesis.newInstance(paramType);
}
fakeParams[i] = fakeParam;
}
return fakeParams;
}
示例15: constructInstance
import org.objenesis.Objenesis; //导入依赖的package包/类
private static Object constructInstance(Class<?> clazz) {
/**
* Use the objensis magic (like PowerMock/EasyMock) to construct an instance of a Java
* object without invoking a constructor. This is a local proxy, and it shouldn't
* run code locally. We only inherit in concrete class cases to allow a proper
* programming interface in the local classloader.
*/
Objenesis objenesis = new ObjenesisStd();
return objenesis.newInstance(clazz);
}