more EMF obliteration

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2035 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2007-11-16 19:20:57 +00:00
parent beb56a76e7
commit bdf7e695b9
5 changed files with 2 additions and 646 deletions

View File

@ -1,157 +0,0 @@
/*******************************************************************************
* 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
*******************************************************************************/
package com.ibm.wala.emf.wrappers;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.eclipse.emf.ecore.EObject;
import com.ibm.wala.ecore.common.CommonFactory;
import com.ibm.wala.ecore.common.EContainer;
import com.ibm.wala.ecore.common.EPair;
import com.ibm.wala.ecore.common.ERelation;
import com.ibm.wala.ecore.java.EClassHierarchy;
import com.ibm.wala.ecore.java.EJavaClass;
import com.ibm.wala.ecore.java.JavaFactory;
import com.ibm.wala.util.collections.HashSetFactory;
import com.ibm.wala.util.debug.Assertions;
/**
*
* This is a convenience class: it provides a view of an EClassHierarchy that
* should be more convenient for most client-side use.
*
* Note that an EMF class hierarchy only includes classes (single inheritance)
* use in conjunction with InterfaceHierarchy to look at the entire
* TypeHierarchy
*
* @author sfink
*
*/
public class EClassHierarchyWrapper extends EObjectTree {
/**
* @return an EClassHierarchy representing the contents of this object
* @see com.ibm.wala.emf.wrappers.EObjectGraphImpl#export()
*/
@Override
public EObject export() {
EClassHierarchy cha = JavaFactory.eINSTANCE.createEClassHierarchy();
makeNodes(cha);
makeEdges(cha);
return cha;
}
/**
* Create edges in this structure corresponding to the class hierarchy
* @param cha a single-inheritance class hierarchy from EMF
*/
@SuppressWarnings("unchecked")
private void makeEdges(EClassHierarchy cha) {
ERelation r = CommonFactory.eINSTANCE.createERelation();
for (Iterator it = iterator(); it.hasNext();) {
EObject src = (EObject) it.next();
for (Iterator it2 = getSuccNodes(src); it2.hasNext();) {
EObject dst = (EObject) it2.next();
EPair p = CommonFactory.eINSTANCE.createEPair();
p.setX(src);
p.setY(dst);
r.getContents().add(p);
}
}
cha.setEdges(r);
}
/**
* Create a set of nodes for this graph
* @param cha an EMF class hierarchy object
*/
private void makeNodes(EClassHierarchy cha) {
EObjectDictionary d = new EObjectDictionary();
for (Iterator it = iterator(); it.hasNext();) {
EObject o = (EObject) it.next();
d.findOrAdd(o);
}
EContainer c = (EContainer) d.export(true);
cha.setNodes(c);
}
/**
* @return a IClassHierarchy populated according to the contents of o
* @throws IllegalArgumentException if cha is null
*/
@SuppressWarnings("unchecked")
public static EClassHierarchyWrapper load(EClassHierarchy cha) {
if (cha == null) {
throw new IllegalArgumentException("cha is null");
}
EClassHierarchyWrapper result = new EClassHierarchyWrapper();
for (Iterator<EObject> it = cha.getNodes().getContents().iterator(); it.hasNext(); ) {
result.addNode(it.next());
}
for (Iterator it = cha.getEdges().getContents().iterator(); it.hasNext(); ) {
EPair p = (EPair)it.next();
result.addEdge(p.getX(),p.getY());
}
return result;
}
/**
* Add a class to this hierarchy
* @param javaKlass
*/
public void addClass(EJavaClass javaKlass) {
addNode(javaKlass);
}
/**
* Record that a child of a parent
* @param parentClass
* @param childClass
*/
public void addSubClass(EJavaClass parentClass, EJavaClass childClass) {
addEdge(parentClass,childClass);
}
public Collection<EJavaClass> getAllSuperclasses(EJavaClass klass) {
HashSet<EJavaClass> result = HashSetFactory.make();
EJavaClass superclass = getSuperclass(klass);
while (superclass != null) {
result.add(superclass);
superclass = getSuperclass(superclass);
}
return result;
}
/**
* @param klass
* @return the superclass, or null if none
*/
public EJavaClass getSuperclass(EJavaClass klass) {
if (getPredNodeCount(klass) == 0) {
return null;
} else {
if (Assertions.verifyAssertions) {
Assertions._assert(getPredNodeCount(klass) == 1);
}
return (EJavaClass)getPredNodes(klass).next();
}
}
}

View File

@ -1,119 +0,0 @@
/*******************************************************************************
* 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
*******************************************************************************/
package com.ibm.wala.emf.wrappers;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import org.eclipse.emf.ecore.EObject;
import com.ibm.wala.ecore.common.CommonFactory;
import com.ibm.wala.ecore.common.EContainer;
import com.ibm.wala.ecore.common.EPair;
import com.ibm.wala.ecore.common.ERelation;
import com.ibm.wala.ecore.java.EInterfaceHierarchy;
import com.ibm.wala.ecore.java.EJavaClass;
import com.ibm.wala.ecore.java.JavaFactory;
import com.ibm.wala.util.graph.impl.GraphInverter;
import com.ibm.wala.util.graph.traverse.DFS;
/**
*
* This is a convenience class: it provides a view of an EMF EClassHierarchy
* that should be more convenient for common client-side use.
*
* the structure of interfaces in a Java hierarchy (a DAG)
*
* @author sfink
*/
public class EInterfaceHierarchyWrapper extends EObjectGraphImpl {
@Override
public EObject export() {
EInterfaceHierarchy h = JavaFactory.eINSTANCE.createEInterfaceHierarchy();
makeNodes(h);
makeEdges(h);
return h;
}
@SuppressWarnings("unchecked")
private void makeEdges(EInterfaceHierarchy h) {
ERelation r = CommonFactory.eINSTANCE.createERelation();
for (Iterator it = iterator(); it.hasNext();) {
EObject src = (EObject) it.next();
for (Iterator it2 = getSuccNodes(src); it2.hasNext();) {
EObject dst = (EObject) it2.next();
EPair p = CommonFactory.eINSTANCE.createEPair();
p.setX(src);
p.setY(dst);
r.getContents().add(p);
}
}
h.setEdges(r);
}
private void makeNodes(EInterfaceHierarchy h) {
EObjectDictionary d = new EObjectDictionary();
for (Iterator it = iterator(); it.hasNext();) {
EObject o = (EObject) it.next();
d.findOrAdd(o);
}
EContainer c = (EContainer) d.export(true);
h.setNodes(c);
}
/**
* TODO: refactor
*/
@SuppressWarnings("unchecked")
public static EInterfaceHierarchyWrapper load(EInterfaceHierarchy h) throws IllegalArgumentException {
if (h == null) {
throw new IllegalArgumentException("h cannot be null");
}
EInterfaceHierarchyWrapper result = new EInterfaceHierarchyWrapper();
for (Iterator<EObject> it = h.getNodes().getContents().iterator(); it.hasNext();) {
result.addNode(it.next());
}
for (Iterator it = h.getEdges().getContents().iterator(); it.hasNext();) {
EPair p = (EPair) it.next();
result.addEdge(p.getX(), p.getY());
}
return result;
}
/**
* Add a class to this hierarchy
*
* @param javaKlass
*/
public void addInterface(EJavaClass javaKlass) {
addNode(javaKlass);
}
/**
* Record that a child of a parent
*
* @param parentClass
* @param childClass
*/
public void addSubClass(EJavaClass parentClass, EJavaClass childClass) {
addEdge(parentClass, childClass);
}
@SuppressWarnings("unchecked")
public Collection<EJavaClass> getAllSuperclasses(EJavaClass klass) {
EObject kludge = klass;
Collection result = DFS.getReachableNodes(GraphInverter.invert(this), Collections.singleton(kludge));
result.remove(klass);
return result;
}
}

View File

