Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed Jan 3, 2025
1 parent f3eeb2e commit 962354d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/com/ibm/wala/types/generics/TypeSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ public static TypeSignature make(String s) throws IllegalArgumentException {
* signatures. The string should start with either {@code (} or {@code <} and have a respective
* matching {@code )} or {@code >}.
*
* <p>TODO handle wildcards
*
* <p>TODO test on all methods in JDK
*
* @param typeSigs a string of consecutive type signatures
* @return an array of top-level type signatures
*/
Expand Down Expand Up @@ -189,6 +185,16 @@ public static String[] parseForTypeSignatures(String typeSigs) throws IllegalArg
result[j] = it.next();
}
return result;
case (byte) '*': // unbounded wildcard
sigs.add("*");
break;
case (byte) '-': // bounded wildcard
case (byte) '+': // bounded wildcard
int boundedStart = i - 1;
i++; // to skip 'L'
i = getEndIndexOfClassType(typeSigs, i);
sigs.add(typeSigs.substring(boundedStart, i));
break;
default:
throw new IllegalArgumentException("bad type signature list " + typeSigs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.is;

import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.classLoader.ShrikeCTMethod;
import com.ibm.wala.core.tests.callGraph.CallGraphTestUtil;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
import com.ibm.wala.shrike.shrikeCT.InvalidClassFileException;
import java.io.IOException;
import org.junit.jupiter.api.Test;

public class TypeSignatureTest {
Expand All @@ -21,4 +32,43 @@ void multiDimArray() {
TypeSignature.parseForTypeSignatures("<[[Ljava/lang/String;[[[J>"),
arrayContaining(is("[[Ljava/lang/String;"), is("[[[J")));
}

@Test
void wildcards() {
assertThat(
TypeSignature.parseForTypeSignatures("<B*J>"), arrayContaining(is("B"), is("*"), is("J")));
assertThat(
TypeSignature.parseForTypeSignatures("<+Ljava/lang/Object;>"),
arrayContaining(is("+Ljava/lang/Object;")));
assertThat(
TypeSignature.parseForTypeSignatures("<-Ljava/lang/Double;BB>"),
arrayContaining(is("-Ljava/lang/Double;"), is("B"), is("B")));
}

@Test
void testAllGenericMethodSigs()
throws IOException, ClassHierarchyException, InvalidClassFileException {
AnalysisScope scope =
CallGraphTestUtil.makeJ2SEAnalysisScope(
TestConstants.WALA_TESTDATA, "J2SEClassHierarchyExclusions.txt");
ClassHierarchy cha = ClassHierarchyFactory.make(scope);
for (IClass klass : cha) {
for (IMethod m : klass.getDeclaredMethods()) {
if (m instanceof ShrikeCTMethod) {
ShrikeCTMethod method = (ShrikeCTMethod) m;
MethodTypeSignature methodTypeSignature = method.getMethodTypeSignature();
if (methodTypeSignature != null) {
String typeSigStr = methodTypeSignature.toString();
for (int i = 0; i < typeSigStr.length(); i++) {
if ((typeSigStr.charAt(i) == '<' && i != 0) || typeSigStr.charAt(i) == '(') {
// parsing will automatically end at the matching '>' or ')'
// this is just testing for crashes
TypeSignature.parseForTypeSignatures(typeSigStr.substring(i));
}
}
}
}
}
}
}
}

0 comments on commit 962354d

Please sign in to comment.