2016-08-04 56 views
0

我正在爲我的個人項目工作,並且遇到了一個非常奇怪的錯誤。當我運行方法「註冊」在我的IDE它工作正常。沒有圓度錯誤。但是,當我導出代理並將其與啓動參數javaagent:"agent.jar"一起使用時,它會崩潰,並返回ClassCircularityError (循環超類層次結構)當超類層次結構正常時,ClassCircularityError

public class Refactorer implements ClassFileTransformer { 
    private static final Set<AbstractMatcher<String>> matchers = new HashSet<AbstractMatcher<String>>(); 

    public static void register(AbstractMatcher<String> matcher) { 
     matchers.add(matcher); 
    } 

    public byte[] transform(ClassLoader loader, String name, Class<?> clazz, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {} 
} 

堆棧跟蹤

java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386) 
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401) 
Caused by: java.lang.ClassCircularityError: java/util/Set 
    at me.vader.Refactorer.register(Refactorer.java:42) 
    at me.vader.Setup.registerModders(Setup.java:30) 
    at me.vader.Agent.setAndAddTransformer(Agent.java:37) 
    at me.vader.Agent.premain(Agent.java:18) 

什麼是真正困惑我的是,當我在編代辦託運的超類層次它打印出來罰款。

測試了:

Class c = matchers.getClass(); 
int i = 0; 
while (c != null){ 
    System.out.println("Class (Sup x" + i+ "): " + c.getName()); 
    c = c.getSuperclass(); 
    i++; 
} 

i = 0; 
c = matcher.getClass(); 
while (c != null){ 
    System.out.println("Class (Sup x" + i+ "): " + c.getName()); 
    c = c.getSuperclass(); 
    i++; 
} 

,輸出:

// The "matchers" 
Class (Sup x0): java.util.HashSet 
Class (Sup x1): java.util.AbstractSet 
Class (Sup x2): java.util.AbstractCollection 
Class (Sup x3): java.lang.Object 

// The matcher being added to "matchers" 
Class (Sup x0): me.vader.match.ClassMatcher 
Class (Sup x1): me.vader.match.AbstractMatcher 
Class (Sup x2): java.lang.Object 

回答

0

看來,類沒有被加載在正確的順序。我不知道爲什麼,但似乎是這樣。

我的修補程序拍打在Refactorer類folllowing:

static { 
    Serializable.class.getName(); 
    Cloneable.class.getName(); 
    Iterable .class.getName(); 
    Collection.class.getName(); 
    AbstractCollection.class.getName(); 
    Set.class.getName(); 
    AbstractSet.class.getName(); 
    HashSet.class.getName(); 
} 

迫使他們以正確的順序加載。這是醜陋的,但它的工作原理。