2007-02-02 17:22:28 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 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
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* Created on Oct 3, 2005
|
|
|
|
*/
|
|
|
|
package com.ibm.wala.cast.java.test;
|
|
|
|
|
2007-08-09 17:39:35 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Set;
|
2007-02-02 17:22:28 +00:00
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
2008-05-27 19:43:53 +00:00
|
|
|
import junit.framework.Assert;
|
|
|
|
|
2008-05-23 15:58:02 +00:00
|
|
|
import org.eclipse.core.resources.IFile;
|
|
|
|
import org.eclipse.core.resources.IWorkspace;
|
|
|
|
import org.eclipse.core.resources.ResourcesPlugin;
|
|
|
|
import org.eclipse.jdt.core.IJavaProject;
|
|
|
|
|
2007-04-18 15:51:08 +00:00
|
|
|
import com.ibm.wala.cast.java.client.JavaSourceAnalysisEngine;
|
2009-03-13 14:20:35 +00:00
|
|
|
import com.ibm.wala.cast.java.ipa.callgraph.JavaSourceAnalysisScope;
|
2008-05-23 16:24:17 +00:00
|
|
|
import com.ibm.wala.classLoader.EclipseSourceFileModule;
|
2007-04-17 14:04:50 +00:00
|
|
|
import com.ibm.wala.classLoader.IClass;
|
|
|
|
import com.ibm.wala.classLoader.IClassLoader;
|
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
|
|
|
import com.ibm.wala.classLoader.JarFileModule;
|
2009-03-26 15:11:45 +00:00
|
|
|
import com.ibm.wala.classLoader.Language;
|
2007-04-17 14:04:50 +00:00
|
|
|
import com.ibm.wala.classLoader.SourceDirectoryTreeModule;
|
|
|
|
import com.ibm.wala.classLoader.SourceFileModule;
|
2008-05-27 13:29:35 +00:00
|
|
|
import com.ibm.wala.core.tests.util.EclipseTestUtil;
|
2008-05-27 19:43:53 +00:00
|
|
|
import com.ibm.wala.core.tests.util.WalaTestCase;
|
2007-02-02 17:22:28 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.CGNode;
|
|
|
|
import com.ibm.wala.ipa.callgraph.CallGraph;
|
2007-06-01 03:37:25 +00:00
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2007-08-09 17:39:35 +00:00
|
|
|
import com.ibm.wala.properties.WalaProperties;
|
2007-02-08 19:06:52 +00:00
|
|
|
import com.ibm.wala.ssa.IR;
|
|
|
|
import com.ibm.wala.ssa.SSAInstruction;
|
2007-02-02 17:22:28 +00:00
|
|
|
import com.ibm.wala.types.ClassLoaderReference;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
|
|
|
import com.ibm.wala.types.TypeName;
|
|
|
|
import com.ibm.wala.types.TypeReference;
|
2007-11-16 20:57:49 +00:00
|
|
|
import com.ibm.wala.util.collections.HashSetFactory;
|
|
|
|
import com.ibm.wala.util.collections.Pair;
|
2007-08-09 17:39:35 +00:00
|
|
|
import com.ibm.wala.util.debug.Assertions;
|
2008-01-24 22:04:26 +00:00
|
|
|
import com.ibm.wala.util.strings.Atom;
|
2007-02-02 17:22:28 +00:00
|
|
|
|
|
|
|
public abstract class IRTests extends WalaTestCase {
|
2008-05-23 15:58:02 +00:00
|
|
|
public IRTests(String name, String projectName) {
|
2007-02-09 15:33:22 +00:00
|
|
|
super(name);
|
2008-05-23 15:58:02 +00:00
|
|
|
this.projectName = projectName;
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2008-05-23 15:58:02 +00:00
|
|
|
protected final String projectName;
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
protected static String javaHomePath;
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2008-12-16 14:29:11 +00:00
|
|
|
private String testSrcPath = "." + File.separator + "src";
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-10-25 13:48:16 +00:00
|
|
|
public static List<String> rtJar;
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-18 02:28:58 +00:00
|
|
|
protected static List<IRAssertion> emptyList = Collections.emptyList();
|
2007-09-13 14:33:20 +00:00
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
static {
|
2007-09-13 14:33:20 +00:00
|
|
|
boolean found = false;
|
2007-08-02 17:20:29 +00:00
|
|
|
try {
|
2007-09-17 20:32:40 +00:00
|
|
|
rtJar = new LinkedList<String>();
|
|
|
|
|
2007-08-02 17:20:29 +00:00
|
|
|
Properties p = WalaProperties.loadProperties();
|
2007-09-13 14:33:20 +00:00
|
|
|
javaHomePath = p.getProperty(WalaProperties.J2SE_DIR);
|
|
|
|
|
|
|
|
if (new File(javaHomePath).isDirectory()) {
|
2008-01-02 22:28:04 +00:00
|
|
|
if ("Mac OS X".equals(System.getProperty("os.name"))) { // nick
|
|
|
|
/**
|
|
|
|
* todo: {@link WalaProperties#getJ2SEJarFiles()}
|
|
|
|
*/
|
2008-02-12 15:17:36 +00:00
|
|
|
rtJar.add(javaHomePath + "/classes.jar");
|
|
|
|
rtJar.add(javaHomePath + "/ui.jar");
|
2008-01-02 22:28:04 +00:00
|
|
|
} 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;
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
2008-12-16 14:29:11 +00:00
|
|
|
} catch (Exception e) {
|
2007-08-02 17:20:29 +00:00
|
|
|
// no properties
|
|
|
|
}
|
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
if (!found) {
|
2007-09-13 14:33:20 +00:00
|
|
|
javaHomePath = System.getProperty("java.home");
|
2008-01-02 22:28:04 +00:00
|
|
|
if ("Mac OS X".equals(System.getProperty("os.name"))) { // nick
|
|
|
|
rtJar.add(javaHomePath + "/../Classes/classes.jar");
|
|
|
|
rtJar.add(javaHomePath + "/../Classes/ui.jar");
|
2007-09-13 14:33:20 +00:00
|
|
|
} else {
|
2008-01-02 22:28:04 +00:00
|
|
|
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");
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IRAssertion {
|
|
|
|
|
|
|
|
void check(CallGraph cg) throws Exception;
|
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
protected static class EdgeAssertions implements IRAssertion {
|
2007-02-09 15:33:22 +00:00
|
|
|
public final String srcDescriptor;
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-03-12 16:09:57 +00:00
|
|
|
public final List/* <String> */<String> tgtDescriptors = new ArrayList<String>();
|
2007-02-09 15:33:22 +00:00
|
|
|
|
|
|
|
public EdgeAssertions(String srcDescriptor) {
|
|
|
|
this.srcDescriptor = srcDescriptor;
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
public static EdgeAssertions make(String srcDescriptor, String tgtDescriptor) {
|
|
|
|
EdgeAssertions ea = new EdgeAssertions(srcDescriptor);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor);
|
|
|
|
return ea;
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
public static EdgeAssertions make(String srcDescriptor, String tgtDescriptor1, String tgtDescriptor2) {
|
|
|
|
EdgeAssertions ea = new EdgeAssertions(srcDescriptor);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor1);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor2);
|
|
|
|
return ea;
|
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
public static EdgeAssertions make(String srcDescriptor, String tgtDescriptor1, String tgtDescriptor2, String tgtDescriptor3) {
|
|
|
|
EdgeAssertions ea = new EdgeAssertions(srcDescriptor);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor1);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor2);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor3);
|
|
|
|
return ea;
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
public static EdgeAssertions make(String srcDescriptor, String tgtDescriptor1, String tgtDescriptor2, String tgtDescriptor3,
|
|
|
|
String tgtDescriptor4) {
|
|
|
|
EdgeAssertions ea = new EdgeAssertions(srcDescriptor);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor1);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor2);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor3);
|
|
|
|
ea.tgtDescriptors.add(tgtDescriptor4);
|
|
|
|
return ea;
|
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
public void check(CallGraph callGraph) {
|
|
|
|
MethodReference srcMethod = descriptorToMethodRef(this.srcDescriptor, callGraph.getClassHierarchy());
|
|
|
|
Set<CGNode> srcNodes = callGraph.getNodes(srcMethod);
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
if (srcNodes.size() == 0) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("Unreachable/non-existent method: " + srcMethod));
|
2007-09-13 14:33:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (srcNodes.size() > 1) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println("Context-sensitive call graph?");
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
// Assume only one node for src method
|
|
|
|
CGNode srcNode = srcNodes.iterator().next();
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
for (String target : this.tgtDescriptors) {
|
|
|
|
MethodReference tgtMethod = descriptorToMethodRef(target, callGraph.getClassHierarchy());
|
|
|
|
// Assume only one node for target method
|
|
|
|
Set<CGNode> tgtNodes = callGraph.getNodes(tgtMethod);
|
|
|
|
if (tgtNodes.size() == 0) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("Unreachable/non-existent method: " + tgtMethod));
|
2007-09-13 14:33:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CGNode tgtNode = tgtNodes.iterator().next();
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
boolean found = false;
|
|
|
|
for (Iterator<? extends CGNode> succIter = callGraph.getSuccNodes(srcNode); succIter.hasNext();) {
|
|
|
|
CGNode succ = succIter.next();
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
if (tgtNode == succ) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("Missing edge: " + srcMethod + " -> " + tgtMethod));
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
protected static class SourceMapAssertion implements IRAssertion {
|
|
|
|
private final String method;
|
2008-01-02 22:28:04 +00:00
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
private final String variableName;
|
|
|
|
|
|
|
|
private final int definingLineNumber;
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
protected SourceMapAssertion(String method, String variableName, int definingLineNumber) {
|
|
|
|
this.method = method;
|
2007-02-09 15:33:22 +00:00
|
|
|
this.variableName = variableName;
|
|
|
|
this.definingLineNumber = definingLineNumber;
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
public void check(CallGraph cg) {
|
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
MethodReference mref = descriptorToMethodRef(method, cg.getClassHierarchy());
|
2007-09-13 14:33:20 +00:00
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
for (CGNode cgNode : cg.getNodes(mref)) {
|
|
|
|
Assert.assertTrue("failed for " + this.variableName + " in " + cgNode, this.check(cgNode.getMethod(), cgNode.getIR()));
|
|
|
|
}
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
|
|
|
|
2007-02-09 15:33:22 +00:00
|
|
|
boolean check(IMethod m, IR ir) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("check for " + variableName + " defined at " + definingLineNumber));
|
2007-02-09 15:33:22 +00:00
|
|
|
SSAInstruction[] insts = ir.getInstructions();
|
|
|
|
for (int i = 0; i < insts.length; i++) {
|
|
|
|
if (insts[i] != null) {
|
2009-05-29 14:23:29 +00:00
|
|
|
int ln = m.getLineNumber(i);
|
2007-02-09 15:33:22 +00:00
|
|
|
if (ln == definingLineNumber) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println((" found " + insts[i] + " at " + ln));
|
2007-02-09 15:33:22 +00:00
|
|
|
for (int j = 0; j < insts[i].getNumberOfDefs(); j++) {
|
|
|
|
int def = insts[i].getDef(j);
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println((" looking at def " + j + ": " + def));
|
2007-02-09 15:33:22 +00:00
|
|
|
String[] names = ir.getLocalNames(i, def);
|
|
|
|
if (names != null) {
|
2007-09-13 14:33:20 +00:00
|
|
|
for (String name : names) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println((" looking at name " + name));
|
2007-09-13 14:33:20 +00:00
|
|
|
if (name.equals(variableName)) {
|
2007-02-09 15:33:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
2009-04-29 18:17:20 +00:00
|
|
|
|
|
|
|
protected Collection<String> singleTestSrc() {
|
|
|
|
return Collections.singletonList(getTestSrcPath() + File.separator + singleJavaInputForTest());
|
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
|
2009-04-29 18:13:14 +00:00
|
|
|
protected Collection<String> singleTestSrc(final String folder) {
|
|
|
|
return Collections.singletonList(getTestSrcPath() + File.separator + folder + File.separator + singleJavaInputForTest());
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
protected Collection<String> singlePkgTestSrc(String pkgName) {
|
2009-01-09 14:12:19 +00:00
|
|
|
return Collections.singletonList(getTestSrcPath() + File.separator + singleJavaPkgInputForTest(pkgName));
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected String[] simpleTestEntryPoint() {
|
|
|
|
return new String[] { "L" + getName().substring(4) };
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String[] simplePkgTestEntryPoint(String pkgName) {
|
|
|
|
return new String[] { "L" + pkgName + "/" + getName().substring(4) };
|
|
|
|
}
|
|
|
|
|
2007-04-18 15:51:08 +00:00
|
|
|
protected abstract JavaSourceAnalysisEngine getAnalysisEngine(String[] mainClassDescriptors);
|
2007-02-09 15:33:22 +00:00
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
public Pair runTest(Collection<String> sources, List<String> libs, String[] mainClassDescriptors, List<? extends IRAssertion> ca,
|
|
|
|
boolean assertReachable) {
|
2007-02-09 15:33:22 +00:00
|
|
|
try {
|
2007-04-18 15:51:08 +00:00
|
|
|
JavaSourceAnalysisEngine engine = getAnalysisEngine(mainClassDescriptors);
|
2007-02-09 15:33:22 +00:00
|
|
|
|
|
|
|
populateScope(engine, sources, libs);
|
|
|
|
|
|
|
|
CallGraph callGraph = engine.buildDefaultCallGraph();
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(callGraph.toString());
|
2007-02-09 15:33:22 +00:00
|
|
|
|
|
|
|
// If we've gotten this far, IR has been produced.
|
2007-02-09 18:20:17 +00:00
|
|
|
dumpIR(callGraph, assertReachable);
|
2007-02-09 15:33:22 +00:00
|
|
|
|
|
|
|
// Now check any assertions as to source mapping
|
2007-09-13 14:33:20 +00:00
|
|
|
for (IRAssertion IRAssertion : ca) {
|
|
|
|
IRAssertion.check(callGraph);
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
return Pair.make(callGraph, engine.getPointerAnalysis());
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2007-02-09 15:33:22 +00:00
|
|
|
e.printStackTrace();
|
2009-02-19 15:59:33 +00:00
|
|
|
Assert.assertTrue(e.toString(), false);
|
2007-09-13 14:33:20 +00:00
|
|
|
return null;
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 17:58:11 +00:00
|
|
|
protected static void dumpIR(CallGraph cg, boolean assertReachable) throws IOException {
|
2007-04-17 14:04:50 +00:00
|
|
|
Set<IMethod> unreachable = HashSetFactory.make();
|
2007-06-01 03:37:25 +00:00
|
|
|
IClassHierarchy cha = cg.getClassHierarchy();
|
2009-03-13 14:20:35 +00:00
|
|
|
IClassLoader sourceLoader = cha.getLoader(JavaSourceAnalysisScope.SOURCE);
|
2007-02-09 15:33:22 +00:00
|
|
|
for (Iterator iter = sourceLoader.iterateAllClasses(); iter.hasNext();) {
|
|
|
|
IClass clazz = (IClass) iter.next();
|
|
|
|
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(clazz);
|
2007-02-09 15:33:22 +00:00
|
|
|
if (clazz.isInterface())
|
|
|
|
continue;
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
for (IMethod m : clazz.getDeclaredMethods()) {
|
|
|
|
if (m.isAbstract()) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(m);
|
2008-01-02 22:28:04 +00:00
|
|
|
} else {
|
2007-02-09 15:33:22 +00:00
|
|
|
Iterator nodeIter = cg.getNodes(m.getReference()).iterator();
|
|
|
|
if (!nodeIter.hasNext()) {
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("Method " + m.getReference() + " not reachable?"));
|
2007-04-17 14:04:50 +00:00
|
|
|
unreachable.add(m);
|
2007-02-09 15:33:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-02 22:28:04 +00:00
|
|
|
CGNode node = (CGNode) nodeIter.next();
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(node.getIR());
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
2007-04-04 13:04:29 +00:00
|
|
|
|
|
|
|
if (assertReachable) {
|
2009-01-09 14:12:19 +00:00
|
|
|
Assert.assertTrue("unreachable methods: " + unreachable.toString(), unreachable.isEmpty());
|
2007-04-04 13:04:29 +00:00
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
/**
|
2008-01-02 22:28:04 +00:00
|
|
|
*
|
|
|
|
* @param srcMethodDescriptor
|
|
|
|
* a full method descriptor of the form ldr#type#methName#methSig
|
|
|
|
* example: Source#Simple1#main#([Ljava/lang/String;)V
|
2007-09-13 14:33:20 +00:00
|
|
|
* @param cha
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static MethodReference descriptorToMethodRef(String srcMethodDescriptor, IClassHierarchy cha) {
|
|
|
|
String[] ldrTypeMeth = srcMethodDescriptor.split("\\#");
|
2007-02-09 15:33:22 +00:00
|
|
|
|
|
|
|
String loaderName = ldrTypeMeth[0];
|
|
|
|
String typeStr = ldrTypeMeth[1];
|
|
|
|
String methName = ldrTypeMeth[2];
|
|
|
|
String methSig = ldrTypeMeth[3];
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
TypeReference typeRef = findOrCreateTypeReference(loaderName, typeStr, cha);
|
|
|
|
|
2009-03-26 15:11:45 +00:00
|
|
|
Language l = cha.getLoader(typeRef.getClassLoader()).getLanguage();
|
|
|
|
return MethodReference.findOrCreate(l, typeRef, methName, methSig);
|
2007-09-13 14:33:20 +00:00
|
|
|
}
|
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
static TypeReference findOrCreateTypeReference(String loaderName, String typeStr, IClassHierarchy cha) {
|
2007-02-09 15:33:22 +00:00
|
|
|
ClassLoaderReference clr = findLoader(loaderName, cha);
|
|
|
|
TypeName typeName = TypeName.string2TypeName("L" + typeStr);
|
|
|
|
TypeReference typeRef = TypeReference.findOrCreate(clr, typeName);
|
2007-09-13 14:33:20 +00:00
|
|
|
return typeRef;
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
|
2007-06-01 03:37:25 +00:00
|
|
|
private static ClassLoaderReference findLoader(String loaderName, IClassHierarchy cha) {
|
2007-02-09 15:33:22 +00:00
|
|
|
Atom loaderAtom = Atom.findOrCreateUnicodeAtom(loaderName);
|
|
|
|
IClassLoader[] loaders = cha.getLoaders();
|
2007-09-13 14:33:20 +00:00
|
|
|
for (IClassLoader loader : loaders) {
|
|
|
|
if (loader.getName() == loaderAtom) {
|
|
|
|
return loader.getReference();
|
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
|
|
|
Assertions.UNREACHABLE();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2008-05-23 15:58:02 +00:00
|
|
|
protected void populateScope(JavaSourceAnalysisEngine engine,
|
|
|
|
Collection<String> sources,
|
|
|
|
List<String> libs)
|
|
|
|
throws IOException
|
|
|
|
{
|
2007-02-09 15:33:22 +00:00
|
|
|
boolean foundLib = false;
|
2007-09-13 14:33:20 +00:00
|
|
|
for (String lib : libs) {
|
2007-02-09 15:33:22 +00:00
|
|
|
File libFile = new File(lib);
|
|
|
|
if (libFile.exists()) {
|
|
|
|
foundLib = true;
|
|
|
|
engine.addSystemModule(new JarFileModule(new JarFile(libFile)));
|
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
2009-04-30 13:16:52 +00:00
|
|
|
assert foundLib : "couldn't find library file from " + libs;
|
2007-02-09 15:33:22 +00:00
|
|
|
|
2008-05-23 15:58:02 +00:00
|
|
|
IWorkspace w = null;
|
|
|
|
IJavaProject project = null;
|
|
|
|
try {
|
|
|
|
if (projectName != null) {
|
|
|
|
w = ResourcesPlugin.getWorkspace();
|
|
|
|
project = EclipseTestUtil.getNamedProject(projectName);
|
|
|
|
}
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
// use Workspace only if it exists
|
|
|
|
}
|
|
|
|
|
2007-09-13 14:33:20 +00:00
|
|
|
for (String srcFilePath : sources) {
|
2008-05-23 15:58:02 +00:00
|
|
|
|
|
|
|
if (w != null) {
|
|
|
|
IFile file = project.getProject().getFile(srcFilePath);
|
2009-01-09 14:12:19 +00:00
|
|
|
try {
|
2009-03-13 17:06:09 +00:00
|
|
|
engine.addSourceModule(EclipseSourceFileModule.createEclipseSourceFileModule(file));
|
2009-01-09 14:12:19 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
assertTrue(e.getMessage(), false);
|
|
|
|
}
|
2008-05-23 15:58:02 +00:00
|
|
|
|
2008-01-02 22:28:04 +00:00
|
|
|
} else {
|
2008-05-23 15:58:02 +00:00
|
|
|
String srcFileName = srcFilePath.substring(srcFilePath.lastIndexOf(File.separator) + 1);
|
|
|
|
File f = new File(srcFilePath);
|
2008-12-17 19:52:27 +00:00
|
|
|
Assert.assertTrue("couldn't find " + srcFilePath, f.exists());
|
2008-05-23 15:58:02 +00:00
|
|
|
if (f.isDirectory()) {
|
|
|
|
engine.addSourceModule(new SourceDirectoryTreeModule(f));
|
|
|
|
} else {
|
|
|
|
engine.addSourceModule(new SourceFileModule(f, srcFileName));
|
|
|
|
}
|
2007-03-12 02:08:45 +00:00
|
|
|
}
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|
2007-02-09 15:33:22 +00:00
|
|
|
}
|
2008-12-16 14:29:11 +00:00
|
|
|
|
|
|
|
protected void setTestSrcPath(String testSrcPath) {
|
|
|
|
this.testSrcPath = testSrcPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getTestSrcPath() {
|
|
|
|
return testSrcPath;
|
|
|
|
}
|
2009-01-09 14:12:19 +00:00
|
|
|
|
|
|
|
protected String singleJavaInputForTest() {
|
|
|
|
return getName().substring(4) + ".java";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String singleInputForTest() {
|
|
|
|
return getName().substring(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String singleJavaPkgInputForTest(String pkgName) {
|
|
|
|
return pkgName + File.separator + getName().substring(4) + ".java";
|
|
|
|
}
|
|
|
|
|
2007-02-02 17:22:28 +00:00
|
|
|
}
|