Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lx01.projects.jd;
- public class GUI {
- public static void runGUI () {
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch (ClassNotFoundException cnfe) {
- System.err.println("Could not find the Nimbus platform look and feel");
- } catch (InstantiationException ie) {
- System.err.println("Could not instantiate the Nimbus platform look and feel");
- } catch (IllegalAccessException iae) {
- System.err.println("Could not access the Nimbus platform look and feel");
- } catch (UnsupportedLookAndFeelException ulafe) {
- System.err.println("Nimbus platform look and feel isn't supported");
- }
- JFileChooser chooser = new JFileChooser();
- chooser.setDialogTitle("LJDGX Java Decompiler - Class selection dialog");
- chooser.setFileFilter(new FileNameExtensionFilter("Java JARs and classes", "jar", "class"));
- chooser.setMultiSelectionEnabled(false);
- chooser.addActionListener((e) -> {
- if (chooser.getSelectedFile() != null) {
- DecompiledClass c = JavaDecompiler.decompile(new FileInputStream(chooser.getSelectedFile()));
- openViewport(c, Files.readAllBytes(new File(chooser.getSelectedFile()).toPath()));
- }
- });
- chooser.showOpenDialog();
- }
- public void openViewport (DecompiledClass c, byte[] data) {
- JFrame gui = new JFrame("LJDGX Java Decompiler");
- gui.setLayout(new GridLayout(1, 2, 5, 5));
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch (ClassNotFoundException cnfe) {
- System.err.println("Could not find the Nimbus platform look and feel");
- } catch (InstantiationException ie) {
- System.err.println("Could not instantiate the Nimbus platform look and feel");
- } catch (IllegalAccessException iae) {
- System.err.println("Could not access the Nimbus platform look and feel");
- } catch (UnsupportedLookAndFeelException ulafe) {
- System.err.println("Nimbus platform look and feel isn't supported");
- }
- boolean[] modeSwitch = new boolean[2];
- JTextArea area = new JTextArea(c.toFormattedString(true), 47, 24);
- area.setEditable(false);
- JPanel buttons = new JPanel(new GridLayout(2, 1, 5, 5));
- JButton textView = new JButton("Enter text view");
- textView.addActionListener((e) -> {
- if (!modeSwitch[0]) {
- area.setText(c.toFormattedString(true));
- modeSwitch[0] = true;
- modeSwitch[1] = false;
- }
- });
- buttons.add(textView);
- JButton hexView = new JButton("Enter hex view");
- textView.addActionListener((e) -> {
- if (!modeSwitch[1]) {
- area.setText(formatHex(data));
- modeSwitch[0] = false;
- modeSwitch[1] = true;
- }
- });
- buttons.add(hexView);
- gui.add(buttons);
- gui.add(area);
- }
- public static String formatHex (byte[] data) {
- String formattedHex = "<html>";
- int constantPoolEnd = getConstantPoolByteLength((data[8] << 8) | data[9], data) + 10;
- int fieldsEnd = getFieldsByteLength(data, constantPoolEnd + 8 + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2)) + constantPoolEnd + 4 + ((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2;
- int methodsEnd = getMethodsByteLength((data, fieldsEnd + 8 + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2))) + fieldsEnd + 4 + ((data[fieldsEnd] << 8) | data[fieldsEnd + 1]) * 2;
- for (int i = 0; i <= data.length; i++) {
- if (i == methodsEnd + 2) formattedHex = formattedHex.concat("<font color=blue>");
- if (i == methodsEnd) formattedHex = formattedHex.concat("<font color=darkblue>");
- if (i == fieldsEnd + 2) formattedHex = formattedHex.concat("<font color=lime>");
- if (i == constantPoolEnd + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2) + 8) formattedHex = formattedHex.concat("<font color=green>");
- if (i == constantPoolEnd + 8) formattedHex = formattedHex.concat("<font color=magenta>");
- if (i == constantPoolEnd + 2) formattedHex = formattedHex.concat("<font color=purple>");
- if (i == constantPoolEnd) formattedHex = formattedHex.concat("<font color=cyan>");
- if (i == 10) formattedHex = formattedHex.concat("<font color=red>");
- if (i == 8) formattedHex = formattedHex.concat("<font color=maroon>");
- if (i == 0) formattedHex = formattedHex.concat("<font color=silver>");
- String snipped = Integer.toHexString(data[i]).substring(6);
- formattedHex = formattedHex.concat(snipped + " ");
- if (i == fieldsEnd - 1) + 9) formattedHex = formattedHex.concat("</font>");
- if (i == constantPoolEnd + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2) + 9) formattedHex = formattedHex.concat("</font>");
- if (i == constantPoolEnd + 7) formattedHex = formattedHex.concat("</font>");
- if (i == constantPoolEnd + 1) formattedHex = formattedHex.concat("</font>");
- if (i == constantPoolEnd - 1) formattedHex = formattedHex.concat("</font>");
- if (i == 9) formattedHex = formattedHex.concat("</font>");
- if (i == 7) formattedHex = formattedHex.concat("</font>");
- }
- return formattedHex.substring(0, formattedHex.length() - 2).concat("</font></html>");
- }
- public static int getConstantPoolByteLength (int length, byte[] data) {
- byte[] constantPoolAndOn = new byte[data.length - 10];
- System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - 10);
- int totalLength = 0;
- for (int i = 0; i <= length; i++) {
- int entryLength;
- switch (constantPoolAndOn[0]) {
- case 1:
- entryLength = (constantPoolAndOn[1] << 8) | constantPoolAndOn[2];
- break;
- case 3:
- entryLength = 4;
- break;
- case 4:
- entryLength = 4;
- break;
- case 5:
- entryLength = 8;
- break;
- case 6:
- entryLength = 8;
- break;
- case 7:
- entryLength = 2;
- break;
- case 8:
- entryLength = 2;
- break;
- case 9:
- entryLength = 4;
- break;
- case 10:
- entryLength = 4;
- break;
- case 11:
- entryLength = 4;
- break;
- case 12:
- entryLength = 4;
- break;
- case 15:
- entryLength = 3;
- break;
- case 16:
- entryLength = 2;
- break;
- case 18:
- entryLength = 4;
- break;
- default:
- entryLength = 0;
- }
- totalLength += 1 + entryLength;
- byte[] temp = new byte[constantPoolAndOn.length];
- System.arraycopy(constantPoolAndOn, 1 + entryLength, temp, 0, constantPoolAndOn.length);
- System.arraycopy(temp, 0, constantPoolAndOn, 0, constantPoolAndOn.length);
- }
- return totalLength;
- }
- public static int getFieldsByteLength (byte[] data, int start) {
- int length = (data[start] << 8) | data[start + 1];
- byte[] fieldsAndOn = new byte[data.length - start + 2];
- System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - start + 2);
- int totalLength = 0;
- for (int i = 0; i <= length; i++) {
- int entryLength = 0;
- for (int i = 0; i <= (fieldsAndOn[6] << 8) | fieldsAndOn[7]; i++)
- entryLength += getAttributeByteLength(fieldsAndOn, 8);
- totalLength += 8 + entryLength;
- byte[] temp = new byte[fieldsAndOn.length];
- System.arraycopy(fieldsAndOn, 8 + entryLength, temp, 0, fieldsAndOn.length);
- System.arraycopy(temp, 0, fieldsAndOn, 0, fieldsAndOn.length);
- }
- return totalLength;
- }
- public static int getMethodsByteLength (byte[] data, int start) {
- int length = (data[start] << 8) | data[start + 1];
- byte[] methodsAndOn = new byte[data.length - start + 2];
- System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - start + 2);
- int totalLength = 0;
- for (int i = 0; i <= length; i++) {
- int entryLength = 0;
- for (int i = 0; i <= (methodsAndOn[6] << 8) | methodsAndOn[7]; i++)
- entryLength += getAttributeByteLength(methodsAndOn, 8);
- totalLength += 8 + entryLength;
- byte[] temp = new byte[methodsAndOn.length];
- System.arraycopy(methodsAndOn, 8 + entryLength, temp, 0, methodsAndOn.length);
- System.arraycopy(temp, 0, methodsAndOn, 0, methodsAndOn.length);
- }
- return totalLength;
- }
- public static int getAttributeByteLength(byte[] data, int start) {
- return 6 + ((data[start + 2] << 24) | (data[start + 3] << 16)) | ((data[start + 4] << 8) | data[start + 5]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement