search

Home  >  Q&A  >  body text

ssh - javassist中的CtClass对象的toClass做了什么?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

<code>public class MyClassLoader extends WebappClassLoader{

    public MyClassLoader(){

        System.out.println("无参构造");

    }

     

    public MyClassLoader(ClassLoader parent){

        super(parent);

        System.out.println();

        System.out.println("当前现成的加载器"+Thread.currentThread().getContextClassLoader());

        System.out.println(getParent());

         

    }

    @Override

    public Class<?> loadClass(String name) throws ClassNotFoundException {

     

        Class<?> clazz=null;

        clazz=findLoadedClass(name);

        if(clazz==null){

            if(name.contains("com.yyd")){

                clazz=loadbyme(name);

            }else{

                clazz=super.loadClass(name);

            }

        }

        return clazz;

 

    }

 

    private Class<?> loadbyme(String name) throws ClassNotFoundException{

        ClassPool pool=ClassPool.getDefault();

        CtClass ctclass=null;

        Class<?> clazz=null;

         

        //这个clazz2就是用来实验的,没用

        CtClass ctclass2=null;

        Class<?> clazz2=null;

         

        try {

            pool.insertClassPath("E:\\***\\WEB-INF\\classes");

            ctclass = pool.get(name);

            ctclass2 = pool.get(name);

        //下面这个try-catch块按理说没起什么作用,但是去掉就不报错,加上就报错   

            try {

                clazz2=ctclass2.toClass();

                System.out.println(name+"------------->"+clazz2);

                 

            } catch (CannotCompileException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

 

        } catch (NotFoundException e) {

            System.err.println("********NotFoundException---------》"+name);

            clazz=super.loadClass(name);

        }

         

        clazz=super.loadClass(name);

 

        return clazz;   

    }

}

 

 

 

 

代码中写有注释的那个try-catch块,ctclass2.toClass()的底层是做了什么操作?</code>

高洛峰高洛峰2887 days ago302

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:51:01

    If you add try-catch and report an error, it means that the code does not throws CannotCompileException at all

    reply
    0
  • Cancelreply