/** * Find or create an atom. * * @param str atom value, as string literal whose characters are unicode * @return atom */ public static Atom findOrCreateUnicodeAtom(String str) { byte[] utf8 = UTF8Convert.toUTF8(str); return findOrCreate(utf8); }
/** * Find or create an atom. * * @param str atom value, as string literal whose characters are unicode * @return atom */ public static Atom findOrCreateUnicodeAtom(String str) { byte[] utf8 = UTF8Convert.toUTF8(str); return findOrCreate(utf8); }
public static ImmutableByteArray make(String s) { return new ImmutableByteArray(UTF8Convert.toUTF8(s)); }
public static ImmutableByteArray make(String s) { return new ImmutableByteArray(UTF8Convert.toUTF8(s)); }
/** * @param s string representation of this descriptor * @return the canonical representative for this descriptor value */ public static Descriptor findOrCreateUTF8(Language l, String s) throws IllegalArgumentException { byte[] b = UTF8Convert.toUTF8(s); return findOrCreate(l, new ImmutableByteArray(b)); }
/** * @param s string representation of this descriptor * @return the canonical representative for this descriptor value */ public static Descriptor findOrCreateUTF8(Language l, String s) throws IllegalArgumentException { byte[] b = UTF8Convert.toUTF8(s); return findOrCreate(l, new ImmutableByteArray(b)); }
@Test public void testIR3() { // 'resolveProxyClass' is a nice long method (at least in Sun libs) doMethod(cha.getScope().findMethod(AnalysisScope.APPLICATION, "Ljava/io/ObjectInputStream", Atom .findOrCreateUnicodeAtom("resolveProxyClass"), new ImmutableByteArray(UTF8Convert .toUTF8("([Ljava/lang/String;)Ljava/lang/Class;")))); }
@Test public void testIR2() { // 'equals' is a nice medium-sized method doMethod(cha.getScope().findMethod(AnalysisScope.APPLICATION, "Ljava/lang/String", Atom.findOrCreateUnicodeAtom("equals"), new ImmutableByteArray(UTF8Convert.toUTF8("(Ljava/lang/Object;)Z")))); }
@Test public void testIR1() { // 'remove' is a nice short method doMethod(cha.getScope().findMethod(AnalysisScope.APPLICATION, "Ljava/util/HashMap", Atom.findOrCreateUnicodeAtom("remove"), new ImmutableByteArray(UTF8Convert.toUTF8("(Ljava/lang/Object;)Ljava/lang/Object;")))); }
@Test public void testIR4() { // test some corner cases with try-finally doMethod(cha.getScope().findMethod(AnalysisScope.APPLICATION, "LcornerCases/TryFinally", Atom.findOrCreateUnicodeAtom("test1"), new ImmutableByteArray(UTF8Convert.toUTF8("(Ljava/io/InputStream;Ljava/io/InputStream;)V")))); } }
@Test public void test3() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("inferInt"), new ImmutableByteArray(UTF8Convert.toUTF8("()V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, true); TypeAbstraction type = ti.getType(7); Assert.assertNotNull("null type abstraction", type); Assert.assertTrue("inferred wrong type", type.toString().equals("int")); }
@Test public void test4() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("useCast"), new ImmutableByteArray(UTF8Convert.toUTF8("(Ljava/lang/Object;)V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, false); TypeAbstraction type = ti.getType(4); Assert.assertNotNull("null type abstraction", type); Assert.assertTrue("inferred wrong type " + type, type instanceof ConeType && ((ConeType)type).getTypeReference().getName().toString().equals("Ljava/lang/String")); }
@Test public void test1() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("()V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, false); for (int i = 1; i <= ir.getSymbolTable().getMaxValueNumber(); i++) { System.err.println(i + " " + ti.getType(i)); } }
@Test public void test2() { MethodReference method = scope.findMethod(AnalysisScope.APPLICATION, "LtypeInference/TI", Atom.findOrCreateUnicodeAtom("bar"), new ImmutableByteArray(UTF8Convert.toUTF8("(I)V"))); Assert.assertNotNull("method not found", method); IMethod imethod = cha.resolveMethod(method); Assert.assertNotNull("imethod not found", imethod); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); System.out.println(ir); TypeInference ti = TypeInference.make(ir, true); Assert.assertNotNull("null type abstraction for parameter", ti.getType(2)); }
@Test public void testLocalNamesWithoutPiNodes() { SSAPiNodePolicy save = options.getSSAOptions().getPiNodePolicy(); options.getSSAOptions().setPiNodePolicy(null); MethodReference mref = scope.findMethod(AnalysisScope.APPLICATION, "LcornerCases/Locals", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("([Ljava/lang/String;)V"))); Assert.assertNotNull("method not found", mref); IMethod imethod = cha.resolveMethod(mref); Assert.assertNotNull("imethod not found", imethod); IAnalysisCacheView cache = new AnalysisCacheImpl(options.getSSAOptions()); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); options.getSSAOptions().setPiNodePolicy(save); // v1 should be the parameter "a" at pc 0 String[] names = ir.getLocalNames(0, 1); Assert.assertTrue("failed local name resolution for v1@0", names != null); Assert.assertTrue("incorrect number of local names for v1@0: " + names.length, names.length == 1); Assert.assertTrue("incorrect local name resolution for v1@0: " + names[0], names[0].equals("a")); // v2 is a compiler-induced temporary Assert.assertTrue("didn't expect name for v2 at pc 2", ir.getLocalNames(2, 2) == null); // at pc 5, v1 should represent the locals "a" and "b" names = ir.getLocalNames(5, 1); Assert.assertTrue("failed local name resolution for v1@5", names != null); Assert.assertTrue("incorrect number of local names for v1@5: " + names.length, names.length == 2); Assert.assertTrue("incorrect local name resolution #0 for v1@5: " + names[0], names[0].equals("a")); Assert.assertTrue("incorrect local name resolution #1 for v1@5: " + names[1], names[1].equals("b")); }
@Test public void testLocalNamesWithPiNodes() { SSAPiNodePolicy save = options.getSSAOptions().getPiNodePolicy(); options.getSSAOptions().setPiNodePolicy(SSAOptions.getAllBuiltInPiNodes()); MethodReference mref = scope.findMethod(AnalysisScope.APPLICATION, "LcornerCases/Locals", Atom.findOrCreateUnicodeAtom("foo"), new ImmutableByteArray(UTF8Convert.toUTF8("([Ljava/lang/String;)V"))); Assert.assertNotNull("method not found", mref); IMethod imethod = cha.resolveMethod(mref); Assert.assertNotNull("imethod not found", imethod); IAnalysisCacheView cache = new AnalysisCacheImpl(options.getSSAOptions()); IR ir = cache.getIRFactory().makeIR(imethod, Everywhere.EVERYWHERE, options.getSSAOptions()); options.getSSAOptions().setPiNodePolicy(save); // v1 should be the parameter "a" at pc 0 String[] names = ir.getLocalNames(0, 1); Assert.assertTrue("failed local name resolution for v1@0", names != null); Assert.assertTrue("incorrect number of local names for v1@0: " + names.length, names.length == 1); Assert.assertTrue("incorrect local name resolution for v1@0: " + names[0], names[0].equals("a")); // v2 is a compiler-induced temporary Assert.assertTrue("didn't expect name for v2 at pc 2", ir.getLocalNames(2, 2) == null); // at pc 5, v1 should represent the locals "a" and "b" names = ir.getLocalNames(5, 1); Assert.assertTrue("failed local name resolution for v1@5", names != null); Assert.assertTrue("incorrect number of local names for v1@5: " + names.length, names.length == 2); Assert.assertTrue("incorrect local name resolution #0 for v1@5: " + names[0], names[0].equals("a")); Assert.assertTrue("incorrect local name resolution #1 for v1@5: " + names[1], names[1].equals("b")); } }