cache references and reuse them

getReference static method
constructor is private
all members are final
setXXX removed


git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@719 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent 9be3b7e0d3
commit 59d1cb7a24
  1. 61
      jode/jode/bytecode/Reference.java

@ -18,9 +18,11 @@
*/ */
package jode.bytecode; package jode.bytecode;
import java.util.Vector; ///#ifdef JDK12
import java.util.Enumeration; ///import java.lang.ref.WeakReference;
import jode.type.Type; ///import java.lang.ref.ReferenceQueue;
///#endif
import java.util.*;
/** /**
* This class represents a field or method reference * This class represents a field or method reference
@ -32,26 +34,52 @@ public class Reference {
/** /**
* The class info. * The class info.
*/ */
String className; final String className;
/** /**
* The member name. Don't make this a MethodInfo, since the clazz * The member name. Don't make this a MethodInfo, since the clazz
* may not be readable. * may not be readable.
*/ */
String memberName; final String memberName;
/** /**
* The member type. * The member type.
*/ */
String memberType; final String memberType;
/** /**
* The cached hash code * The cached hash code
*/ */
int cachedHashCode; final int cachedHashCode;
public Reference(String className, String name, String type) { ///#ifdef JDK12
/// private static final Map references = new WeakHashMap();
///#else
private static final Hashtable references = new Hashtable();
///#endif
public static Reference getReference(String className,
String name, String type) {
Reference reference = new Reference(className, name, type);
///#ifdef JDK12
/// WeakReference ref = (WeakReference) references.get(reference);
/// Reference cachedRef = (ref == null) ? null : (Reference) ref.get();
///#else
Reference cachedRef = (Reference) references.get(reference);
///#endif
if (cachedRef == null) {
///#ifdef JDK12
/// references.put(reference, new WeakReference(reference));
///#else
references.put(reference, reference);
///#endif
return reference;
}
return cachedRef;
}
private Reference(String className, String name, String type) {
this.className = className.intern(); this.className = className.intern();
this.memberName = name.intern(); this.memberName = name.intern();
this.memberType = type.intern(); this.memberType = type.intern();
cachedHashCode = this.cachedHashCode =
className.hashCode() ^ name.hashCode() ^ type.hashCode(); className.hashCode() ^ name.hashCode() ^ type.hashCode();
} }
@ -67,18 +95,6 @@ public class Reference {
return memberType; return memberType;
} }
public void setClazz(String name) {
className = name;
}
public void setName(String name) {
memberName = name;
}
public void setType(String type) {
memberType = type;
}
public String toString() { public String toString() {
return className + " " + memberName + " " + memberType; return className + " " + memberName + " " + memberType;
} }
@ -86,7 +102,8 @@ public class Reference {
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof Reference) { if (o instanceof Reference) {
Reference other = (Reference) o; Reference other = (Reference) o;
return other.className.equals(className) return other.cachedHashCode == cachedHashCode
&& other.className.equals(className)
&& other.memberName.equals(memberName) && other.memberName.equals(memberName)
&& other.memberType.equals(memberType); && other.memberType.equals(memberType);
} }

Loading…
Cancel
Save