Когда объект создается в Java? ⇐ JAVA
-
Anonymous
Когда объект создается в Java?
I can't believe it wasn't asked already, it sounds very basic, but I searched on SO and haven't found such a question (it's not this one since it's actually about the order of invoked constructors)
When is an object created in Java (from the GC's standpoint)?
I didn't give it much thought at first. "At least, no sooner than the constructor finishes execution," I figured. Then I discovered that Mockito uses some esoteric native technology to bypass constructors at all
And then I read about finalizers. I checked and confirm that this code
public class FinalizerAttackExample { public static void main(String[] args) throws InterruptedException { try { new MaliciousSubclass(); } catch(SecurityException ex) { System.out.println("wouldn't get hands on a ResourceClass instance"); } System.gc(); Thread.sleep(2000); } static class ResourceClass { ResourceClass() { if(!checkCaller()) throw new SecurityException(); } public void criticalAction() { System.out.println("ResourceClass.criticalAction()"); } } /** For our demonstration, all callers are invalid */ static boolean checkCaller() { return false; } static class MaliciousSubclass extends ResourceClass { @Override protected void finalize() { System.out.println("see, I got hands on " + this); criticalAction(); } } } indeed produces this output (the hash may be different)
wouldn't get hands on a ResourceClass instance see, I got hands on FinalizerAttackExample$MaliciousSubclass@7ad74083 ResourceClass.criticalAction() Which begs the question: what object was finalize() invoked on if the constructor never successfully completed? Is a Java object created in an imaginary "finally" block of a constructor? Is it created beforehand? Here's what the specification says
A new class instance is explicitly created when evaluation of a class instance creation expression (§15.9) causes a class to be instantiated.
JLS, §12.5
It doesn't clear things up at all
If it happens to be JVM-specific (which it shouldn't be), let's assume we're running our code on Hotspot
Источник: https://stackoverflow.com/questions/780 ... ed-in-java
I can't believe it wasn't asked already, it sounds very basic, but I searched on SO and haven't found such a question (it's not this one since it's actually about the order of invoked constructors)
When is an object created in Java (from the GC's standpoint)?
I didn't give it much thought at first. "At least, no sooner than the constructor finishes execution," I figured. Then I discovered that Mockito uses some esoteric native technology to bypass constructors at all
And then I read about finalizers. I checked and confirm that this code
public class FinalizerAttackExample { public static void main(String[] args) throws InterruptedException { try { new MaliciousSubclass(); } catch(SecurityException ex) { System.out.println("wouldn't get hands on a ResourceClass instance"); } System.gc(); Thread.sleep(2000); } static class ResourceClass { ResourceClass() { if(!checkCaller()) throw new SecurityException(); } public void criticalAction() { System.out.println("ResourceClass.criticalAction()"); } } /** For our demonstration, all callers are invalid */ static boolean checkCaller() { return false; } static class MaliciousSubclass extends ResourceClass { @Override protected void finalize() { System.out.println("see, I got hands on " + this); criticalAction(); } } } indeed produces this output (the hash may be different)
wouldn't get hands on a ResourceClass instance see, I got hands on FinalizerAttackExample$MaliciousSubclass@7ad74083 ResourceClass.criticalAction() Which begs the question: what object was finalize() invoked on if the constructor never successfully completed? Is a Java object created in an imaginary "finally" block of a constructor? Is it created beforehand? Here's what the specification says
A new class instance is explicitly created when evaluation of a class instance creation expression (§15.9) causes a class to be instantiated.
JLS, §12.5
It doesn't clear things up at all
If it happens to be JVM-specific (which it shouldn't be), let's assume we're running our code on Hotspot
Источник: https://stackoverflow.com/questions/780 ... ed-in-java
Мобильная версия