开发者

CtClass.toClass throws an exception with message 'null'

Steps i perform to create dynamic class is as follows:

      try
      {

            classLoader = Class.forName("org.yaddayadda.DynamicClass" + genericType.getName().toLowerCase() + uniqueID);
        }
        catch (ClassNotFoundException classNotFoundException)
        {
        }

        if (classLoader == null)
        {
            dynamicClass = pool.get("org.yaddayadda.DynamicClass");
            dynamicClass.defrost();
            dynamicClass.replaceClassName("org.yaddayadda.DynamicClass","org.yaddayadda.DynamicClass"+ genericType.getName().toLowerCase() + uniqueID);

            dynamicClass.defrost();


            CtMethod readObject = dynamicClass.getDeclaredMethod("Method1");
            CtMethod writeObject = dynamicClass.getDeclaredMethod("Method2");

            StringBuffer method1= new StringBuffer();
            StringBuffer method2= new StringBuffer();

            GenerateDynamicCode.generateCode(genericType, method1, method2);

            writeObject.insertAt(1, method1.toString());
            readObject.insertAt(1, method2.toString());

            //This is where the exception is thrown
            classLoader = dynamicClass.toClass();
        }

I first try to check if the class already exists in ClassPool if not then i proceed on to creating a class. Defrost is done for just a fail-safe mechanism. The Exception comes when the method toClass is called dynamicClass.toClass();

The Exception is as follows

    Exception Message: null
    Stack Trace: 
    sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    javassist.ClassPool.toClass2(ClassPool.java:1102)
    javassist.ClassPool.toClass(ClassPool.java:1083)
    javassist.ClassPool.toCl开发者_C百科ass(ClassPool.java:1032)
    javassist.ClassPool.toClass(ClassPool.java:990)
    javassist.CtClass.toClass(CtClass.java:1125)
    This Class

What is the reason of the above exception and what can be done to avoid it ?


Well i have found a solution, in fact a workaround i.e. i simply call in a loop again defrosting the compiled class and redoing everything i just tried when an exception comes and the loop runs at most 3 times just to be on the safe side

        int i = 0;
        while (classLoader == null)
        {
            try
            {
                if (classLoader == null)
                {
                    dynamicClass = pool.get("org.yaddayadda.DynamicClass");
                    dynamicClass.defrost();


                    try
                    {
                        dynamicSurrugate = pool.get("org.yaddayadda.DynamicClass" + genericType.getName().toLowerCase() + uniqueID);

                        dynamicSurrugate.defrost();
                    }
                    catch (NotFoundException notFoundException)
                    {
                    }


                    //Everything the same
                }
            }
            catch (Exception exception)
            {
                classLoader = null;
                i++;
            }

            if (i == 3)
            {
                throw new Exception("IF failed throw message third time");
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