Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsafe Class-loading Issue?

Tags:

java

unsafe

Unsafe has a method to ensure that classes are initialized:

Unsafe.ensureClassInitialized(Class) line: not available [native method]

I suspect that this type of initialization doesn't lock on the class like regular java class-loading because I've occasionally bumped into some impossible situations. I can give more details later if needed, but does anyone know if class-loading using Unsafe has quirks like that?

Btw, here's a short stack trace of how that class gets loaded up:

Unsafe.ensureClassInitialized(Class) line: not available [native method]               
UnsafeFieldAccessorFactory.newFieldAccessor(Field, boolean) line: 25
ReflectionFactory.newFieldAccessor(Field, boolean) line: 122    
Field.acquireFieldAccessor(boolean) line: 918    
Field.getFieldAccessor(Object) line: 899               
Field.get(Object) line: 358          
like image 908
LazyCubicleMonkey Avatar asked May 10 '11 00:05

LazyCubicleMonkey


Video Answer


1 Answers

For reference and web seaches, it turns out that you can force class initialization without using Unsafe:

Class.forName(cls.getName(), true, cls.getClassLoader());

Not pretty, but it works.

like image 118
JodaStephen Avatar answered Oct 14 '22 03:10

JodaStephen