The spec says:-
The hash code of an annotation member is
33(hash code of member name) + hash code of member value
Please find the code below:-
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public @interface SampleAnnotations {
String id();
}
@SampleAnnotations(id="It is the simple test") public class LibSupportTest1 {
public void assertion1() {
Annotation sm_ref1 = LibSupportTest1.class.getAnnotation(SampleAnnotations.class);
int auto_hashCode = sm_ref1.hashCode();
int manual_hashCode = assertion2();
int diff = auto_hashCode - manual_hashCode;
if(diff == 0) {
System.out.println("The test passed");
} else {
System.out.println("The auto_hashCode is :"+auto_hashCode);
System.out.println("The manual_hashCode is :"+manual_hashCode);
System.out.println("The test failed diff in hashCode is :"+diff);
}
}
public int assertion2() {
// computing hasCode according to the specified algo
String member_value = "It is the simple test";
String member_name = "id";
int code = 33*member_name.hashCode() + member_value.hashCode();
return code;
}
public static void main(String args[]) {
LibSupportTest1 ref = new LibSupportTest1();
ref.assertion1();
}
}
Output on Solaris-9 using Tiger-beta2 build41 :-
The auto_hashCode is :-2113679460
The manual_hashCode is :-2113679477
The test failed diff in hashCode is :17
The hash code of an annotation member is
33(hash code of member name) + hash code of member value
Please find the code below:-
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public @interface SampleAnnotations {
String id();
}
@SampleAnnotations(id="It is the simple test") public class LibSupportTest1 {
public void assertion1() {
Annotation sm_ref1 = LibSupportTest1.class.getAnnotation(SampleAnnotations.class);
int auto_hashCode = sm_ref1.hashCode();
int manual_hashCode = assertion2();
int diff = auto_hashCode - manual_hashCode;
if(diff == 0) {
System.out.println("The test passed");
} else {
System.out.println("The auto_hashCode is :"+auto_hashCode);
System.out.println("The manual_hashCode is :"+manual_hashCode);
System.out.println("The test failed diff in hashCode is :"+diff);
}
}
public int assertion2() {
// computing hasCode according to the specified algo
String member_value = "It is the simple test";
String member_name = "id";
int code = 33*member_name.hashCode() + member_value.hashCode();
return code;
}
public static void main(String args[]) {
LibSupportTest1 ref = new LibSupportTest1();
ref.assertion1();
}
}
Output on Solaris-9 using Tiger-beta2 build41 :-
The auto_hashCode is :-2113679460
The manual_hashCode is :-2113679477
The test failed diff in hashCode is :17