@ -10,21 +10,10 @@
*******************************************************************************/
package com.ibm.wala.emf.wrappers;
import java.io.UTFDataFormatException;
import java.util.Iterator;
import com.ibm.wala.classLoader.CallSiteReference;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.ecore.java.ECallSite;
import com.ibm.wala.ecore.java.EClassLoaderName;
import com.ibm.wala.ecore.java.EJavaClass;
import com.ibm.wala.ecore.java.EJavaMethod;
import com.ibm.wala.ecore.java.JavaFactory;
import com.ibm.wala.ipa.callgraph.impl.FakeRootMethod;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.debug.Assertions;
@ -36,56 +25,6 @@ import com.ibm.wala.util.debug.Assertions;
*/
public class EMFBridge {
/**
* @param method
* an EMF method
* @param site
* a WALA name for a call site
* @return an EMF call site
* @throws IllegalArgumentException if site is null
*/
public static ECallSite makeCallSite(EJavaMethod method, CallSiteReference site) {
if (site == null) {
throw new IllegalArgumentException("site is null");
}
ECallSite result = JavaFactory.eINSTANCE.createECallSite();
result.setBytecodeIndex(site.getProgramCounter());
result.setJavaMethod(method);
result.setDeclaredTarget(makeJavaMethod(site.getDeclaredTarget()));
return result;
}
/**
* @return EMF method representing the WALA fake root method
*/
public static EJavaMethod makeFakeRootMethod() {
return makeJavaMethod(FakeRootMethod.getRootMethod());
}
/**
* @param m
* method reference
* @return corresponding EMF method
* @throws IllegalArgumentException if m is null
*/
public static EJavaMethod makeJavaMethod(MethodReference m) {
if (m == null) {
throw new IllegalArgumentException("m is null");
}
try {
EJavaMethod result = JavaFactory.eINSTANCE.createEJavaMethod();
EJavaClass klass = makeJavaClass(m.getDeclaringClass());
result.setJavaClass(klass);
result.setDescriptor(m.getDescriptor().toUnicodeString());
result.setMethodName(m.getName().toUnicodeString());
return result;
} catch (UTFDataFormatException e) {
e.printStackTrace();
Assertions.UNREACHABLE();
return null;
}
}
public static EJavaClass makeJavaClass(TypeReference t) {
if (t == null) {
throw new IllegalArgumentException("t is null");
@ -115,102 +54,4 @@ public class EMFBridge {
}
}
/**
* @param cha
* a WALA class hierarchy
* @return a EMF class hierarchy
*/
public static com.ibm.wala.emf.wrappers.EClassHierarchyWrapper makeClassHierarchy(IClassHierarchy cha)
throws IllegalArgumentException {
if (cha == null) {
throw new IllegalArgumentException("cha must not be null");
}
com.ibm.wala.emf.wrappers.EClassHierarchyWrapper result = new com.ibm.wala.emf.wrappers.EClassHierarchyWrapper();
// create nodes
for (IClass klass : cha) {
if (!klass.isInterface()) {
EJavaClass javaKlass = makeJavaClass(klass.getReference());
result.addClass(javaKlass);
}
}
// create edges
for (IClass parent : cha) {
EJavaClass parentClass = makeJavaClass(parent.getReference());
if (!parent.isInterface()) {
for (IClass child : cha.getImmediateSubclasses(parent)) {
if (!child.isInterface()) {
EJavaClass childClass = makeJavaClass(child.getReference());
result.addSubClass(parentClass, childClass);
}
}
}
}
return result;
}
/**
* @param cha
* a WALA class hierarchy
* @return a EMF interface hierarchy
* @throws IllegalArgumentException if cha is null
*/
public static EInterfaceHierarchyWrapper makeInterfaceHierarchy(IClassHierarchy cha) {
if (cha == null) {
throw new IllegalArgumentException("cha is null");
}
EInterfaceHierarchyWrapper result = new EInterfaceHierarchyWrapper();
// create nodes
for (IClass klass : cha) {
if (klass.isInterface()) {
EJavaClass javaKlass = makeJavaClass(klass.getReference());
result.addInterface(javaKlass);
}
}
// create edges
for (IClass parent : cha) {
EJavaClass parentClass = makeJavaClass(parent.getReference());
if (parent.isInterface()) {
for (IClass child : cha.getImmediateSubclasses(parent)) {
if (child.isInterface()) {
EJavaClass childClass = makeJavaClass(child.getReference());
result.addSubClass(parentClass, childClass);
}
}
}
}
return result;
}
/**
* @param cha
* a WALA class hierarchy
* @return a EMF type hierarchy
* @throws IllegalArgumentException if cha is null
*/
public static ETypeHierarchyWrapper makeTypeHierarchy(IClassHierarchy cha) {
if (cha == null) {
throw new IllegalArgumentException("cha is null");
}
com.ibm.wala.emf.wrappers.EClassHierarchyWrapper c = makeClassHierarchy(cha);
com.ibm.wala.emf.wrappers.EInterfaceHierarchyWrapper i = makeInterfaceHierarchy(cha);
ETypeHierarchyWrapper result = new ETypeHierarchyWrapper(c, i);
for (IClass klass : cha) {
EJavaClass eklass = makeJavaClass(klass.getReference());
if (!klass.isInterface()) {
try {
for (Iterator<IClass> it2 = klass.getDirectInterfaces().iterator(); it2.hasNext();) {
IClass iface = it2.next();
EJavaClass eIface = makeJavaClass(iface.getReference());
result.recordImplements(eklass, eIface);
}
} catch (ClassHierarchyException e) {
e.printStackTrace();
Assertions.UNREACHABLE();
}
}
}
return result;
}
}

View File

@ -1,209 +0,0 @@
/*******************************************************************************
* 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
*******************************************************************************/
package com.ibm.wala.emf.wrappers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource.XMLMap;
import com.ibm.wala.ecore.common.CommonFactory;
import com.ibm.wala.ecore.common.EPair;
import com.ibm.wala.ecore.common.ERelation;
import com.ibm.wala.ecore.java.EClassHierarchy;
import com.ibm.wala.ecore.java.EInterfaceHierarchy;
import com.ibm.wala.ecore.java.EJavaClass;
import com.ibm.wala.ecore.java.ETypeHierarchy;
import com.ibm.wala.ecore.java.JavaFactory;
import com.ibm.wala.ecore.java.JavaPackage;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.collections.HashSetFactory;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.intset.BasicNaturalRelation;
import com.ibm.wala.util.intset.IntIterator;
import com.ibm.wala.util.intset.IntPair;
import com.ibm.wala.util.intset.IntSet;
/**
* This is a convenience class: it provides a view of an EClassHierarchy that
* should be more convenient for common client-side use.
*
* a container of a class hierarchy and an interface hierarchy
*
* @author sfink
*/
public class ETypeHierarchyWrapper {
private final EClassHierarchyWrapper cha;
private final EInterfaceHierarchyWrapper iface;
private final BasicNaturalRelation implementR = new BasicNaturalRelation();
/**
* @param t
* @throws IllegalArgumentException if t is null
*/
public ETypeHierarchyWrapper(ETypeHierarchy t) {
if (t == null) {
throw new IllegalArgumentException("t is null");
}
this.cha = EClassHierarchyWrapper.load(t.getClasses());
this.iface = EInterfaceHierarchyWrapper.load(t.getInterfaces());
for (Iterator it = t.getImplements().getContents().iterator(); it.hasNext();) {
EPair p = (EPair) it.next();
EJavaClass x = (EJavaClass) p.getX();
EJavaClass y = (EJavaClass) p.getY();
recordImplements(x, y);
}
}
/**
* Warning: this constructor does <em>NOT</em> set up the "implements"
* relation; the caller must do this separately.
*
*
* @param cha
* @param iface
*/
public ETypeHierarchyWrapper(EClassHierarchyWrapper cha, EInterfaceHierarchyWrapper iface) {
this.cha = cha;
this.iface = iface;
}
@SuppressWarnings("unchecked")
public EObject toEMF() {
ETypeHierarchy t = JavaFactory.eINSTANCE.createETypeHierarchy();
t.setClasses((EClassHierarchy) cha.export());
t.setInterfaces((EInterfaceHierarchy) iface.export());
ERelation impl = CommonFactory.eINSTANCE.createERelation();
for (Iterator it = implementR.iterator(); it.hasNext();) {
IntPair p = (IntPair) it.next();
EJavaClass klass = (EJavaClass) cha.getNode(p.getX());
EJavaClass interf = (EJavaClass) iface.getNode(p.getY());
EPair pp = CommonFactory.eINSTANCE.createEPair();
pp.setX(klass);
pp.setY(interf);
impl.getContents().add(pp);
}
t.setImplements(impl);
return t;
}
public static ETypeHierarchyWrapper loadFromFile(String fileName) throws FileNotFoundException {
if (fileName == null) {
throw new IllegalArgumentException("fileName is null");
}
System.err.println("eload..");
ETypeHierarchy t = eloadFromFile(fileName);
Assertions.productionAssertion(t != null);
System.err.println("ctor..");
ETypeHierarchyWrapper result = new ETypeHierarchyWrapper(t);
return result;
}
// TODO: refactor!!!
@SuppressWarnings("unchecked")
private static ETypeHierarchy eloadFromFile(String fileName) throws FileNotFoundException {
File f = new File(fileName);
InputStream s = new FileInputStream(f);
ResourceSet resSet = new ResourceSetImpl();
Resource r = resSet.createResource(URI.createURI("junk"));
Map<String, XMLMap> options = HashMapFactory.make();
try {
r.load(s, options);
} catch (IOException e) {
e.printStackTrace();
Assertions.UNREACHABLE();
}
EList contents = r.getContents();
for (Iterator<EObject> it = contents.iterator(); it.hasNext();) {
Object o = it.next();
if (o instanceof ETypeHierarchy) {
return (ETypeHierarchy) o;
}
}
return null;
}
public EClassHierarchyWrapper getClasses() {
return cha;
}
public EInterfaceHierarchyWrapper getInterfaces() {
return iface;
}
public void recordImplements(EJavaClass klass, EJavaClass i) {
int index1 = cha.getNumber(klass);
int index2 = iface.getNumber(i);
if (Assertions.verifyAssertions) {
Assertions._assert(index1 > -1);
if (index2 == -1) {
Assertions._assert(index2 > -1, "interface not found " + i);
}
}
implementR.add(index1, index2);
}
/**
* @param klass
* @return set of EJavaClass, the interfaces klass directly implements
*/
public Collection<EJavaClass> getImplements(EJavaClass klass) {
Assertions.precondition(cha.getNumber(klass) > -1, "invalid klass " + klass);
IntSet s = implementR.getRelated(cha.getNumber(klass));
if (s == null) {
return Collections.emptySet();
}
HashSet<EJavaClass> result = HashSetFactory.make(3);
for (IntIterator it = s.intIterator(); it.hasNext();) {
int x = it.next();
EJavaClass c = (EJavaClass) iface.getNode(x);
result.add(c);
}
return result;
}
/**
* @param klass
* @return all superclasses of klass.
*/
public Collection<EJavaClass> getAllSuperclasses(EJavaClass klass) {
if (cha.containsNode(klass)) {
return cha.getAllSuperclasses(klass);
} else {
return iface.getAllSuperclasses(klass);
}
}
public EClass getTargetType() {
return JavaPackage.eINSTANCE.getETypeHierarchy();
}
}

View File

@ -1106,7 +1106,7 @@ public class ClassHierarchy implements IClassHierarchy {
/**
* temporarily marking this internal to avoid infinite sleep with randomly
* chosen IProgressMonitor. TODO: nanny for testgen
* chosen IProgressMonitor.
*/
@Internal
public static ClassHierarchy make(AnalysisScope scope, IProgressMonitor monitor) throws ClassHierarchyException {
@ -1120,7 +1120,7 @@ public class ClassHierarchy implements IClassHierarchy {
/**
* temporarily marking this internal to avoid infinite sleep with randomly
* chosen IProgressMonitor. TODO: nanny for testgen
* chosen IProgressMonitor.
*/
@Internal
public static ClassHierarchy make(AnalysisScope scope, ClassLoaderFactory factory, IProgressMonitor monitor)