'volatile' in method signature?

This is strange. I have the following code:

class A { protected A clone() throws CloneNotSupportedException { return (A) super.clone(); } } 

when I decompiled its bytecode through 'showmycode.com', it showed me the following code:

 class A { A() { } protected A clone() throws clonenotsupportedexception { return (A)super.clone(); } protected volatile object clone() throws clonenotsupportedexception { return clone(); } } 

What does it mean that the type of the returned method will be unstable in the second clone method? (This code was compiled using the JDK 1.6 compiler by default, Eclipse).

+6
source share
4 answers

The modifier mask for fields and methods is similar, but not exactly the same. The decompiler most likely uses the toString method here

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/Modifier.java

but what he does not do is process all the bits

 // Bits not (yet) exposed in the public API either because they // have different meanings for fields and methods and there is no // way to distinguish between the two in this class, or because // they are not Java programming language keywords 

What doesn't handle it are the bits, which can mean synthetic and bridge , which define the code generated by the compiler.

If volatile means something at all here, it may mean that you do not delete this method, even if it does nothing.

+4
source

This answer has already been addressed in the question Why use the volatile method in java? But here is some more information.

When you overload methods (perhaps only general methods in a superclass), the method is marked as a "bridge method" . From java.lang.reflect.Modifier :

 static final int BRIDGE = 0x00000040; 

Unfortunately, this is the same bit that is used to designate fields as volatile :

 public static final int VOLATILE = 0x00000040; 

If you type modifiers for this method, you will see something like:

 public volatile 

This is a limitation in the Modifiers.toString(int) method, which does not know if it is a field or a method.

 public static String toString(int mod) { StringBuffer sb = new StringBuffer(); ... if ((mod & VOLATILE) != 0) sb.append("volatile "); // no mention of BRIDGE here ... return sb.toString().substring(0, len-1); } 
+8
source

This means nothing. This is a bug in the decompiler. The end of the story.

(The error is probably due to the fact that some flag bits used in the class file format are โ€œoverloadedโ€, which means different things in the context of a class, field or method. I also vaguely remember that there were some โ€œnew usesโ€ in recent versions JVM specifications.)

+4
source

This is a bug in your decompiler.

volatile is only a valid modifier for a field.

I recommend you read this aricle.

+1
source

Source: https://habr.com/ru/post/914444/


All Articles