Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About instantiation. #110

Open
LitnhJacuzzi opened this issue Jul 29, 2021 · 3 comments
Open

About instantiation. #110

LitnhJacuzzi opened this issue Jul 29, 2021 · 3 comments

Comments

@LitnhJacuzzi
Copy link

LitnhJacuzzi commented Jul 29, 2021

I recommend you to use sun.misc.Unsafe.allocateInstance(), the Objenesis will be not available when the class is dynamically generated(It causes NoClassDefFoundError like #100 and problems in #45).

@kostaskougios
Copy link
Owner

@Littlenotch-Jacuzzi , I am getting


java.lang.SecurityException: Unsafe

	at jdk.unsupported/sun.misc.Unsafe.getUnsafe(Unsafe.java:99)
	at com.rits.cloning.Cloner.newInstance(Cloner.java:311)
	at com.rits.cloning.Cloner$CloneObjectCloner.deepClone(Cloner.java:618)
	at com.rits.cloning.Cloner.cloneInternal(Cloner.java:447)
	at com.rits.cloning.Cloner.deepClone(Cloner.java:338)
	at com.rits.tests.cloning.TestCloner.testNullInsteadOfCloneAnnotatedFields(TestCloner.java:555)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at junit.framework.TestCase.runTest(TestCase.java:177)
	at junit.framework.TestCase.runBare(TestCase.java:142)
	at junit.framework.TestResult$1.protect(TestResult.java:122)
	at junit.framework.TestResult.runProtected(TestResult.java:142)
	at junit.framework.TestResult.run(TestResult.java:125)
	at junit.framework.TestCase.run(TestCase.java:130)
	at junit.framework.TestSuite.runTest(TestSuite.java:241)
	at junit.framework.TestSuite.run(TestSuite.java:236)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

if I replace objenesis with Unsafe

@LitnhJacuzzi
Copy link
Author

LitnhJacuzzi commented Jul 31, 2021

@kostaskougios
Yes, you can't directly call that method in Unsafe. I wrote a method to solve it:

private static Unsafe hackUnsafe() {
	try {
		Field field = Unsafe.class.getDeclaredField("theUnsafe");
		field.setAccessible(true);
		return (Unsafe) field.get(null);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

Use this method to replace the Unsafe.getUnsafe() method.

@LitnhJacuzzi
Copy link
Author

LitnhJacuzzi commented Aug 8, 2021

Note that the efficiency of the Unsafe.allocateInstance() method is about 2000 times that of Objenesis. But the instantiated objects has not gone any initialization, and the fields in these objects are all null(reference type) or default values(basic type).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants