1 Reply Latest reply on Aug 6, 2006 11:16 AM by Shigeru Chiba

    RuntimeException: cannot set to static

    Martin Burger Newbie

      Hello!

      I wrote a class that removes final modifier from given classes, here's the most relevant method:

      public void instrument(CtClass clazz) {
      
       int modifiers = clazz.getModifiers();
      
       if (Modifier.isFinal(modifiers)) {
       logger.info("Will remove final modifier from class: " + clazz.getName());
       logger.debug("Old modifiers: " + Modifier.toString(modifiers));
      
       int newModifiers = Modifier.clear(modifiers, Modifier.FINAL);
       logger.debug("New modifiers: " + Modifier.toString(newModifiers));
      
       clazz.setModifiers(newModifiers);
      
       } else {
       logger.debug("Class is not final, nothing do change: " + clazz.getName());
       }
       }


      Running my util on the JDK classes results in an exception:

      Exception in thread "main" java.lang.RuntimeException: cannot set to static
       at javassist.CtClassType.setModifiers(CtClassType.java:374)


      The last lines of my log are:

      307 [ main] TRACE mon.javassist.FileInstrumenter - Current jar entry: com/sun/corba/se/impl/corba/AnyImpl$AnyInputStream.class
      307 [ main] INFO y.framework.utils.FinalRemover - Will remove final modifier from class: com.sun.corba.se.impl.corba.AnyImpl$AnyInputStream
      308 [ main] DEBUG y.framework.utils.FinalRemover - Old modifiers: static final
      308 [ main] DEBUG y.framework.utils.FinalRemover - New modifiers: static


      The class CtClassType contains the following lines:

      public void setModifiers(int mod) {
       if (Modifier.isStatic(mod))
       throw new RuntimeException("cannot set to static");
      
       checkModify();
       int acc = AccessFlag.of(mod) | AccessFlag.SUPER;
       getClassFile2().setAccessFlags(acc);
       }
      


      So, why is it not possible to change the modifier from "static final" to "static"? AFAIK, static inner classes are allowed, aren't they?

      Regards, Martin