fix for bug reported by Juergen Graf: handle multiple fields with the same name in a JVM class

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@4552 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
msridhar1 2012-04-03 22:26:12 +00:00
parent 7fe479ee82
commit 1d28b2dc26
9 changed files with 138 additions and 11 deletions

View File

@ -153,6 +153,10 @@ abstract public class AstClass implements IClass, ClassConstants {
}
}
public IField getField(Atom name, TypeName type) {
// assume that for AST classes, you can't have multiple fields with the same name
return getField(name);
}
public Collection<IMethod> getDeclaredMethods() {
return declaredMethods.values();
}

View File

@ -117,6 +117,11 @@ abstract public class AstFunctionClass implements IClass, ClassConstants {
return loader.lookupClass(superReference.getName()).getField(name);
}
public IField getField(Atom name, TypeName type) {
// assume that for AST classes, you can't have multiple fields with the same name
return loader.lookupClass(superReference.getName()).getField(name);
}
public TypeReference getReference() {
return reference;
}

View File

@ -152,6 +152,9 @@
<copy todir="${destination.temp.folder}/com.ibm.wala.core.testdata_1.0.0" failonerror="true" overwrite="false">
<fileset dir="${basedir}" includes="META-INF/" />
</copy>
<copy todir="${destination.temp.folder}/com.ibm.wala.core.testdata_1.0.0" failonerror="true" overwrite="false">
<fileset dir="${basedir}/classes" includes="**" />
</copy>
</target>
<target name="build.zips" depends="init">

Binary file not shown.

View File

@ -0,0 +1,51 @@
/*******************************************************************************
* 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.core.tests.cha;
import java.io.IOException;
import junit.framework.Assert;
import org.junit.Test;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IField;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.core.tests.util.WalaTestCase;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.io.FileProvider;
import com.ibm.wala.util.strings.Atom;
public class DupFieldsTest extends WalaTestCase {
@Test public void testDupFieldNames() throws IOException, ClassHierarchyException {
AnalysisScope scope = null;
scope = AnalysisScopeReader.readJavaScope(TestConstants.WALA_TESTDATA, FileProvider.getFile("J2SEClassHierarchyExclusions.txt"), DupFieldsTest.class.getClassLoader());
ClassHierarchy cha = ClassHierarchy.make(scope);
IClass klass = cha.lookupClass(TypeReference.findOrCreate(ClassLoaderReference.Application, "LDupFieldName"));
boolean threwException = false;
try {
klass.getField(Atom.findOrCreateUnicodeAtom("a"));
} catch (IllegalStateException e) {
threwException = true;
}
Assert.assertTrue(threwException);
IField f = klass.getField(Atom.findOrCreateUnicodeAtom("a"), TypeReference.IntName);
Assert.assertEquals(f.getFieldTypeReference(), TypeReference.Int);
f = klass.getField(Atom.findOrCreateUnicodeAtom("a"), TypeReference.BooleanName);
Assert.assertEquals(f.getFieldTypeReference(), TypeReference.Boolean);
}
}

View File

@ -129,6 +129,10 @@ public class ArrayClass implements IClass, Constants {
return getSuperclass().getField(name);
}
public IField getField(Atom name, TypeName typeName) {
return getSuperclass().getField(name, typeName);
}
/*
* @see com.ibm.wala.classLoader.IClass#getDeclaredMethods()
*/

View File

@ -121,7 +121,7 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
protected int hashCode;
private final HashMap<Atom, IField> fieldMap = HashMapFactory.make(5);
/**
* A warning for when we get a class not found exception
*/
@ -192,12 +192,17 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
if (fieldMap.containsKey(name)) {
return fieldMap.get(name);
} else {
IField f = findDeclaredField(name);
if (f != null) {
fieldMap.put(name, f);
return f;
List<IField> fields = findDeclaredField(name);
if (!fields.isEmpty()) {
if (fields.size() == 1) {
IField f = fields.iterator().next();
fieldMap.put(name, f);
return f;
} else {
throw new IllegalStateException("multiple fields with name " + name);
}
} else if ((superClass = getSuperclass()) != null) {
f = superClass.getField(name);
IField f = superClass.getField(name);
if (f != null) {
fieldMap.put(name, f);
return f;
@ -205,7 +210,7 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
}
// try superinterfaces
for (IClass i : getAllImplementedInterfaces()) {
f = i.getField(name);
IField f = i.getField(name);
if (f != null) {
fieldMap.put(name, f);
return f;
@ -216,6 +221,43 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
return null;
}
public IField getField(Atom name, TypeName type) {
try {
// typically, there will be at most one field with the name
IField field = getField(name);
if (field != null && field.getFieldTypeReference().getName().equals(type)) {
return field;
} else {
return null;
}
} catch (IllegalStateException e) {
assert e.getMessage().startsWith("multiple fields with");
// multiple fields. look through all of them and see if any have the appropriate type
List<IField> fields = findDeclaredField(name);
for (IField f : fields) {
if (f.getFieldTypeReference().getName().equals(type)) {
return f;
}
}
// check superclass
if (getSuperclass() != null) {
IField f = superClass.getField(name, type);
if (f != null) {
return f;
}
}
// try superinterfaces
for (IClass i : getAllImplementedInterfaces()) {
IField f = i.getField(name, type);
if (f != null) {
return f;
}
}
}
return null;
}
private void computeSuperclass() {
superclassComputed = true;
@ -466,11 +508,14 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
return result;
}
protected IField findDeclaredField(Atom name) {
protected List<IField> findDeclaredField(Atom name) {
List<IField> result = new ArrayList<IField>(1);
if (instanceFields != null) {
for (int i = 0; i < instanceFields.length; i++) {
if (instanceFields[i].getName() == name) {
return instanceFields[i];
result.add(instanceFields[i]);
}
}
}
@ -478,12 +523,12 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
if (staticFields != null) {
for (int i = 0; i < staticFields.length; i++) {
if (staticFields[i].getName() == name) {
return staticFields[i];
result.add(staticFields[i]);
}
}
}
return null;
return result;
}
protected void addFieldToList(List<FieldImpl> L, Atom name, ImmutableByteArray fieldType, int accessFlags,

View File

@ -88,9 +88,16 @@ public interface IClass extends IClassHierarchyDweller {
/**
* Finds a field.
*
* @throws IllegalStateException if the class contains multiple fields with name <code>name</code>.
*/
IField getField(Atom name);
/**
* Finds a field, given a name and a type. Returns <code>null</code> if not found.
*/
IField getField(Atom name, TypeName type);
/**
* @return canonical TypeReference corresponding to this class
*/

View File

@ -17,6 +17,7 @@ import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.TypeName;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.strings.Atom;
/**
* An {@link IClass} that exists nowhere in bytecode.
@ -121,4 +122,11 @@ public abstract class SyntheticClass implements IClass {
public TypeName getName() {
return getReference().getName();
}
/**
* we assume synthetic classes do not need to have multiple fields with the same name.
*/
public IField getField(Atom name, TypeName typeName) {
return getField(name);
}
}