Beispiel #1
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"));
  }
  public JavassistTemplateBuilder(TemplateRegistry registry, ClassLoader cl) {
    super(registry);
    pool = new ClassPool();
    boolean appended = false;
    loader = cl;
    if (loader == null) {
      loader = pool.getClassLoader();
    }

    try {
      if (loader != null) {
        pool.appendClassPath(new LoaderClassPath(loader));
        appended = true;
      }
    } catch (SecurityException e) {
      LOG.fine("Cannot append a search path of classloader");
      e.printStackTrace();
    }
    if (!appended) {
      pool.appendSystemPath();
    }
  }
Beispiel #3
0
  @Test
  public void testToString() throws Exception {

    ClassPool pool = ClassPool.getDefault();
    System.out.println(pool.toString());

    pool.appendSystemPath();
    System.out.println(pool.toString());

    pool.appendClassPath("/*");
    System.out.println(pool.toString());

    pool.importPackage("java.util");
    System.out.println(pool.toString());

    StringTokenizer st =
        new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
    Set<String> classpath = new HashSet<String>();
    while (st.hasMoreElements()) classpath.add(st.nextToken());
    for (String element : classpath) pool.appendClassPath(element);
    System.out.println(pool.toString());

    Assert.assertTrue(true);
  }
Beispiel #4
0
 /**
  * Creates a root class pool. If <code>useDefaultPath</code> is true, <code>appendSystemPath()
  * </code> is called. Otherwise, this constructor is equivalent to the constructor taking no
  * parameter.
  *
  * @param useDefaultPath true if the system search path is appended.
  */
 public ClassPool(boolean useDefaultPath) {
   this(null);
   if (useDefaultPath) appendSystemPath();
 }