Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit: This works only on concrete classes (the class to inspect has its own .class file). I changed the code below to reflect this. Until diving deeper into the ClassFileAnalyzer library to work with classes directly instead of reading them from a temporary file this limitation exists.</strong></p> <p>Following approach works for me:</p> <p>Download and import following libarary <a href="http://classfileanalyzer.javaseiten.de/" rel="nofollow noreferrer">ClassFileAnalyzer</a></p> <p>Add the following two static methods (Attention! getClussDump() needs a little modification for writing out the class file to a temporary file: I removed my code here because it's very special at this point):</p> <pre><code>public static String getClassDump(Class&lt;?&gt; c) throws Exception { String classFileName = c.getSimpleName() + ".class"; URL resource = c.getResource(classFileName); if (resource == null) { throw new RuntimeException("Works only for concreate classes!"); } String absolutePath = ...; // write to temp file and get absolute path ClassFile classFile = new ClassFile(absolutePath); classFile.parse(); Info infos = new Info(classFile, absolutePath); StringBuffer infoBuffer = infos.getInfos(); return infoBuffer.toString(); } public static &lt;S extends List&lt;Method&gt;&gt; S sortMethodsBySourceOrder(Class&lt;?&gt; c, S methods) throws Exception { String classDump = getClassDump(c); int index = classDump.indexOf("constant_pool_count:"); final String dump = classDump.substring(index); Collections.sort(methods, new Comparator&lt;Method&gt;() { public int compare(Method o1, Method o2) { Integer i1 = Integer.valueOf(dump.indexOf(" " + o1.getName() + lineSeparator)); Integer i2 = Integer.valueOf(dump.indexOf(" " + o2.getName() + lineSeparator)); return i1.compareTo(i2); }}); return methods; } </code></pre> <p>Now you can call the sortMethodsBySourceOrder with any List of methods (because sorting arrays is not very comfortable) and you will get the list back sorted. </p> <p>It works by looking at the class dumps constant pool which in turn can be determined by the library.</p> <p>Greetz, GHad</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload