int main(int argc, const char* argv[]) { JvmEnv jenv; jenv.addClassPath("."); jenv.addClassPath("./tests"); jenv.addClassPath("/home/trapni/projects/jvmtoy/classpath-rt"); std::string className = argc == 2 ? argv[1] : "Test"; Class* c = jenv.getClass(className); if (!c) { fprintf(stderr, "Could not find class '%s'.\n", className.c_str()); return 1; } c->dump(); Method* method = c->findMethod("main"); int errors = 0; if (!method) { fprintf(stderr, "Could not find method '%s' in %s.\n", "main", c->name().c_str()); ++errors; } else { if (!(method->flags() & MethodFlags::Public)) { printf("main: must be public\n"); ++errors; } if (!(method->flags() & MethodFlags::Static)) { printf("main: must be static\n"); ++errors; } if (method->signature() != "([Ljava/lang/String;)V") { printf("main: invalid signature\n"); ++errors; } } if (!errors) { printf("entry method found\n"); } return 0; }
Object JVM::newS(const jbyte* bys, int len) { Object ary = this->newAry("B", len); if (len) { ary.sary((void*) bys, 0, len); } Class cls = this->findClass("Ljava/lang/String;"); Method m = cls.findMethod("<init>", "[B", "V", false); jval val = ary.val; return m.newA(&val, 1); }
void ClassTestSuite::testMethodSearch() { Class test = Class::lookup("ClassTest::Test1"); auto retDouble = test.findAllMethods([&](const Method& m) { return m.returnSpelling() == "double"; }); TS_ASSERT_EQUALS(retDouble.size(), 3); auto retDoubleStrict = test.findAllMethods([&](const Method& m) { return m.returnSpelling() == "double" && m.getClass() == test; //filter out inherited methods }); TS_ASSERT_EQUALS(retDoubleStrict.size(), 2); auto constMethods = test.findAllMethods([&](const Method& m) { return m.isConst(); }); TS_ASSERT_EQUALS(constMethods.size(), 2); Method m = test.findMethod([&](const Method& m) { return m.name() == "method1"; }); TS_ASSERT(m.isValid()); TS_ASSERT(m.isConst()); TS_ASSERT_EQUALS(m.returnSpelling(), "std::string"); Method m30 = test.findMethod([&](const Method& m) { return m.name() == "method30"; }); TS_ASSERT(!m30.isValid()); TS_ASSERT_THROWS_ANYTHING(m30.isConst()); }