<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 25,comments - 0,trackbacks - 0

    Class search path

    The default ClassPool returned by a static method ClassPool.getDefault() searches the same path that the underlying JVM (Java virtual machine) has. If a program is running on a web application server such as JBoss and Tomcat, the ClassPool object may not be able to find user classes since such a web application server uses multiple class loaders as well as the system class loader. In that case, an additional class path must be registered to the ClassPool. Suppose that pool refers to aClassPool object:

      pool.insertClassPath(new ClassClassPath(this.getClass())); 

    This statement registers the class path that was used for loading the class of the object that this refers to. You can use any Class object as an argument instead ofthis.getClass(). The class path used for loading the class represented by that Class object is registered.

    You can register a directory name as the class search path. For example, the following code adds a directory /usr/local/javalib to the search path:

      ClassPool pool = ClassPool.getDefault(); pool.insertClassPath("/usr/local/javalib"); 

    The search path that the users can add is not only a directory but also a URL:

      ClassPool pool = ClassPool.getDefault(); ClassPath cp = new URLClassPath("www.javassist.org", 80, "/java/", "org.javassist."); pool.insertClassPath(cp); 

    This program adds "http://www.javassist.org:80/java/" to the class search path. This URL is used only for searching classes belonging to a package org.javassist. For example, to load a class org.javassist.test.Main, its class file will be obtained from:

      http://www.javassist.org:80/java/org/javassist/test/Main.class 

    Furthermore, you can directly give a byte array to a ClassPool object and construct a CtClass object from that array. To do this, use ByteArrayClassPath. For example,

      ClassPool cp = ClassPool.getDefault(); byte[] b = a byte array; String name = class name; cp.insertClassPath(new ByteArrayClassPath(name, b)); CtClass cc = cp.get(name); 

    The obtained CtClass object represents a class defined by the class file specified by b. The ClassPool reads a class file from the given ByteArrayClassPath if get() is called and the class name given to get() is equal to one specified by name.

    If you do not know the fully-qualified name of the class, then you can use makeClass() in ClassPool:

      ClassPool cp = ClassPool.getDefault(); InputStream ins = an input stream for reading a class file; CtClass cc = cp.makeClass(ins); 

    makeClass() returns the CtClass object constructed from the given input stream. You can use makeClass() for eagerly feeding class files to the ClassPool object. This might improve performance if the search path includes a large jar file. Since a ClassPool object reads a class file on demand, it might repeatedly search the whole jar file for every class file. makeClass() can be used for optimizing this search. The CtClass constructed by makeClass() is kept in the ClassPool object and the class file is never read again.

    The users can extend the class search path. They can define a new class implementing ClassPath interface and give an instance of that class to insertClassPath() inClassPool. This allows a non-standard resource to be included in the search path.





    package com.cloud.dm.util;

    import java.io.File;
    import java.lang.reflect.Field;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import javassist.ClassClassPath;
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.CtNewMethod;
    import javassist.bytecode.DuplicateMemberException;

    import org.apache.commons.lang3.StringUtils;

    public class Struts2GetterSetterGen {
        private static ClassPool pool = ClassPool.getDefault();

        public static void init() throws Exception {
            URL url = Struts2GetterSetterGen.class.getResource("/");
            List<File> resultList = new ArrayList<File>();
            FileSearcher.findFiles(url.getFile(), "*Action.class", resultList);
            for (File object : resultList) {
                String className = StringUtils.substringBetween(object.toString(),
                        "classes\\", ".class").replaceAll("\\\\", ".");
                CtClass ct = null;
                pool.insertClassPath(new ClassClassPath(Class.forName(className))); //在servlet容器中啟動
                ct = pool.get(className);
                Field[] fs = Class.forName(className).getDeclaredFields();
                for (Field f : fs) {
                    genGetter(ct, f);
                    genSetter(ct, f);
                }
                ct.writeFile(url.getPath()); // 覆蓋之前的class文件
            }
        }

        private static void genGetter(CtClass ct, Field field) throws Exception {
            String string = "public " + field.getType().getName() + " get"
                    + StringUtils.capitalize(field.getName()) + "() {return "
                    + field.getName() + "; }";
            CtMethod m = CtNewMethod.make(string, ct);
            try {
                ct.addMethod(m);
            } catch (DuplicateMemberException e) {
            }
        }

        private static void genSetter(CtClass ct, Field field) throws Exception {
            String string = "public void set"
                    + StringUtils.capitalize(field.getName()) + "("
                    + field.getType().getName() + " " + field.getName() + "){this."
                    + field.getName() + " = " + field.getName() + "; }";
            CtMethod m = CtNewMethod.make(string, ct);
            try {
                ct.addMethod(m);
            } catch (DuplicateMemberException e) {
            }
        }
    }


    posted on 2012-03-22 15:22 周磊 閱讀(4139) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲人av高清无码| 亚洲看片无码在线视频| 一级全免费视频播放| 国产成人精品高清免费| 亚洲youwu永久无码精品 | 亚洲精品国产高清在线观看| 最近免费中文字幕mv电影| 久久久久亚洲AV无码永不| 最近2019年免费中文字幕高清| 亚洲视频在线观看一区| 午夜不卡久久精品无码免费| 亚洲AV无码一区二区三区系列| 久久国产精品成人免费| 亚洲欧洲校园自拍都市| 波多野结衣在线免费观看| 一本色道久久88亚洲精品综合 | 成年女人男人免费视频播放| 亚洲色成人四虎在线观看| 国产免费人人看大香伊| 春意影院午夜爽爽爽免费| a级亚洲片精品久久久久久久| 鲁丝片一区二区三区免费| 亚洲欧洲日产v特级毛片| 成人a视频片在线观看免费| 国产成人综合亚洲一区| 伊人久久亚洲综合| 亚洲一区二区在线免费观看| 亚洲午夜电影在线观看高清| 成人免费无毒在线观看网站| 污污的视频在线免费观看| 亚洲av无码一区二区三区网站| 永久免费在线观看视频| 亚洲乱码日产精品一二三| 国产亚洲欧洲Aⅴ综合一区| 99久久免费精品视频| 亚洲国产AV无码一区二区三区| 精品国产亚洲一区二区在线观看 | 国外亚洲成AV人片在线观看| 99re6热视频精品免费观看| 亚洲综合一区国产精品| 久久久久亚洲精品天堂久久久久久|