hash code patches from Y. Haviv

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3155 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2009-01-05 14:00:43 +00:00
parent 37c5e8459c
commit 552e754506
6 changed files with 264 additions and 160 deletions

View File

@ -44,6 +44,33 @@ public abstract class CallSiteReference extends ProgramCounter implements Byteco
this.declaredTarget = declaredTarget;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((declaredTarget == null) ? 0 : declaredTarget.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
CallSiteReference other = (CallSiteReference) obj;
if (declaredTarget == null) {
if (other.declaredTarget != null)
return false;
} else if (!declaredTarget.equals(other.declaredTarget))
return false;
return true;
}
// the following atrocities are needed to save a word of space by
// declaring these classes static, so they don't keep a pointer
// to the enclosing environment
@ -200,4 +227,8 @@ public abstract class CallSiteReference extends ProgramCounter implements Byteco
public CallSiteReference cloneReference(int pc) {
return make(pc, getDeclaredTarget(), getInvocationCode());
}
}

View File

@ -214,6 +214,39 @@ public class NestedJarFileModule implements Module {
return FileSuffixes.isSourceFile(getName());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Entry other = (Entry) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
private NestedJarFileModule getOuterType() {
return NestedJarFileModule.this;
}
}
@Override
@ -221,4 +254,31 @@ public class NestedJarFileModule implements Module {
return "Nested Jar File:" + entry.getName();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NestedJarFileModule other = (NestedJarFileModule) obj;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
return true;
}
}

View File

