clean up and refactor some APIs surrounding file I/O and exclusions files. Needed in order to allow clients to use some APIs with application-specific exclusions.

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2251 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2008-01-02 22:28:04 +00:00
parent afd0014e74
commit fa5af94335
2 changed files with 263 additions and 290 deletions

View File

@ -76,34 +76,34 @@ public abstract class IRTests extends WalaTestCase {
javaHomePath = p.getProperty(WalaProperties.J2SE_DIR);
if (new File(javaHomePath).isDirectory()) {
if("Mac OS X".equals(System.getProperty("os.name"))) { //nick
/**
* todo: {@link WalaProperties#getJ2SEJarFiles()}
*/
rtJar.add(javaHomePath + "/Classes/classes.jar");
rtJar.add(javaHomePath + "/Classes/ui.jar");
} else {
rtJar.add(javaHomePath + File.separator + "classes.jar");
rtJar.add(javaHomePath + File.separator + "rt.jar");
rtJar.add(javaHomePath + File.separator + "core.jar");
rtJar.add(javaHomePath + File.separator + "vm.jar");
}
found = true;
if ("Mac OS X".equals(System.getProperty("os.name"))) { // nick
/**
* todo: {@link WalaProperties#getJ2SEJarFiles()}
*/
rtJar.add(javaHomePath + "/Classes/classes.jar");
rtJar.add(javaHomePath + "/Classes/ui.jar");
} else {
rtJar.add(javaHomePath + File.separator + "classes.jar");
rtJar.add(javaHomePath + File.separator + "rt.jar");
rtJar.add(javaHomePath + File.separator + "core.jar");
rtJar.add(javaHomePath + File.separator + "vm.jar");
}
found = true;
}
} catch (WalaException e) {
// no properties
}
if (! found) {
if (!found) {
javaHomePath = System.getProperty("java.home");
if("Mac OS X".equals(System.getProperty("os.name"))) { //nick
rtJar.add(javaHomePath + "/../Classes/classes.jar");
rtJar.add(javaHomePath + "/../Classes/ui.jar");
if ("Mac OS X".equals(System.getProperty("os.name"))) { // nick
rtJar.add(javaHomePath + "/../Classes/classes.jar");
rtJar.add(javaHomePath + "/../Classes/ui.jar");
} else {
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "rt.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "core.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "vm.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "classes.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "rt.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "core.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "vm.jar");
rtJar.add(javaHomePath + File.separator + "lib" + File.separator + "classes.jar");
}
}
}
@ -197,6 +197,7 @@ public abstract class IRTests extends WalaTestCase {
protected static class SourceMapAssertion implements IRAssertion {
private final String method;
private final String variableName;
private final int definingLineNumber;
@ -209,12 +210,11 @@ public abstract class IRTests extends WalaTestCase {
public void check(CallGraph cg) {
MethodReference mref = descriptorToMethodRef(method, cg.getClassHierarchy());
MethodReference mref = descriptorToMethodRef(method, cg.getClassHierarchy());
for (CGNode cgNode : cg.getNodes(mref)) {
Assert.assertTrue("failed for " + this.variableName + " in " + cgNode,
this.check(cgNode.getMethod(), cgNode.getIR()));
}
for (CGNode cgNode : cg.getNodes(mref)) {
Assert.assertTrue("failed for " + this.variableName + " in " + cgNode, this.check(cgNode.getMethod(), cgNode.getIR()));
}
}
boolean check(IMethod m, IR ir) {
@ -274,12 +274,8 @@ public abstract class IRTests extends WalaTestCase {
protected abstract JavaSourceAnalysisEngine getAnalysisEngine(String[] mainClassDescriptors);
public Pair runTest(Collection<String> sources,
List<String> libs,
String[] mainClassDescriptors,
List<? extends IRAssertion> ca,
boolean assertReachable)
{
public Pair runTest(Collection<String> sources, List<String> libs, String[] mainClassDescriptors, List<? extends IRAssertion> ca,
boolean assertReachable) {
try {
JavaSourceAnalysisEngine engine = getAnalysisEngine(mainClassDescriptors);
@ -318,15 +314,14 @@ public abstract class IRTests extends WalaTestCase {
for (IMethod m : clazz.getDeclaredMethods()) {
if (m.isAbstract()) {
Trace.println(m);
}
else {
} else {
Iterator nodeIter = cg.getNodes(m.getReference()).iterator();
if (!nodeIter.hasNext()) {
Trace.println("Method " + m.getReference() + " not reachable?");
unreachable.add(m);
continue;
}
CGNode node = (CGNode)nodeIter.next();
CGNode node = (CGNode) nodeIter.next();
Trace.println(node.getIR());
}
}
@ -337,11 +332,11 @@ public abstract class IRTests extends WalaTestCase {
}
}
/**
*
* @param srcMethodDescriptor a full method descriptor of the form ldr#type#methName#methSig
* example: Source#Simple1#main#([Ljava/lang/String;)V
*
* @param srcMethodDescriptor
* a full method descriptor of the form ldr#type#methName#methSig
* example: Source#Simple1#main#([Ljava/lang/String;)V
* @param cha
* @return
*/
@ -358,8 +353,7 @@ public abstract class IRTests extends WalaTestCase {
return MethodReference.findOrCreate(typeRef, methName, methSig);
}
static TypeReference findOrCreateTypeReference(String loaderName, String typeStr,
IClassHierarchy cha) {
static TypeReference findOrCreateTypeReference(String loaderName, String typeStr, IClassHierarchy cha) {
ClassLoaderReference clr = findLoader(loaderName, cha);
TypeName typeName = TypeName.string2TypeName("L" + typeStr);
TypeReference typeRef = TypeReference.findOrCreate(clr, typeName);
@ -378,8 +372,7 @@ public abstract class IRTests extends WalaTestCase {
return null;
}
protected void populateScope(JavaSourceAnalysisEngine engine, Collection<String> sources, List<String> libs)
throws IOException {
protected void populateScope(JavaSourceAnalysisEngine engine, Collection<String> sources, List<String> libs) throws IOException {
boolean foundLib = false;
for (String lib : libs) {
@ -397,8 +390,7 @@ public abstract class IRTests extends WalaTestCase {
Assert.assertTrue(f.exists());
if (f.isDirectory()) {
engine.addSourceModule(new SourceDirectoryTreeModule(f));
}
else {
} else {
engine.addSourceModule(new SourceFileModule(f, srcFileName));
}
}

View File

@ -22,7 +22,6 @@ import java.util.Set;
import junit.framework.Assert;
import com.ibm.wala.cast.java.client.JavaSourceAnalysisEngine;
import com.ibm.wala.cast.java.ipa.slicer.AstJavaSlicer;
import com.ibm.wala.cast.java.loader.JavaSourceLoaderImpl;
@ -55,8 +54,6 @@ import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.Atom;
import com.ibm.wala.util.collections.Pair;
public class JavaIRTests extends IRTests {
public JavaIRTests(String name) {
super(name);
@ -86,14 +83,13 @@ public class JavaIRTests extends IRTests {
public void testSimple1() {
List<? extends IRAssertion> assertions = Arrays.asList(
new SourceMapAssertion("Source#Simple1#doStuff#(I)V", "prod", 14),
new SourceMapAssertion("Source#Simple1#doStuff#(I)V", "j", 13),
new SourceMapAssertion("Source#Simple1#main#([Ljava/lang/String;)V", "s", 22),
new SourceMapAssertion("Source#Simple1#main#([Ljava/lang/String;)V", "i", 18),
new SourceMapAssertion("Source#Simple1#main#([Ljava/lang/String;)V", "sum", 19),
EdgeAssertions.make("Source#Simple1#main#([Ljava/lang/String;)V", "Source#Simple1#doStuff#(I)V"),
EdgeAssertions.make("Source#Simple1#instanceMethod1#()V", "Source#Simple1#instanceMethod2#()V"));
List<? extends IRAssertion> assertions = Arrays.asList(new SourceMapAssertion("Source#Simple1#doStuff#(I)V", "prod", 14),
new SourceMapAssertion("Source#Simple1#doStuff#(I)V", "j", 13), new SourceMapAssertion(
"Source#Simple1#main#([Ljava/lang/String;)V", "s", 22), new SourceMapAssertion(
"Source#Simple1#main#([Ljava/lang/String;)V", "i", 18), new SourceMapAssertion(
"Source#Simple1#main#([Ljava/lang/String;)V", "sum", 19), EdgeAssertions.make(
"Source#Simple1#main#([Ljava/lang/String;)V", "Source#Simple1#doStuff#(I)V"), EdgeAssertions.make(
"Source#Simple1#instanceMethod1#()V", "Source#Simple1#instanceMethod2#()V"));
// this needs soure positions to work too
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), assertions, true);
@ -102,346 +98,339 @@ public class JavaIRTests extends IRTests {
public void testTwoClasses() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
new IRAssertion() {
new IRAssertion() {
public void check(CallGraph cg)
throws Exception {
final String typeStr = singleInputForTestNoExt();
public void check(CallGraph cg) throws Exception {
final String typeStr = singleInputForTestNoExt();
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
Assert.assertEquals("Expected two classes.", iClass.getClassLoader().getNumberOfClasses(), 2);
Assert.assertEquals("Expected two classes.", iClass.getClassLoader().getNumberOfClasses(), 2);
for(Iterator<IClass> it = iClass.getClassLoader().iterateAllClasses(); it.hasNext(); ) {
IClass cls = it.next();
for (Iterator<IClass> it = iClass.getClassLoader().iterateAllClasses(); it.hasNext();) {
IClass cls = it.next();
Assert.assertTrue("Expected class to be either " + typeStr + " or " + "Bar",
cls.getName().getClassName().toString().equals(typeStr) ||
cls.getName().getClassName().toString().equals("Bar"));
}
Assert.assertTrue("Expected class to be either " + typeStr + " or " + "Bar", cls.getName().getClassName().toString()
.equals(typeStr)
|| cls.getName().getClassName().toString().equals("Bar"));
}
}
), true);
}), true);
}
public void testInterfaceTest1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
* IFoo is an interface
*/
new IRAssertion() {
/**
* IFoo is an interface
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
final String typeStr = "IFoo";
public void check(CallGraph cg) throws Exception {
final String typeStr = "IFoo";
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
Assert.assertTrue("Expected IFoo to be an interface.", iClass.isInterface());
}
},
Assert.assertTrue("Expected IFoo to be an interface.", iClass.isInterface());
}
},
/**
* Foo implements IFoo
*/
new IRAssertion() {
/**
* Foo implements IFoo
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
final String typeStr = "Foo";
public void check(CallGraph cg) throws Exception {
final String typeStr = "Foo";
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final Collection<IClass> interfaces = iClass.getDirectInterfaces();
final Collection<IClass> interfaces = iClass.getDirectInterfaces();
Assert.assertEquals("Expected one single interface.", interfaces.size(), 1);
Assert.assertEquals("Expected one single interface.", interfaces.size(), 1);
Assert.assertTrue("Expected Foo to implement IFoo", interfaces.contains(cg.getClassHierarchy().lookupClass(
Assert.assertTrue("Expected Foo to implement IFoo", interfaces.contains(cg.getClassHierarchy().lookupClass(
findOrCreateTypeReference("Source", "IFoo", cg.getClassHierarchy()))));
}
}), true);
}
}), true);
}
public void testInheritance1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
* 'Derived' extends 'Base'
*/
new IRAssertion() {
/**
* 'Derived' extends 'Base'
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
final String typeStr = "Derived";
public void check(CallGraph cg) throws Exception {
final String typeStr = "Derived";
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass derivedClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, derivedClass);
final IClass derivedClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, derivedClass);
final TypeReference baseType = findOrCreateTypeReference("Source", "Base", cg.getClassHierarchy());
final IClass baseClass = cg.getClassHierarchy().lookupClass(baseType);
final TypeReference baseType = findOrCreateTypeReference("Source", "Base", cg.getClassHierarchy());
final IClass baseClass = cg.getClassHierarchy().lookupClass(baseType);
Assert.assertTrue("Expected 'Base' to be the superclass of 'Derived'", derivedClass.getSuperclass().equals(baseClass));
Assert.assertTrue("Expected 'Base' to be the superclass of 'Derived'", derivedClass.getSuperclass().equals(baseClass));
Collection<IClass> subclasses = cg.getClassHierarchy().computeSubClasses(baseType);
Collection<IClass> subclasses = cg.getClassHierarchy().computeSubClasses(baseType);
Assert.assertTrue("Expected subclasses of 'Base' to be 'Base' and 'Derived'.",
subclasses.contains(derivedClass) && subclasses.contains(baseClass));
}
}), true);
Assert.assertTrue("Expected subclasses of 'Base' to be 'Base' and 'Derived'.", subclasses.contains(derivedClass)
&& subclasses.contains(baseClass));
}
}), true);
}
public void testArray1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
* 'foo' has four array instructions:
* - 2 SSAArrayLengthInstruction
* - 1 SSAArrayLoadInstruction
* - 1 SSAArrayStoreInstruction
*/
new IRAssertion() {
/**
* 'foo' has four array instructions: - 2 SSAArrayLengthInstruction - 1
* SSAArrayLoadInstruction - 1 SSAArrayStoreInstruction
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
public void check(CallGraph cg) throws Exception {
MethodReference mref = descriptorToMethodRef("Source#Array1#foo#()V", cg.getClassHierarchy());
MethodReference mref = descriptorToMethodRef("Source#Array1#foo#()V", cg.getClassHierarchy());
int count = 0;
CGNode node = cg.getNodes(mref).iterator().next();
for(SSAInstruction s : node.getIR().getInstructions()) {
if(isArrayInstruction(s)) {
count++;
}
int count = 0;
CGNode node = cg.getNodes(mref).iterator().next();
for (SSAInstruction s : node.getIR().getInstructions()) {
if (isArrayInstruction(s)) {
count++;
}
Assert.assertEquals("Unexpected number of array instructions in 'foo'.", count, 4);
}
private boolean isArrayInstruction(SSAInstruction s) {
return s instanceof SSAArrayReferenceInstruction || s instanceof SSAArrayLengthInstruction;
}
}), true);
Assert.assertEquals("Unexpected number of array instructions in 'foo'.", count, 4);
}
private boolean isArrayInstruction(SSAInstruction s) {
return s instanceof SSAArrayReferenceInstruction || s instanceof SSAArrayLengthInstruction;
}
}), true);
}
public void testArrayLiteral1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
* 'foo' has four array instructions:
* - 2 SSAArrayLengthInstruction
* - 1 SSAArrayLoadInstruction
* - 1 SSAArrayStoreInstruction
*/
new IRAssertion() {
/**
* 'foo' has four array instructions: - 2 SSAArrayLengthInstruction - 1
* SSAArrayLoadInstruction - 1 SSAArrayStoreInstruction
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
public void check(CallGraph cg) throws Exception {
MethodReference mref = descriptorToMethodRef("Source#ArrayLiteral1#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
MethodReference mref = descriptorToMethodRef("Source#ArrayLiteral1#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
CGNode node = cg.getNodes(mref).iterator().next();
SSAInstruction s = node.getIR().getInstructions()[3];
Assert.assertTrue("Did not find new array instruction.", s instanceof SSANewInstruction);
Assert.assertTrue("", ((SSANewInstruction)s).getNewSite().getDeclaredType().isArrayType());
}
CGNode node = cg.getNodes(mref).iterator().next();
SSAInstruction s = node.getIR().getInstructions()[3];
Assert.assertTrue("Did not find new array instruction.", s instanceof SSANewInstruction);
Assert.assertTrue("", ((SSANewInstruction) s).getNewSite().getDeclaredType().isArrayType());
}
}), true);
}), true);
}
public void testArrayLiteral2() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
* int[] y= { 1, 2, 3, 4 } is represented in the IR as four array store instructions
*/
new IRAssertion() {
/**
* int[] y= { 1, 2, 3, 4 } is represented in the IR as four array store
* instructions
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
public void check(CallGraph cg) throws Exception {
MethodReference mref = descriptorToMethodRef("Source#ArrayLiteral2#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
MethodReference mref = descriptorToMethodRef("Source#ArrayLiteral2#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
CGNode node = cg.getNodes(mref).iterator().next();
CGNode node = cg.getNodes(mref).iterator().next();
final SSAInstruction[] instructions = node.getIR().getInstructions();
// test 1
{
SSAInstruction s1 = instructions[3];
if(s1 instanceof SSANewInstruction) {
Assert.assertTrue("", ((SSANewInstruction)s1).getNewSite().getDeclaredType().isArrayType());
} else {
Assert.assertTrue("Expected 3rd to be a new array instruction.", false);
}
}
// test 2
{
SSAInstruction s2 = instructions[4];
if(s2 instanceof SSANewInstruction) {
Assert.assertTrue("", ((SSANewInstruction)s2).getNewSite().getDeclaredType().isArrayType());
} else {
Assert.assertTrue("Expected 4th to be a new array instruction.", false);
}
}
// test 3: the last 4 instructions are of the form y[i] = i+1;
{
final SymbolTable symbolTable = node.getIR().getSymbolTable();
for(int i = 5; i <= 8; i++) {
Assert.assertTrue("Expected only array stores.", instructions[i] instanceof SSAArrayStoreInstruction);
SSAArrayStoreInstruction as = (SSAArrayStoreInstruction)instructions[i];
Assert.assertEquals("Expected an array store to 'y'.", node.getIR().getLocalNames(i, as.getArrayRef())[0], "y");
final Integer valueOfArrayIndex = ((Integer)symbolTable.getConstantValue(as.getIndex()));
final Integer valueAssigned = (Integer)symbolTable.getConstantValue(as.getValue());
Assert.assertEquals("Expected an array store to 'y' with value " + (valueOfArrayIndex + 1),
valueAssigned.intValue(), valueOfArrayIndex + 1);
}
final SSAInstruction[] instructions = node.getIR().getInstructions();
// test 1
{
SSAInstruction s1 = instructions[3];
if (s1 instanceof SSANewInstruction) {
Assert.assertTrue("", ((SSANewInstruction) s1).getNewSite().getDeclaredType().isArrayType());
} else {
Assert.assertTrue("Expected 3rd to be a new array instruction.", false);
}
}
// test 2
{
SSAInstruction s2 = instructions[4];
if (s2 instanceof SSANewInstruction) {
Assert.assertTrue("", ((SSANewInstruction) s2).getNewSite().getDeclaredType().isArrayType());
} else {
Assert.assertTrue("Expected 4th to be a new array instruction.", false);
}
}
// test 3: the last 4 instructions are of the form y[i] = i+1;
{
final SymbolTable symbolTable = node.getIR().getSymbolTable();
for (int i = 5; i <= 8; i++) {
Assert.assertTrue("Expected only array stores.", instructions[i] instanceof SSAArrayStoreInstruction);
}), true);
SSAArrayStoreInstruction as = (SSAArrayStoreInstruction) instructions[i];
Assert.assertEquals("Expected an array store to 'y'.", node.getIR().getLocalNames(i, as.getArrayRef())[0], "y");
final Integer valueOfArrayIndex = ((Integer) symbolTable.getConstantValue(as.getIndex()));
final Integer valueAssigned = (Integer) symbolTable.getConstantValue(as.getValue());
Assert.assertEquals("Expected an array store to 'y' with value " + (valueOfArrayIndex + 1), valueAssigned.intValue(),
valueOfArrayIndex + 1);
}
}
}
}), true);
}
public void testInheritedField() {
List<EdgeAssertions> edgeAssertionses = Arrays.asList(
EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V", "Source#B#foo#()V"),
EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V", "Source#B#bar#()V"));
List<EdgeAssertions> edgeAssertionses = Arrays.asList(EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V",
"Source#B#foo#()V"), EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V", "Source#B#bar#()V"));
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), edgeAssertionses, true);
}
public void testQualifiedStatic() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
/**
*
*/
new IRAssertion() {
/**
*
*/
new IRAssertion() {
public void check(CallGraph cg) throws Exception {
public void check(CallGraph cg) throws Exception {
MethodReference mref = descriptorToMethodRef("Source#QualifiedStatic#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
MethodReference mref = descriptorToMethodRef("Source#QualifiedStatic#main#([Ljava/lang/String;)V", cg.getClassHierarchy());
CGNode node = cg.getNodes(mref).iterator().next();
SSAInstruction s = node.getIR().getInstructions()[5];
CGNode node = cg.getNodes(mref).iterator().next();
SSAInstruction s = node.getIR().getInstructions()[5];
Assert.assertTrue("Did not find a getstatic instruction.", s instanceof SSAGetInstruction && ((SSAGetInstruction)s).isStatic());
final FieldReference field = ((SSAGetInstruction)s).getDeclaredField();
Assert.assertEquals("Expected a getstatic for 'value'.", field.getName().toString(), "value");
Assert.assertEquals("Expected a getstatic for 'value'.", field.getDeclaringClass().getName().toString(), "LFooQ");
}
Assert.assertTrue("Did not find a getstatic instruction.", s instanceof SSAGetInstruction
&& ((SSAGetInstruction) s).isStatic());
final FieldReference field = ((SSAGetInstruction) s).getDeclaredField();
Assert.assertEquals("Expected a getstatic for 'value'.", field.getName().toString(), "value");
Assert.assertEquals("Expected a getstatic for 'value'.", field.getDeclaringClass().getName().toString(), "LFooQ");
}
}), true);
}), true);
}
public void testStaticNesting() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
new IRAssertion() {
new IRAssertion() {
public void check(CallGraph cg)
throws Exception {
final String typeStr = singleInputForTestNoExt() + "$WhatsIt";
public void check(CallGraph cg) throws Exception {
final String typeStr = singleInputForTestNoExt() + "$WhatsIt";
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
//todo: this fails: Assert.assertNotNull("Expected to be enclosed in 'StaticNesting'.", ((JavaSourceLoaderImpl.JavaClass)iClass).getEnclosingClass());
//todo: is there the concept of CompilationUnit?
// todo: this fails: Assert.assertNotNull("Expected to be enclosed in
// 'StaticNesting'.",
// ((JavaSourceLoaderImpl.JavaClass)iClass).getEnclosingClass());
// todo: is there the concept of CompilationUnit?
/**
* {@link JavaCAst2IRTranslator#getEnclosingType} return null for static inner classes..?
*/
}
/**
* {@link JavaCAst2IRTranslator#getEnclosingType} return null for static
* inner classes..?
*/
}
), true);
}), true);
}
public void testInnerClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
new IRAssertion() {
new IRAssertion() {
public void check(CallGraph cg)
throws Exception {
final String typeStr = singleInputForTestNoExt();
public void check(CallGraph cg) throws Exception {
final String typeStr = singleInputForTestNoExt();
final TypeReference type = findOrCreateTypeReference("Source", typeStr + "$WhatsIt", cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr + "$WhatsIt", cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
Assert.assertEquals("Expected to be enclosed in 'InnerClass'.",
((JavaSourceLoaderImpl.JavaClass)iClass).getEnclosingClass(), //todo is there another way?
Assert.assertEquals("Expected to be enclosed in 'InnerClass'.", ((JavaSourceLoaderImpl.JavaClass) iClass)
.getEnclosingClass(), // todo is there another way?
cg.getClassHierarchy().lookupClass(findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy())));
}
}
), true);
}), true);
}
public void testLocalClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
new IRAssertion() {
new IRAssertion() {
/**
* Classes local to method are enclosed in the class the methods belong to.
*/
public void check(CallGraph cg)
throws Exception {
final String typeStr = singleInputForTestNoExt();
final String localClassStr = "Foo";
/**
* Classes local to method are enclosed in the class the methods belong
* to.
*/
public void check(CallGraph cg) throws Exception {
final String typeStr = singleInputForTestNoExt();
final String localClassStr = "Foo";
// Observe the descriptor for a class local to a method.
final TypeReference mainFooType = findOrCreateTypeReference("Source",
typeStr + "/main([Ljava/lang/String;)V/" + localClassStr, cg.getClassHierarchy());
// Observe the descriptor for a class local to a method.
final TypeReference mainFooType = findOrCreateTypeReference("Source", typeStr + "/main([Ljava/lang/String;)V/"
+ localClassStr, cg.getClassHierarchy());
// Observe the descriptor for a class local to a method.
final IClass mainFooClass = cg.getClassHierarchy().lookupClass(mainFooType);
Assert.assertNotNull("Could not find class " + mainFooType, mainFooClass);
// Observe the descriptor for a class local to a method.
final IClass mainFooClass = cg.getClassHierarchy().lookupClass(mainFooType);
Assert.assertNotNull("Could not find class " + mainFooType, mainFooClass);
final TypeReference methodFooType = findOrCreateTypeReference("Source",
typeStr + "/method()V/" + localClassStr, cg.getClassHierarchy());
final TypeReference methodFooType = findOrCreateTypeReference("Source", typeStr + "/method()V/" + localClassStr, cg
.getClassHierarchy());
final IClass methodFooClass = cg.getClassHierarchy().lookupClass(methodFooType);
Assert.assertNotNull("Could not find class " + methodFooType, methodFooClass);
final IClass methodFooClass = cg.getClassHierarchy().lookupClass(methodFooType);
Assert.assertNotNull("Could not find class " + methodFooType, methodFooClass);
final IClass localClass = cg.getClassHierarchy().lookupClass(findOrCreateTypeReference("Source",
typeStr, cg.getClassHierarchy()));
final IClass localClass = cg.getClassHierarchy().lookupClass(
findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy()));
Assert.assertSame("'Foo' is enclosed in 'Local'", ((JavaSourceLoaderImpl.JavaClass)methodFooClass).getEnclosingClass(),
Assert.assertSame("'Foo' is enclosed in 'Local'", ((JavaSourceLoaderImpl.JavaClass) methodFooClass).getEnclosingClass(),
localClass);
//todo: is this failing because 'main' is static?
// Assert.assertSame("'Foo' is enclosed in 'Local'", ((JavaSourceLoaderImpl.JavaClass)mainFooClass).getEnclosingClass(),
// localClass);
}
// todo: is this failing because 'main' is static?
// Assert.assertSame("'Foo' is enclosed in 'Local'",
// ((JavaSourceLoaderImpl.JavaClass)mainFooClass).getEnclosingClass(),
// localClass);
}
), true);
}), true);
}
public void testAnonymousClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), Arrays.asList(
new IRAssertion() {
new IRAssertion() {
public void check(CallGraph cg)
throws Exception {
final String typeStr = singleInputForTestNoExt();
public void check(CallGraph cg) throws Exception {
final String typeStr = singleInputForTestNoExt();
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final TypeReference type = findOrCreateTypeReference("Source", typeStr, cg.getClassHierarchy());
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
final IClass iClass = cg.getClassHierarchy().lookupClass(type);
Assert.assertNotNull("Could not find class " + typeStr, iClass);
//todo what to check?? could not find anything in the APIs for anonymous
}
// todo what to check?? could not find anything in the APIs for
// anonymous
}
), true);
}), true);
}
public void testWhileTest1() {
@ -496,16 +485,10 @@ public class JavaIRTests extends IRTests {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(), emptyList, true);
}
private MethodReference
getSliceRootReference(String className,
String methodName,
String methodDescriptor)
{
TypeName clsName =
TypeName.string2TypeName("L" + className.replace('.', '/'));
TypeReference clsRef =
TypeReference.findOrCreate(EclipseProjectPath.SOURCE_REF, clsName);
private MethodReference getSliceRootReference(String className, String methodName, String methodDescriptor) {
TypeName clsName = TypeName.string2TypeName("L" + className.replace('.', '/'));
TypeReference clsRef = TypeReference.findOrCreate(EclipseProjectPath.SOURCE_REF, clsName);
Atom nameAtom = Atom.findOrCreateUnicodeAtom(methodName);
Descriptor descr = Descriptor.findOrCreateUTF8(methodDescriptor);
@ -518,20 +501,18 @@ public class JavaIRTests extends IRTests {
PointerAnalysis pa = (PointerAnalysis) x.snd;
CallGraph cg = (CallGraph) x.fst;
// test partial slice
MethodReference sliceRootRef =
getSliceRootReference("MiniaturSliceBug", "validNonDispatchedCall", "(LIntWrapper;)V");
Set<CGNode> roots = cg.getNodes( sliceRootRef );
Pair<Collection<Statement>, SDG> y = AstJavaSlicer.computeAssertionSlice(cg, pa, roots);
// test partial slice
MethodReference sliceRootRef = getSliceRootReference("MiniaturSliceBug", "validNonDispatchedCall", "(LIntWrapper;)V");
Set<CGNode> roots = cg.getNodes(sliceRootRef);
Pair<Collection<Statement>, SDG> y = AstJavaSlicer.computeAssertionSlice(cg, pa, roots);
Collection<Statement> slice = y.fst;
SlicerTest.dumpSlice(slice);
assertEquals(0, SlicerTest.countAllocations(slice));
assertEquals(1, SlicerTest.countPutfields(slice));
// test slice from main
sliceRootRef =
getSliceRootReference("MiniaturSliceBug", "main", "([Ljava/lang/String;)V");
roots = cg.getNodes( sliceRootRef );
sliceRootRef = getSliceRootReference("MiniaturSliceBug", "main", "([Ljava/lang/String;)V");
roots = cg.getNodes(sliceRootRef);
y = AstJavaSlicer.computeAssertionSlice(cg, pa, roots);
slice = y.fst;
SlicerTest.dumpSlice(slice);