Wiki Markup |
---|
In a JVM, "Two classes are the same class (and consequently the same type) if they are loaded by the same class loader, and they have the same fully qualified name" \[[JVMSpec 1999|AA. Bibliography#JVMSpec 99]\]. ATwo classclasses with the same name but a different package namenames isare differentdistinct, as andare atwo classclasses with the same fully qualified name but which has been loaded by a different class loader is also differentloaders. |
It could be necessary to check whether a given object has a specific class type or whether two objects have the same class type associated with them, for example, when implementing the equals()
method. If the comparison is performed incorrectly, the code could assume that the two objects are of the same class when they are not. As a result, class names must not be compared.
...
Conversely, the assumption that two classes deriving from the same code base are themselves the same is error - prone. While this assumption is commonly observed to be true in desktop applications, it is typically not the case with J2EE servlet containers. The containers can use different class loader instances to deploy and recall applications at runtime , without having to restart the JVM. In such situations, two objects whose classes come from the same code base could appear to the JVM to be two different classes. Also note that the equals()
method might not return true
when comparing objects originating from the same code base.
...
This noncompliant code example compares the name of the class (Auth
) of object auth
to the string "com.application.auth.DefaultAuthenticationHandler"
and branches on the result of the comparison.
Code Block | ||
---|---|---|
| ||
// Determine whether object auth has required/expected class name
if (auth.getClass().getName().equals(
"com.application.auth.DefaultAuthenticationHandler")) {
// ...
}
|
...
This compliant solution compares the class object of class Auth
auth
to the class object of the class that the current class loader loads, instead of comparing just the class names.
Code Block | ||
---|---|---|
| ||
// Determine whether object authh has required/expected class objectname if (auth.getClass() == this.getClass().getClassLoader().loadClass( "com.application.auth.DefaultAuthenticationHandler")) { // ... } |
...
This noncompliant code example compares the names of the class objects of x
and y
using the equals()
method. Again, it is possible that x
and y
are distinct classes with the same name , if they come from different class loaders.
...
Related Guidelines
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a95349b104a2c95c-a152b7d7-409c4617-97fe841f-774aef624758c6ac2a23049a"><ac:plain-text-body><![CDATA[ | [[Christudas 2005 | AA. Bibliography#Christudas 05]] | Internals of Java Class Loading | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4e8e24f9e45db2dd-7969ee12-4acb45bb-84089d74-45f167c9ea3dc1afc6147027"><ac:plain-text-body><![CDATA[ | [[JVMSpec 1999 | AA. Bibliography#JVMSpec 99]] | [§2.8.1, Class Names | http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0d52729f00baf47b-8bb33df9-47d04a9f-aa0baa7f-6501b013ddade49df795e186"><ac:plain-text-body><![CDATA[ | [[McGraw 1998 | AA. Bibliography#Mcgraw 98]] | Twelve rules for developing more secure Java code Developing More Secure Java Code | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4b163e2fe27530c1-1db10a65-49a74dc5-b3219b72-9018f1e7a3d3a61afe044efe"><ac:plain-text-body><![CDATA[ | [[Wheeler 2003 | AA. Bibliography#Wheeler 03]] | [Java | http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/java.html] Java Secure programming Programming for Linux and Unix UNIX HOWTO | ]]></ac:plain-text-body></ac:structured-macro> |
...