@ -148,10 +148,9 @@ public class SSAInvokeInstruction extends SSAAbstractInvokeInstruction {
@Override
public int hashCode() {
return site.hashCode() * 7529;
return (site.hashCode() * 7529) + (exception * 9823);
}
@Override
public Collection<TypeReference> getExceptionTypes() {
Collection<TypeReference> empty = Collections.emptySet();

View File

@ -16,6 +16,7 @@ import java.util.Map;
import com.ibm.wala.annotations.Internal;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.debug.UnimplementedError;
/**
*
@ -50,17 +51,21 @@ public class ParanoidHashMap<K, V> extends LinkedHashMap<K, V> {
*/
@Override
public V put(K arg0, V arg1) {
if (arg0 != null && arg0.hashCode() == System.identityHashCode(arg0)) {
assertOverridesHashCode(arg0);
return super.put(arg0, arg1);
}
public static void assertOverridesHashCode(Object o) throws UnimplementedError {
if (o != null && o.hashCode() == System.identityHashCode(o)) {
try {
Method method = arg0.getClass().getMethod("hashCode");
Method method = o.getClass().getMethod("hashCode");
if (method.getDeclaringClass() == Object.class){
Assertions._assert(false, arg0.getClass().toString());
Assertions._assert(false, o.getClass().toString());
}
} catch (Exception e) {
Assertions._assert(false, "Could not find hashCode method");
}
}
return super.put(arg0, arg1);
}
/*

View File

@ -78,9 +78,8 @@ public class ParanoidHashSet<T> extends LinkedHashSet<T> {
if (arg0 == null) {
throw new IllegalArgumentException("arg0 is null");
}
if (arg0.hashCode() == System.identityHashCode(arg0)) {
Assertions._assert(false, arg0.getClass().toString());
}
ParanoidHashMap.assertOverridesHashCode(arg0);
boolean result = super.add(arg0);
if (result) {
nAdded++;
@ -104,5 +103,4 @@ public class ParanoidHashSet<T> extends LinkedHashSet<T> {
}
return result;
}
}

View File

@ -1,150 +1,161 @@
/*******************************************************************************
* Copyright (c) 2008 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.j2ee;
import java.util.Collection;
import java.util.Collections;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IField;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.classLoader.SyntheticClass;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.ipa.summaries.BypassMethodTargetSelector;
import com.ibm.wala.ipa.summaries.MethodSummary;
import com.ibm.wala.ipa.summaries.SummarizedMethod;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.Descriptor;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.Selector;
import com.ibm.wala.types.TypeName;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.strings.Atom;
/**
* Method representing a factory that creates ActionForm objects. We extend {@link SummarizedMethod} to allow for re-use
* of the machinery in FactoryBypassInterpreter.
*
* @author manu
*
*/
public class ActionFormFactoryMethod extends SummarizedMethod {
public static final Atom name = Atom.findOrCreateUnicodeAtom("makeActionForm");
public static final Descriptor descr = Descriptor.findOrCreateUTF8("()" + StrutsEntrypoints.actionFormName + ";");
public static final TypeReference factoryClassRef = TypeReference.findOrCreate(ClassLoaderReference.Primordial, TypeName
.string2TypeName("Lcom/ibm/wala/FakeActionFormFactoryClass"));
public static final MethodReference ref = MethodReference.findOrCreate(factoryClassRef, name, descr);
public ActionFormFactoryMethod(IClassHierarchy cha) {
super(ref, makeNoOpFactorySummary(), new FakeActionFormFactoryClass(cha));
}
private static MethodSummary makeNoOpFactorySummary() {
MethodSummary noOpSummary = BypassMethodTargetSelector.generateNoOp(ref, true);
noOpSummary.setFactory(true);
return noOpSummary;
}
private static class FakeActionFormFactoryClass extends SyntheticClass {
public FakeActionFormFactoryClass(IClassHierarchy cha) {
super(factoryClassRef, cha);
}
public Collection<IField> getAllFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IClass> getAllImplementedInterfaces() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IField> getAllInstanceFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IMethod> getAllMethods() throws ClassHierarchyException {
// TODO Auto-generated method stub
assert false;
return null;
}
public Collection<IField> getAllStaticFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public IMethod getClassInitializer() {
return null;
}
public Collection<IField> getDeclaredInstanceFields() {
return Collections.emptySet();
}
public Collection<IMethod> getDeclaredMethods() {
// TODO Auto-generated method stub
assert false;
return null;
}
public Collection<IField> getDeclaredStaticFields() {
return Collections.emptySet();
}
public Collection<IClass> getDirectInterfaces() throws ClassHierarchyException {
return Collections.emptySet();
}
public IField getField(Atom name) {
// TODO Auto-generated method stub
assert false;
return null;
}
public IMethod getMethod(Selector selector) {
// TODO Auto-generated method stub
assert false;
return null;
}
public int getModifiers() {
// TODO Auto-generated method stub
assert false;
return 0;
}
public TypeName getName() {
return factoryClassRef.getName();
}
public IClass getSuperclass() throws ClassHierarchyException {
return null;
}
public boolean isPublic() {
// TODO Auto-generated method stub
assert false;
return false;
}
public boolean isReferenceType() {
// TODO Auto-generated method stub
assert false;
return false;
}
}
}
/*******************************************************************************
* Copyright (c) 2008 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.j2ee;
import java.util.Collection;
import java.util.Collections;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IField;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.classLoader.SyntheticClass;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.ipa.summaries.BypassMethodTargetSelector;
import com.ibm.wala.ipa.summaries.MethodSummary;
import com.ibm.wala.ipa.summaries.SummarizedMethod;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.Descriptor;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.Selector;
import com.ibm.wala.types.TypeName;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.strings.Atom;
/**
* Method representing a factory that creates ActionForm objects. We extend {@link SummarizedMethod} to allow for re-use
* of the machinery in FactoryBypassInterpreter.
*
* @author manu
*
*/
public class ActionFormFactoryMethod extends SummarizedMethod {
public static final Atom name = Atom.findOrCreateUnicodeAtom("makeActionForm");
public static final Descriptor descr = Descriptor.findOrCreateUTF8("()" + StrutsEntrypoints.actionFormName + ";");
public static final TypeReference factoryClassRef = TypeReference.findOrCreate(ClassLoaderReference.Primordial, TypeName
.string2TypeName("Lcom/ibm/wala/FakeActionFormFactoryClass"));
public static final MethodReference ref = MethodReference.findOrCreate(factoryClassRef, name, descr);
public ActionFormFactoryMethod(IClassHierarchy cha) {
super(ref, makeNoOpFactorySummary(), new FakeActionFormFactoryClass(cha));
}
private static MethodSummary makeNoOpFactorySummary() {
MethodSummary noOpSummary = BypassMethodTargetSelector.generateNoOp(ref, true);
noOpSummary.setFactory(true);
return noOpSummary;
}
private static class FakeActionFormFactoryClass extends SyntheticClass {
public FakeActionFormFactoryClass(IClassHierarchy cha) {
super(factoryClassRef, cha);
}
public Collection<IField> getAllFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IClass> getAllImplementedInterfaces() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IField> getAllInstanceFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public Collection<IMethod> getAllMethods() throws ClassHierarchyException {
// TODO Auto-generated method stub
assert false;
return null;
}
public Collection<IField> getAllStaticFields() throws ClassHierarchyException {
return Collections.emptySet();
}
public IMethod getClassInitializer() {
return null;
}
public Collection<IField> getDeclaredInstanceFields() {
return Collections.emptySet();
}
public Collection<IMethod> getDeclaredMethods() {
// TODO Auto-generated method stub
assert false;
return null;
}
public Collection<IField> getDeclaredStaticFields() {
return Collections.emptySet();
}
public Collection<IClass> getDirectInterfaces() throws ClassHierarchyException {
return Collections.emptySet();
}
public IField getField(Atom name) {
// TODO Auto-generated method stub
assert false;
return null;
}
public IMethod getMethod(Selector selector) {
// TODO Auto-generated method stub
assert false;
return null;
}
public int getModifiers() {
// TODO Auto-generated method stub
assert false;
return 0;
}
public TypeName getName() {
return factoryClassRef.getName();
}
public IClass getSuperclass() throws ClassHierarchyException {
return null;
}
public boolean isPublic() {
// TODO Auto-generated method stub
assert false;
return false;
}
public boolean isReferenceType() {
// TODO Auto-generated method stub
assert false;
return false;
}
@Override
public int hashCode() {
return super.hashCode(); // like a singleton
}
@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
}