Example #1
0
 // not used anymore.
 private void notTestGetInner() throws Exception {
   ClassPool pool = ClassPool.getDefault();
   CtClass c = pool.get("javassist.CtMethod.ConstParameter");
   CtClass d = pool.get("javassist.CtMethod.ConstParameter");
   CtClass e = pool.get("javassist.CtMethod$ConstParameter");
   assertSame(c, d);
   assertSame(c, e);
   try {
     c = pool.get("test2.Inner.Fake");
     fail("found not-existing class");
   } catch (NotFoundException ex) {
   }
 }
Example #2
0
  public void not_testURLClassPath() throws Exception {
    String host = "www.csg.is.titech.ac.jp";
    String path = "/~chiba/tmp/";
    String url;

    ClassPool cp = new ClassPool(null);
    cp.insertClassPath(new URLClassPath(host, 80, path, "test"));

    url = cp.find("test.TestClassPath").toString();
    System.out.println(url);
    assertEquals("http://" + host + ":80" + path + "test/TestClassPath.class", url);

    assertNull(cp.find("test.No"));
  }
Example #3
0
  public void testInheritance() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String classname = "test2.Inherit";
    CtClass target = pool.get(classname);
    String src =
        "public void sampleMethod() {"
            + "  test2.Inherit i = new test2.Inherit();"
            + "  test2.Inherit2 i2 = i;"
            + "  test2.Inherit3 i3 = i;"
            + "  i3.foo2(); i3.foo2(); i2.foo1(); }";

    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);
    target.writeFile();
  }
Example #4
0
 public void onWrite(ClassPool pool, String className)
     throws NotFoundException, CannotCompileException {
   CtClass cc = pool.get(className);
   try {
     if (isPersistent(className)) {
       CtClass base = cc.getSuperclass();
       CtConstructor cons = new CtConstructor(constructorParams, cc);
       if (base.subclassOf(persistent) || base == object) {
         cons.setBody(null);
         cc.addConstructor(cons);
         if (base == object) {
           cc.setSuperclass(persistent);
         }
       } else {
         if (!isPersistent(base.getName())) {
           throw new NotFoundException(
               "Base class " + base.getName() + " was not declared as persistent");
         }
         cons.setBody("super($0);");
         cc.addConstructor(cons);
       }
       preprocessMethods(cc, true, true);
       if (base == persistent || base == object) {
         CtMethod m = new CtMethod(isRecursive, cc, null);
         m.setBody("return false;");
         cc.addMethod(m);
         addSerializeMethods(cc, false);
       } else if (base.subtypeOf(serializable)) {
         addSerializeMethods(cc, true);
       }
       if ((cc.getModifiers() & Modifier.PRIVATE) == 0) {
         CtClass f = pool.makeClass(className + "LoadFactory");
         f.addInterface(factory);
         CtMethod c = new CtMethod(create, f, null);
         c.setBody("return new " + className + "($1);");
         f.addMethod(c);
         CtNewConstructor.defaultConstructor(f);
       }
     } else {
       preprocessMethods(
           cc, cc.subtypeOf(persistent) && cc != persistent, !className.startsWith("org.nachodb"));
     }
   } catch (Exception x) {
     x.printStackTrace();
   }
 }
Example #5
0
  public void testInner() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String classname = "test2.Inner";
    CtClass target = pool.get(classname);
    String src =
        "public void sampleMethod() throws Exception {"
            + "java.util.Properties props = new java.util.Properties();"
            + "java.rmi.activation.ActivationGroupDesc.CommandEnvironment ace "
            + " = null;"
            + "java.rmi.activation.ActivationGroupDesc agd "
            + " = new java.rmi.activation.ActivationGroupDesc(props,ace);}";
    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);

    String src2 =
        "public java.lang.Character.Subset sampleMethod2() {"
            + "  java.lang.Character.Subset s "
            + "    = Character.UnicodeBlock.HIRAGANA; "
            + "  return s; }";
    CtMethod newmethod2 = CtNewMethod.make(src2, target);
    target.addMethod(newmethod2);

    target.writeFile();
  }
Example #6
0
 public void start(ClassPool pool) throws NotFoundException {
   persistent = pool.get("org.nachodb.Persistent");
   persistentInterface = pool.get("org.nachodb.IPersistent");
   factory = pool.get("org.nachodb.impl.LoadFactory");
   object = pool.get("java.lang.Object");
   isRecursive = persistent.getDeclaredMethod("recursiveLoading");
   constructorParams = new CtClass[] {pool.get("org.nachodb.impl.ClassDescriptor")};
   serializable = pool.get("org.nachodb.impl.FastSerializable");
   pack = serializable.getDeclaredMethod("pack");
   unpack = serializable.getDeclaredMethod("unpack");
   create = factory.getDeclaredMethod("create");
 }
Example #7
0
 public void testToClass() throws Exception {
   ClassPool cp = ClassPool.getDefault();
   CtClass cc = cp.makeClass("test2.ToClassTest");
   Class c = cc.toClass();
   assertEquals(getClass().getClassLoader(), c.getClassLoader());
 }
Example #8
0
  public void testURL() throws Exception {
    String url;

    ClassPool cp = new ClassPool(null);
    cp.appendSystemPath();

    url = cp.find("java.lang.Object").toString();
    System.out.println(url);
    assertTrue(url.startsWith("jar:file:"));
    assertTrue(url.endsWith(".jar!/java/lang/Object.class"));

    assertNull(cp.find("class.not.Exist"));

    cp = new ClassPool(null);
    cp.insertClassPath(".");

    url = cp.find("test2.Inner").toString();
    System.out.println(url);
    assertTrue(url.startsWith("file:/"));
    assertTrue(url.endsWith("/test2/Inner.class"));

    assertNull(cp.find("test2.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(JAR_PATH + "javassist.jar");

    url = cp.find("javassist.CtClass").toString();
    System.out.println(url);
    assertTrue(url.startsWith("jar:file:"));
    assertTrue(url.endsWith("javassist.jar!/javassist/CtClass.class"));

    assertNull(cp.find("javassist.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(new LoaderClassPath(cloader));

    url = cp.find("javassist.CtMethod").toString();
    System.out.println(url);
    // assertTrue(url.startsWith("jar:file:"));
    // assertTrue(url.endsWith("javassist.jar!/javassist/CtMethod.class"));

    assertNull(cp.find("javassist.TestURL"));

    cp = new ClassPool(null);
    cp.insertClassPath(new ByteArrayClassPath("test2.ByteArray", null));

    url = cp.find("test2.ByteArray").toString();
    System.out.println(url);
    assertTrue(url.equals("file:/ByteArrayClassPath/test2/ByteArray.class"));

    assertNull(cp.find("test2.TestURL"));
  }