misc. bug fixes and annotations

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@582 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2007-01-15 21:06:37 +00:00
parent d8154e1038
commit 8942ca89cf
24 changed files with 329 additions and 274 deletions

View File

@ -13,41 +13,44 @@ package com.ibm.wala.core.plugin;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext;
import com.ibm.wala.annotations.Internal;
/**
* The main plugin class to be used in the desktop.
*/
@Internal
public class CorePlugin extends Plugin {
//The shared instance.
private static CorePlugin plugin;
/**
* The constructor.
*/
public CorePlugin() {
plugin = this;
}
// The shared instance.
private static CorePlugin plugin;
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* The constructor.
*/
public CorePlugin() {
plugin = this;
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* Returns the shared instance.
*/
public static CorePlugin getDefault() {
return plugin;
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static CorePlugin getDefault() {
return plugin;
}
}

View File

@ -15,14 +15,13 @@ import com.ibm.wala.util.warnings.WalaException;
/**
* A generic process launcher
*/
public class BasicLauncher extends Launcher {
public class BasicLauncher extends Launcher {
protected String cmd;
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @generated
*/
public BasicLauncher() {
@ -30,8 +29,8 @@ public class BasicLauncher extends Launcher {
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @generated
*/
public String getCmd() {
@ -39,8 +38,8 @@ public class BasicLauncher extends Launcher {
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @generated
*/
public void setCmd(String newCmd) {
@ -48,8 +47,8 @@ public class BasicLauncher extends Launcher {
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @generated
*/
public String toString() {
@ -64,10 +63,12 @@ public class BasicLauncher extends Launcher {
return result.toString();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see com.ibm.capa.core.impl.EAnalysisEngineImpl#processImpl()
*/
public void launch() throws WalaException {
public void launch() throws WalaException, IllegalArgumentException {
Process p = spawnProcess(getCmd());
Thread d1 = drainStdErr(p);
Thread d2 = isCaptureOutput() ? captureStdOut(p) : drainStdOut(p);
@ -78,8 +79,8 @@ public class BasicLauncher extends Launcher {
throw new WalaException("Internal error", e);
}
if (isCaptureOutput()) {
Drainer d = (Drainer)d2;
Drainer d = (Drainer) d2;
setOutput(d.getCapture().toByteArray());
}
}
}
}

View File

@ -11,10 +11,10 @@
package com.ibm.wala.fixedpoint.impl;
import java.util.HashSet;
import java.util.NoSuchElementException;
import com.ibm.wala.util.collections.HashSetFactory;
import com.ibm.wala.util.collections.Heap;
import com.ibm.wala.util.debug.Assertions;
/**
* Worklist for fixed-point solver implementation
@ -33,7 +33,7 @@ public class Worklist extends Heap {
return (eq1.getOrderNumber() < eq2.getOrderNumber());
}
public AbstractStatement takeStatement() {
public AbstractStatement takeStatement() throws NoSuchElementException {
AbstractStatement result = (AbstractStatement)super.take();
contents.remove(result);
return result;
@ -45,10 +45,5 @@ public class Worklist extends Heap {
super.insert(eq);
}
}
/* (non-Javadoc)
* @see com.ibm.wala.util.collections.Heap#insert(java.lang.Object)
*/
public void insert(Object elt) {
Assertions.UNREACHABLE();
}
}

View File

@ -30,7 +30,13 @@ public class SSAArrayLengthInstruction extends SSAInstruction {
this.arrayref = arrayref;
}
public SSAInstruction copyForSSA(int[] defs, int[] uses) {
public SSAInstruction copyForSSA(int[] defs, int[] uses) throws IllegalArgumentException {
if (defs != null && defs.length != 1) {
throw new IllegalArgumentException();
}
if (uses != null && uses.length != 1) {
throw new IllegalArgumentException();
}
return new SSAArrayLengthInstruction(defs == null ? result : defs[0], uses == null ? arrayref : uses[0]);
}

View File

@ -24,7 +24,10 @@ public class SSAThrowInstruction extends SSAAbstractThrowInstruction {
super(exception);
}
public SSAInstruction copyForSSA(int[] defs, int[] uses) {
public SSAInstruction copyForSSA(int[] defs, int[] uses) throws IllegalArgumentException {
if (uses != null && uses.length != 1) {
throw new IllegalArgumentException("if non-null, uses.length must be 1");
}
return new SSAThrowInstruction(uses==null? getException(): uses[0]);
}

View File

@ -57,7 +57,7 @@ public final class Descriptor {
* @param b a byte array holding the string representation of this descriptor
* @return the canonical representative for this descriptor value
*/
public static Descriptor findOrCreate(ImmutableByteArray b) {
public static Descriptor findOrCreate(ImmutableByteArray b) throws IllegalArgumentException {
TypeName returnType = StringStuff.parseForReturnTypeName(b);
TypeName[] parameters = StringStuff.parseForParameterNames(b);
Key k = new Key(returnType, parameters);
@ -73,7 +73,7 @@ public final class Descriptor {
* @param s string representation of this descriptor
* @return the canonical representative for this descriptor value
*/
public static Descriptor findOrCreateUTF8(String s) {
public static Descriptor findOrCreateUTF8(String s) throws IllegalArgumentException {
byte[] b = UTF8Convert.toUTF8(s);
return findOrCreate(new ImmutableByteArray(b));
}

View File

@ -11,6 +11,7 @@
package com.ibm.wala.types.generics;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.debug.UnimplementedError;
/**
* UNDER CONSTRUCTION
@ -66,11 +67,11 @@ public class TypeArgument {
return s;
}
public static TypeArgument make(String string) {
public static TypeArgument make(String string) throws UnimplementedError {
if (string.equals("*")) {
return WILDCARD;
} else {
Assertions.UNREACHABLE();
Assertions.UNREACHABLE("implement me");
return null;
}
}

View File

@ -13,6 +13,7 @@ package com.ibm.wala.util;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import com.ibm.wala.annotations.Internal;
import com.ibm.wala.util.debug.Assertions;
/**
@ -22,6 +23,7 @@ import com.ibm.wala.util.debug.Assertions;
* @author Julian Dolby
* @author sfink
*/
@Internal
public final class CacheReference {
private final static byte SOFT = 0;

View File

@ -122,11 +122,14 @@ public class StringStuff {
* method descriptor - something like "(III)V"
* @return type description
*/
public static final TypeName parseForReturnTypeName(ImmutableByteArray b) {
if (Assertions.verifyAssertions) {
if (b.get(0) != '(') {
Assertions._assert(b.get(0) == '(', "bad descriptor: " + b);
}
public static final TypeName parseForReturnTypeName(ImmutableByteArray b) throws IllegalArgumentException {
if (b.length() <= 2) {
throw new IllegalArgumentException("invalid descriptor: " + b);
}
if (b.get(0) != '(') {
throw new IllegalArgumentException("invalid descriptor: " + b);
}
int i = 0;
@ -171,12 +174,14 @@ public class StringStuff {
*
* @return parameter descriptions, or null if there are no parameters
*/
public static final TypeName[] parseForParameterNames(ImmutableByteArray b) {
public static final TypeName[] parseForParameterNames(ImmutableByteArray b) throws IllegalArgumentException {
if (Assertions.verifyAssertions) {
if (b.get(0) != '(') {
Assertions._assert(b.get(0) == '(', "bad descriptor " + b);
}
if (b.length() <= 2) {
throw new IllegalArgumentException("invalid descriptor: " + b);
}
if (b.get(0) != '(') {
throw new IllegalArgumentException("invalid descriptor: " + b);
}
ArrayList<TypeName> sigs = new ArrayList<TypeName>(10);
@ -335,7 +340,7 @@ public class StringStuff {
* @return an ImmutableByteArray that represents the package, or null if it's
* the unnamed package
*/
public static ImmutableByteArray parseForClass(ImmutableByteArray name) {
public static ImmutableByteArray parseForClass(ImmutableByteArray name) throws IllegalArgumentException {
return parseForClass(name, 0, name.length());
}
@ -345,15 +350,14 @@ public class StringStuff {
*
* @return dimensionality - something like "1" or "2"
*/
public static short parseForArrayDimensionality(ImmutableByteArray b, int start, int length) {
public static short parseForArrayDimensionality(ImmutableByteArray b, int start, int length) throws IllegalArgumentException {
for (int i = start; i < start + length; ++i) {
if (b.b[i] != '[') {
return (short) (i - start);
}
}
Assertions.UNREACHABLE();
return -1;
throw new IllegalArgumentException("ill-formed array descriptor " + b);
}
/**
@ -390,11 +394,14 @@ public class StringStuff {
* @param length
* @return true iff the class returned by parseForClass is primitive
*/
public static boolean classIsPrimitive(ImmutableByteArray name, int start, int length) {
public static boolean classIsPrimitive(ImmutableByteArray name, int start, int length) throws IllegalArgumentException {
while (length > 0 && name.b[start] == '[') {
start++;
length--;
}
if (start >= name.b.length) {
throw new IllegalArgumentException("ill-formed type name: " + name);
}
return name.b[start] != 'L';
}
@ -402,7 +409,10 @@ public class StringStuff {
* @param methodSig
* something like "java_cup.lexer.advance()V"
*/
public static MethodReference makeMethodReference(String methodSig) {
public static MethodReference makeMethodReference(String methodSig) throws IllegalArgumentException {
if (methodSig.lastIndexOf('.') < 0) {
throw new IllegalArgumentException("ill-formed sig " + methodSig);
}
String type = methodSig.substring(0, methodSig.lastIndexOf('.'));
type = deployment2CanonicalTypeString(type);
TypeReference t = TypeReference.findOrCreate(ClassLoaderReference.Application, type);
@ -420,10 +430,14 @@ public class StringStuff {
* a String containing a type name in JVM internal format.
* @return the same type name in readable (source code) format.
*/
public static String jvmToReadableType(String jvmType) {
public static String jvmToReadableType(String jvmType)throws IllegalArgumentException {
StringBuffer readable = new StringBuffer(); // human readable version
int numberOfDimensions = 0; // the number of array dimensions
if (jvmType.length() == 0) {
throw new IllegalArgumentException("ill-formed type : " + jvmType);
}
// cycle through prefixes of '['
char prefix = jvmType.charAt(0);
while (prefix == '[') {

View File

@ -50,28 +50,31 @@ public abstract class AbstractGraph<T> implements Graph<T> {
/*
* (non-Javadoc)
*/
public int getPredNodeCount(T N) {
public int getPredNodeCount(T N) throws IllegalArgumentException {
return getEdgeManager().getPredNodeCount(N);
}
/*
* (non-Javadoc)
*/
public Iterator<? extends T> getPredNodes(T N) {
public Iterator<? extends T> getPredNodes(T N) throws IllegalArgumentException {
return getEdgeManager().getPredNodes(N);
}
/*
* (non-Javadoc)
*/
public int getSuccNodeCount(T N) {
public int getSuccNodeCount(T N) throws IllegalArgumentException {
if (!containsNode(N)) {
throw new IllegalArgumentException("node not in graph " + N);
}
return getEdgeManager().getSuccNodeCount(N);
}
/*
* (non-Javadoc)
*/
public Iterator<? extends T> getSuccNodes(T N) {
public Iterator<? extends T> getSuccNodes(T N) throws IllegalArgumentException {
return getEdgeManager().getSuccNodes(N);
}
@ -90,11 +93,11 @@ public abstract class AbstractGraph<T> implements Graph<T> {
* @see com.ibm.wala.util.graph.EdgeManager#addEdge(com.ibm.wala.util.graph.Node,
* com.ibm.wala.util.graph.Node)
*/
public void addEdge(T src, T dst) {
public void addEdge(T src, T dst) throws IllegalArgumentException {
getEdgeManager().addEdge(src, dst);
}
public void removeEdge(T src, T dst) {
public void removeEdge(T src, T dst) throws IllegalArgumentException {
getEdgeManager().removeEdge(src, dst);
}
@ -110,7 +113,7 @@ public abstract class AbstractGraph<T> implements Graph<T> {
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(com.ibm.wala.util.graph.Node)
*/
public void removeAllIncidentEdges(T node) {
public void removeAllIncidentEdges(T node) throws IllegalArgumentException {
getEdgeManager().removeAllIncidentEdges(node);
}
@ -119,7 +122,7 @@ public abstract class AbstractGraph<T> implements Graph<T> {
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(com.ibm.wala.util.graph.Node)
*/
public void removeIncomingEdges(T node) {
public void removeIncomingEdges(T node) throws IllegalArgumentException {
getEdgeManager().removeIncomingEdges(node);
}
@ -128,7 +131,7 @@ public abstract class AbstractGraph<T> implements Graph<T> {
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(com.ibm.wala.util.graph.Node)
*/
public void removeOutgoingEdges(T node) {
public void removeOutgoingEdges(T node) throws IllegalArgumentException {
getEdgeManager().removeOutgoingEdges(node);
}
@ -137,7 +140,7 @@ public abstract class AbstractGraph<T> implements Graph<T> {
*
* @see com.ibm.wala.util.graph.Graph#removeNode(com.ibm.wala.util.graph.Node)
*/
public void removeNodeAndEdges(T N) {
public void removeNodeAndEdges(T N) throws IllegalArgumentException {
getEdgeManager().removeAllIncidentEdges(N);
getNodeManager().removeNode(N);
}

View File

@ -62,13 +62,13 @@ public abstract class AbstractNumberedGraph<T> extends AbstractGraph<T> implemen
/* (non-Javadoc)
*/
public IntSet getPredNodeNumbers(T node) {
public IntSet getPredNodeNumbers(T node) throws IllegalArgumentException {
return ((NumberedEdgeManager<T>) getEdgeManager()).getPredNodeNumbers(node);
}
/* (non-Javadoc)
*/
public IntSet getSuccNodeNumbers(T node) {
public IntSet getSuccNodeNumbers(T node) throws IllegalArgumentException {
return ((NumberedEdgeManager<T>) getEdgeManager()).getSuccNodeNumbers(node);
}
}

View File

@ -21,25 +21,13 @@ import com.ibm.wala.util.intset.IntSet;
* @author sfink
*/
public interface NumberedNodeManager<T> extends NodeManager<T> {
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.NumberedGraph#getNumber(com.ibm.wala.util.graph.Node)
*/
public abstract int getNumber(T N);
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.NumberedGraph#getNode(int)
*/
public abstract T getNode(int number);
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.NumberedGraph#getMaxNumber()
*/
public abstract int getMaxNumber();
/**

View File

@ -164,16 +164,6 @@ public class DelegatingNumberedEdgeManager<T extends INodeWithNumber> implements
n.removeOutgoingEdges();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
// TODO Auto-generated method stub
Assertions.UNREACHABLE();
return super.toString();
}
/*
* (non-Javadoc)

View File

@ -11,6 +11,7 @@
package com.ibm.wala.util.graph.impl;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.debug.UnimplementedError;
import com.ibm.wala.util.graph.INodeWithNumberedEdges;
import com.ibm.wala.util.intset.BimodalMutableIntSet;
import com.ibm.wala.util.intset.IntSet;
@ -21,27 +22,32 @@ import com.ibm.wala.util.intset.IntSet;
public class NodeWithNumberedEdges extends NodeWithNumber implements INodeWithNumberedEdges {
private BimodalMutableIntSet predNumbers;
private BimodalMutableIntSet succNumbers;
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see com.ibm.wala.dataflow.fixpoint.IVariable#getUseNumbers()
*/
public IntSet getSuccNumbers() {
return succNumbers;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see com.ibm.wala.dataflow.fixpoint.IVariable#getDefNumbers()
*/
public IntSet getPredNumbers() {
return predNumbers;
}
/**
/**
* Note that this variable appears on the RHS of an equation.
*
* @param eqNumber the equation number
*
* @param eqNumber
* the equation number
*/
public void addSucc(int eqNumber) {
if (succNumbers == null) {
@ -52,10 +58,11 @@ public class NodeWithNumberedEdges extends NodeWithNumber implements INodeWithNu
}
}
/**
* Note that this variable appears on the LHS of an equation.
*
* @param eqNumber the equation number
/**
* Note that this variable appears on the LHS of an equation.
*
* @param eqNumber
* the equation number
*/
public void addPred(int eqNumber) {
if (predNumbers == null) {
@ -65,10 +72,9 @@ public class NodeWithNumberedEdges extends NodeWithNumber implements INodeWithNu
predNumbers.add(eqNumber);
}
}
/**
* remove the edge that indicates this variable is Succd by a certain
* equation
* remove the edge that indicates this variable is Succd by a certain equation
*
* @param eqNumber
*/
@ -80,6 +86,7 @@ public class NodeWithNumberedEdges extends NodeWithNumber implements INodeWithNu
}
}
}
/**
* remove the edge that indicates this variable is Predined by a certain
* equation
@ -95,30 +102,28 @@ public class NodeWithNumberedEdges extends NodeWithNumber implements INodeWithNu
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.INodeWithNumberedEdges#removeAllIncidentEdges()
*/
public void removeAllIncidentEdges() {
// TODO Auto-generated method stub
Assertions.UNREACHABLE();
public void removeAllIncidentEdges() throws UnimplementedError {
Assertions.UNREACHABLE("Implement me");
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*/
public void removeIncomingEdges() {
// TODO Auto-generated method stub
Assertions.UNREACHABLE();
public void removeIncomingEdges() throws UnimplementedError {
Assertions.UNREACHABLE("Implement me");
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*/
public void removeOutgoingEdges() {
// TODO Auto-generated method stub
Assertions.UNREACHABLE();
public void removeOutgoingEdges() throws UnimplementedError {
Assertions.UNREACHABLE("Implement me");
}
}

View File

@ -30,26 +30,14 @@ public class SlowNumberedNodeManager<T> implements NumberedNodeManager<T> {
*/
private MutableMapping<T> map = new MutableMapping<T>();
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.NumberedGraph#getNumber(com.ibm.wala.util.graph.Node)
*/
public int getNumber(Object T) {
return map.getMappedIndex(T);
}
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.NumberedGraph#getNode(int)
*/
public T getNode(int number) {
T result = (T) map.getMappedObject(number);
if (Assertions.verifyAssertions) {
Assertions._assert(getNumber(result) == number);
}
return result;
}
@ -62,20 +50,12 @@ public class SlowNumberedNodeManager<T> implements NumberedNodeManager<T> {
return map.getMappingSize() - 1;
}
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.Graph#iterateNodes()
*/
public Iterator<T> iterateNodes() {
return map.iterator();
}
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.Graph#getNumberOfNodes()
*/
public int getNumberOfNodes() {
return map.getMappingSize();
}

View File

@ -14,7 +14,6 @@ import java.util.Arrays;
import java.util.Iterator;
import com.ibm.wala.util.collections.EmptyIterator;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.graph.NumberedEdgeManager;
import com.ibm.wala.util.graph.NumberedNodeManager;
import com.ibm.wala.util.intset.BasicNonNegativeIntRelation;
@ -36,19 +35,23 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
* cache this state here for efficiency
*/
private final BitVector hasSuccessor = new BitVector();
/**
* @param nodeManager an object to track nodes
* @param nodeManager
* an object to track nodes
*/
public SparseNumberedEdgeManager(NumberedNodeManager<T> nodeManager) {
this(nodeManager,0,BasicNonNegativeIntRelation.TWO_LEVEL);
this(nodeManager, 0, BasicNonNegativeIntRelation.TWO_LEVEL);
}
/**
* If normalOutCount == n, this edge manager will eagerly allocated n words
* to hold out edges for each node. (performance optimization for time)
* @param nodeManager an object to track nodes
* @param normalCase what is the "normal" number of out edges for a node?
* If normalOutCount == n, this edge manager will eagerly allocated n words to
* hold out edges for each node. (performance optimization for time)
*
* @param nodeManager
* an object to track nodes
* @param normalCase
* what is the "normal" number of out edges for a node?
*/
public SparseNumberedEdgeManager(NumberedNodeManager<T> nodeManager, int normalCase, byte delegateImpl) {
this.nodeManager = nodeManager;
@ -57,17 +60,20 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
predecessors = new BasicNonNegativeIntRelation(defaultImpl, delegateImpl);
} else {
byte[] impl = new byte[normalCase];
Arrays.fill(impl,BasicNonNegativeIntRelation.SIMPLE);
Arrays.fill(impl, BasicNonNegativeIntRelation.SIMPLE);
successors = new BasicNonNegativeIntRelation(impl, delegateImpl);
predecessors = new BasicNonNegativeIntRelation(impl, delegateImpl);
}
}
/**
* The default implementation policy conservatively uses 2-level vectors, in an attempt to somewhat optimize for space.
* The default implementation policy conservatively uses 2-level vectors, in
* an attempt to somewhat optimize for space.
*/
private final static byte[] defaultImpl = new byte[] {BasicNonNegativeIntRelation.TWO_LEVEL};
private final static byte[] defaultImpl = new byte[] { BasicNonNegativeIntRelation.TWO_LEVEL };
private final IBinaryNonNegativeIntRelation successors;
private final IBinaryNonNegativeIntRelation predecessors;
/*
@ -75,12 +81,10 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
*
* @see com.ibm.wala.util.graph.EdgeManager#getPredNodes(java.lang.Object)
*/
public Iterator<T> getPredNodes(T N) {
public Iterator<T> getPredNodes(T N) throws IllegalArgumentException {
int number = nodeManager.getNumber(N);
if (Assertions.verifyAssertions) {
if (number < 0) {
Assertions.UNREACHABLE("number " + number + " for " + N);
}
if (number < 0) {
throw new IllegalArgumentException(N + " is not in graph");
}
IntSet s = predecessors.getRelated(number);
Iterator<T> empty = EmptyIterator.instance();
@ -92,8 +96,11 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
*
* @see com.ibm.wala.util.graph.EdgeManager#getPredNodeCount(java.lang.Object)
*/
public int getPredNodeCount(T N) {
public int getPredNodeCount(T N) throws IllegalArgumentException {
int number = nodeManager.getNumber(N);
if (number < 0) {
throw new IllegalArgumentException(N + " is not in graph");
}
return predecessors.getRelatedCount(number);
}
@ -102,16 +109,16 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
*
* @see com.ibm.wala.util.graph.EdgeManager#getSuccNodes(java.lang.Object)
*/
public Iterator<T> getSuccNodes(T N) {
public Iterator<T> getSuccNodes(T N) throws IllegalArgumentException {
int number = nodeManager.getNumber(N);
if (number == -1) {
Assertions.UNREACHABLE("asked for successors of " + N + " which is not in graph");
throw new IllegalArgumentException(N + " is not in graph");
}
IntSet s = successors.getRelated(number);
Iterator<T> empty = EmptyIterator.instance();
return (s == null) ? empty : nodeManager.iterateNodes(s);
}
/*
* (non-Javadoc)
*
@ -123,21 +130,26 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
return (s == null) ? empty : nodeManager.iterateNodes(s);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*/
public IntSet getSuccNodeNumbers(T node) {
public IntSet getSuccNodeNumbers(T node) throws IllegalArgumentException {
if (nodeManager.getNumber(node) < 0) {
throw new IllegalArgumentException("Node not in graph " + node);
}
return successors.getRelated(nodeManager.getNumber(node));
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*/
public IntSet getPredNodeNumbers(T node) {
public IntSet getPredNodeNumbers(T node) throws IllegalArgumentException {
if (nodeManager.getNumber(node) < 0) {
throw new IllegalArgumentException("Node not in graph " + node);
}
return predecessors.getRelated(nodeManager.getNumber(node));
}
/*
* (non-Javadoc)
*
@ -146,7 +158,7 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
public int getSuccNodeCount(T N) {
return getSuccNodeCount(nodeManager.getNumber(N));
}
/*
* (non-Javadoc)
*
@ -162,20 +174,26 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
* @see com.ibm.wala.util.graph.EdgeManager#addEdge(java.lang.Object,
* java.lang.Object)
*/
public void addEdge(T src, T dst) {
public void addEdge(T src, T dst) throws IllegalArgumentException {
int x = nodeManager.getNumber(src);
int y = nodeManager.getNumber(dst);
if (x < 0 || y < 0) {
throw new IllegalArgumentException();
}
predecessors.add(y, x);
successors.add(x, y);
hasSuccessor.set(x);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*/
public boolean hasEdge(T src, T dst) {
int x = nodeManager.getNumber(src);
int y = nodeManager.getNumber(dst);
if (x < 0 || y < 0) {
return false;
}
return successors.contains(x, y);
}
@ -184,8 +202,11 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(java.lang.Object)
*/
public void removeAllIncidentEdges(T node) {
public void removeAllIncidentEdges(T node) throws IllegalArgumentException {
final int number = nodeManager.getNumber(node);
if (number < 0) {
throw new IllegalArgumentException("node not in graph: " + node);
}
IntSet succ = successors.getRelated(number);
if (succ != null) {
succ.foreach(new IntSetAction() {
@ -209,14 +230,17 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
hasSuccessor.clear(number);
predecessors.removeAll(number);
}
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(java.lang.Object)
*/
public void removeIncomingEdges(T node) {
public void removeIncomingEdges(T node) throws IllegalArgumentException {
final int number = nodeManager.getNumber(node);
if (number < 0) {
throw new IllegalArgumentException("node not in graph: " + node);
}
IntSet pred = predecessors.getRelated(number);
if (pred != null) {
pred.foreach(new IntSetAction() {
@ -230,24 +254,33 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
}
predecessors.removeAll(number);
}
public void removeEdge(T src, T dst) {
public void removeEdge(T src, T dst) throws IllegalArgumentException {
final int srcNumber = nodeManager.getNumber(src);
final int dstNumber = nodeManager.getNumber(dst);
if (srcNumber < 0) {
throw new IllegalArgumentException("src not in graph: " + src);
}
if (dstNumber < 0) {
throw new IllegalArgumentException("dst not in graph: " + dst);
}
successors.remove(srcNumber, dstNumber);
if (successors.getRelatedCount(srcNumber) == 0) {
hasSuccessor.clear(srcNumber);
}
predecessors.remove(dstNumber, srcNumber);
}
/*
* (non-Javadoc)
*
* @see com.ibm.wala.util.graph.EdgeManager#removeEdges(java.lang.Object)
*/
public void removeOutgoingEdges(T node) {
public void removeOutgoingEdges(T node) throws IllegalArgumentException {
final int number = nodeManager.getNumber(node);
if (number < 0) {
throw new IllegalArgumentException("node not in graph: " + node);
}
IntSet succ = successors.getRelated(number);
if (succ != null) {
succ.foreach(new IntSetAction() {
@ -259,22 +292,24 @@ public final class SparseNumberedEdgeManager<T> implements NumberedEdgeManager<T
successors.removeAll(number);
hasSuccessor.clear(number);
}
/**
* This is implemented as a shortcut for efficiency
*
* @param node
* @return true iff that node has any successors
*/
public boolean hasAnySuccessor(int node) {
return hasSuccessor.get(node);
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return "Successors relation:\n" + successors;
}
}

View File

@ -128,13 +128,13 @@ public final class BasicNonNegativeIntRelation implements IBinaryNonNegativeIntR
* @param y
* @return true iff the relation changes as a result of this call.
*/
public boolean add(int x, int y) {
public boolean add(int x, int y) throws IllegalArgumentException {
if (Assertions.verifyAssertions) {
if (x < 0) {
Assertions.UNREACHABLE();
throw new IllegalArgumentException("illegal x: " + x);
}
if (y < 0) {
Assertions.UNREACHABLE();
throw new IllegalArgumentException("illegal y: " + y);
}
}
maxX = Math.max(maxX, x);

View File

@ -21,33 +21,32 @@ import com.ibm.wala.util.debug.Trace;
public class IntSetUtil {
public static final String INT_SET_FACTORY_CONFIG_PROPERTY_NAME = "com.ibm.wala.mutableIntSetFactory";
private static MutableIntSetFactory defaultIntSetFactory;
static {
MutableIntSetFactory defaultFactory = new MutableSharedBitVectorIntSetFactory();
if (System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME) != null) {
try {
Class intSetFactoryClass = Class.forName(System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME));
MutableIntSetFactory intSetFactory = (MutableIntSetFactory) intSetFactoryClass.newInstance();
setDefaultIntSetFactory(intSetFactory);
} catch (Exception e) {
Trace.println("Cannot use int set factory " + System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME));
setDefaultIntSetFactory(defaultFactory);
}
} else {
setDefaultIntSetFactory(defaultFactory);
}
assert defaultIntSetFactory != null;
MutableIntSetFactory defaultFactory = new MutableSharedBitVectorIntSetFactory();
if (System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME) != null) {
try {
Class intSetFactoryClass = Class.forName(System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME));
MutableIntSetFactory intSetFactory = (MutableIntSetFactory) intSetFactoryClass.newInstance();
setDefaultIntSetFactory(intSetFactory);
} catch (Exception e) {
Trace.println("Cannot use int set factory " + System.getProperty(INT_SET_FACTORY_CONFIG_PROPERTY_NAME));
setDefaultIntSetFactory(defaultFactory);
}
} else {
setDefaultIntSetFactory(defaultFactory);
}
assert defaultIntSetFactory != null;
}
public static MutableIntSet make() {
return defaultIntSetFactory.make();
}
private final static boolean DEBUG = false;
/**
* This method constructs an appropriate mutable copy of set.
*
@ -66,9 +65,9 @@ public class IntSetUtil {
} else if (set instanceof SemiSparseMutableIntSet) {
return new SemiSparseMutableIntSet((SemiSparseMutableIntSet) set);
} else if (set instanceof DebuggingMutableIntSet) {
MutableIntSet pCopy = makeMutableCopy(((DebuggingMutableIntSet)set).primaryImpl);
MutableIntSet sCopy = makeMutableCopy(((DebuggingMutableIntSet)set).secondaryImpl);
return new DebuggingMutableIntSet(pCopy, sCopy);
MutableIntSet pCopy = makeMutableCopy(((DebuggingMutableIntSet) set).primaryImpl);
MutableIntSet sCopy = makeMutableCopy(((DebuggingMutableIntSet) set).secondaryImpl);
return new DebuggingMutableIntSet(pCopy, sCopy);
} else {
Assertions.UNREACHABLE(set.getClass().toString());
return null;
@ -120,19 +119,14 @@ public class IntSetUtil {
}
return SparseIntSet.diff((SparseIntSet) A, (SparseIntSet) B);
} else if (A instanceof SemiSparseMutableIntSet &&
B instanceof SemiSparseMutableIntSet)
{
} else if (A instanceof SemiSparseMutableIntSet && B instanceof SemiSparseMutableIntSet) {
if (DEBUG) {
Trace.println("call SemiSparseMutableIntSet.diff");
Trace.println("call SemiSparseMutableIntSet.diff");
}
IntSet d =
SemiSparseMutableIntSet.diff(
(SemiSparseMutableIntSet) A,
(SemiSparseMutableIntSet) B);
IntSet d = SemiSparseMutableIntSet.diff((SemiSparseMutableIntSet) A, (SemiSparseMutableIntSet) B);
if (DEBUG) {
Assertions._assert(d.sameValue(defaultSlowDiff(A, B, factory)));
Assertions._assert(d.sameValue(defaultSlowDiff(A, B, factory)));
}
return d;
@ -148,24 +142,21 @@ public class IntSetUtil {
* @param B
*/
public static MutableIntSet removeAll(MutableIntSet A, IntSet B) {
if (A instanceof SemiSparseMutableIntSet &&
B instanceof SemiSparseMutableIntSet)
{
if (A instanceof SemiSparseMutableIntSet && B instanceof SemiSparseMutableIntSet) {
if (DEBUG) {
Trace.println("call SemiSparseMutableIntSet.removeAll");
}
return
((SemiSparseMutableIntSet) A).removeAll((SemiSparseMutableIntSet) B);
return ((SemiSparseMutableIntSet) A).removeAll((SemiSparseMutableIntSet) B);
} else {
for (IntIterator it = B.intIterator(); it.hasNext();) {
int I = it.next();
A.remove(I);
if (DEBUG) {
Trace.println("removed " + I + " now is " + A);
}
A.remove(I);
if (DEBUG) {
Trace.println("removed " + I + " now is " + A);
}
}
if (DEBUG) {
Trace.println("return " + A);
Trace.println("return " + A);
}
return A;
}
@ -178,32 +169,43 @@ public class IntSetUtil {
* @param high
* @return index \in [low,high] s.t. data[index] = key, or -1 if not found
*/
public static int binarySearch(int[] data, int key, int low, int high) {
public static int binarySearch(int[] data, int key, int low, int high) throws IllegalArgumentException {
if (data == null) {
throw new IllegalArgumentException("null array");
}
if (data.length == 0) {
return -1;
}
if (low <= high) {
int mid = (low + high) / 2;
int midValue = data[mid];
if (midValue == key)
if (midValue == key) {
return mid;
else if (midValue > key)
} else if (midValue > key) {
return binarySearch(data, key, low, mid - 1);
else
} else {
return binarySearch(data, key, mid + 1, high);
}
} else {
return -1;
}
}
/**
* @return Returns the defaultIntSetFactory.
*/
public static MutableIntSetFactory getDefaultIntSetFactory() {
return defaultIntSetFactory;
}
/**
* @param defaultIntSetFactory The defaultIntSetFactory to set.
* @param defaultIntSetFactory
* The defaultIntSetFactory to set.
*/
public static void setDefaultIntSetFactory(MutableIntSetFactory defaultIntSetFactory) {
IntSetUtil.defaultIntSetFactory = defaultIntSetFactory;
}
/**
* @param s
* @param j
@ -211,8 +213,8 @@ public class IntSetUtil {
*/
public static IntSet add(IntSet s, int j) {
if (s instanceof SparseIntSet) {
SparseIntSet sis = (SparseIntSet)s;
return SparseIntSet.add(sis,j);
SparseIntSet sis = (SparseIntSet) s;
return SparseIntSet.add(sis, j);
} else {
Assertions.UNREACHABLE("implement me");
return null;

View File

@ -10,7 +10,6 @@
*******************************************************************************/
package com.ibm.wala.util.intset;
/**
*
* Utilities for dealing with LongSets
@ -19,7 +18,6 @@ package com.ibm.wala.util.intset;
*/
public class LongSetUtil {
/**
* @param data
* @param key
@ -27,16 +25,23 @@ public class LongSetUtil {
* @param high
* @return index \in [low,high] s.t. data[index] = key, or -1 if not found
*/
public static int binarySearch(long[] data, long key, int low, int high) {
public static int binarySearch(long[] data, long key, int low, int high) throws IllegalArgumentException {
if (data == null) {
throw new IllegalArgumentException("null array");
}
if (data.length == 0) {
return -1;
}
if (low <= high) {
int mid = (low + high) / 2;
long midValue = data[mid];
if (midValue == key)
if (midValue == key) {
return mid;
else if (midValue > key)
} else if (midValue > key) {
return binarySearch(data, key, low, mid - 1);
else
} else {
return binarySearch(data, key, mid + 1, high);
}
} else {
return -1;
}

View File

@ -60,11 +60,11 @@ public class MutableSparseIntSet extends SparseIntSet implements MutableIntSet {
*
* @param initialCapacity
*/
public MutableSparseIntSet(int initialCapacity) {
public MutableSparseIntSet(int initialCapacity) throws IllegalArgumentException {
super(new int[initialCapacity]);
size = 0;
if (Assertions.verifyAssertions) {
Assertions._assert(initialCapacity > 0);
if (initialCapacity <= 0) {
throw new IllegalArgumentException("initialCapacity must be positive");
}
}

View File

@ -42,7 +42,7 @@ public class SemiSparseMutableIntSetFactory implements MutableIntSetFactory {
/**
* @param string
*/
public MutableIntSet parse(String string) {
public MutableIntSet parse(String string) throws NumberFormatException {
int[] data = SparseIntSet.parseIntArray(string);
MutableIntSet result = new SemiSparseMutableIntSet();
for (int i = 0; i < data.length; i++)

View File

@ -120,6 +120,9 @@ public class SparseIntSet implements IntSet {
* @see com.ibm.wala.util.intset.IntSet#contains(int)
*/
public final boolean contains(int x) {
if (elements == null) {
return false;
}
return IntSetUtil.binarySearch(elements, x, 0, size - 1) >= 0;
}
@ -128,6 +131,9 @@ public class SparseIntSet implements IntSet {
* @return index i s.t. elements[i] == x, or -1 if not found.
*/
public final int getIndex(int x) {
if (elements == null) {
return -1;
}
return IntSetUtil.binarySearch(elements, x, 0, size - 1);
}
@ -449,6 +455,9 @@ public class SparseIntSet implements IntSet {
*/
public static SparseIntSet add(SparseIntSet s, int j) {
if (s.elements == null) {
return SparseIntSet.singleton(j);
}
SparseIntSet result = new SparseIntSet(s.size + 1);
int k = 0;
int m = 0;

View File

@ -116,6 +116,9 @@ public class SparseLongSet implements LongSet {
* @see com.ibm.wala.util.intset.IntSet#contains(int)
*/
public final boolean contains(long x) {
if (elements == null) {
return false;
}
return LongSetUtil.binarySearch(elements, x, 0, size - 1) >= 0;
}
@ -124,6 +127,9 @@ public class SparseLongSet implements LongSet {
* @return index i s.t. elements[i] == x, or -1 if not found.
*/
public final int getIndex(long x) {
if (elements == null) {
return -1;
}
return LongSetUtil.binarySearch(elements, x, 0, size - 1);
}
@ -299,10 +305,14 @@ public class SparseLongSet implements LongSet {
/**
* Reverse of toString(): "{2,3}" -> [2,3]
*/
public static long[] parseLongArray(String str) throws NumberFormatException {
public static long[] parseLongArray(String str) throws NumberFormatException, IllegalArgumentException {
int len = str.length();
if (str.charAt(0) != '{' || str.charAt(len - 1) != '}')
if (len < 2) {
throw new IllegalArgumentException("malformed input: " + str);
}
if (str.charAt(0) != '{' || str.charAt(len - 1) != '}') {
throw new NumberFormatException(str);
}
str = str.substring(1, len - 1);
StringTokenizer tok = new StringTokenizer(str, " ,");
@ -363,12 +373,12 @@ public class SparseLongSet implements LongSet {
}
/**
* @param s
* @param j
* @return a new sparse int set which adds j to s
*/
public static SparseLongSet add(SparseLongSet s, int j) {
if (s == null || s.elements == null) {
return SparseLongSet.singleton(j);
}
SparseLongSet result = new SparseLongSet(s.size + 1);
int k = 0;
int m = 0;

View File

@ -19,8 +19,11 @@ public class TunedMutableSparseIntSet extends MutableSparseIntSet {
private final int initialSize;
private final float expansion;
public TunedMutableSparseIntSet(int initialSize, float expansion) {
public TunedMutableSparseIntSet(int initialSize, float expansion) throws IllegalArgumentException {
super();
if (initialSize <= 0) {
throw new IllegalArgumentException("invalid initial size " + initialSize);
}
this.initialSize = initialSize;
this.expansion = expansion;
}