1 8 9 package de.loskutov.bco.compare; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.util.BitSet ; 15 16 import org.eclipse.compare.BufferedContent; 17 import org.eclipse.compare.CompareUI; 18 import org.eclipse.compare.ITypedElement; 19 import org.eclipse.compare.structuremergeviewer.IStructureComparator; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.widgets.Display; 26 27 import de.loskutov.bco.BytecodeOutlinePlugin; 28 import de.loskutov.bco.asm.DecompiledClass; 29 import de.loskutov.bco.asm.DecompilerClassVisitor; 30 import de.loskutov.bco.ui.JdtUtils; 31 32 35 public class TypedElement extends BufferedContent 36 implements 37 ITypedElement, 38 IStructureComparator { 39 40 private final String name; 41 42 private String type; 43 44 private final String methodName; 45 46 private final IJavaElement element; 47 48 49 public static final String TYPE_BYTECODE = "bytecode"; 50 51 52 public static final String TYPE_ASM_IFIER = "java"; 53 54 private final BitSet modes; 55 56 63 public TypedElement(String name, String methodName, String type, IJavaElement element, BitSet modes) { 64 super(); 65 this.name = name; 66 this.methodName = methodName; 67 this.type = type; 68 this.element = element; 69 this.modes = modes; 70 } 71 72 75 public String getName() { 76 return name; 77 } 78 79 80 83 public String getType() { 84 return type; 85 } 86 87 90 protected void setType(String type) { 91 this.type = type; 92 } 93 94 97 public String getElementName() { 98 return JdtUtils.getElementName(element); 99 } 100 101 public Image getImage() { 102 return CompareUI.getImage("class"); 104 } 105 106 public Object [] getChildren() { 107 return new TypedElement[0]; 108 } 109 110 protected InputStream createStream() throws CoreException { 111 InputStream stream = JdtUtils.createInputStream(element); 112 if (stream == null) { 113 throw new CoreException(new Status( 114 IStatus.ERROR, "de.loskutov.bco", -1, 115 "cannot get bytecode from class file", null)); 116 } 117 DecompiledClass decompiledClass = null; 118 try { 119 decompiledClass = DecompilerClassVisitor.getDecompiledClass( 120 stream, null, methodName, modes, null); 121 } catch (IOException e) { 122 throw new CoreException(new Status( 123 IStatus.ERROR, "de.loskutov.bco", -1, 124 "cannot get bytecode dump", e)); 125 } catch (UnsupportedClassVersionError e){ 126 throw new CoreException(new Status( 127 IStatus.ERROR, "de.loskutov.bco", -1, 128 "Error caused by attempt to load class compiled with Java version which" 129 + " is not supported by current JVM", e)); 130 } finally { 131 try { 132 stream.close(); 133 } catch (IOException e) { 134 BytecodeOutlinePlugin.log(e, IStatus.WARNING); 135 } 136 } 137 final byte[] bytes = decompiledClass.getText().getBytes(); 138 Display.getDefault().syncExec(new Runnable (){ 140 public void run() { 141 setContent(bytes); 142 } 143 }); 144 145 return new ByteArrayInputStream (bytes); 146 } 147 148 153 public void setMode(int mode, boolean value){ 154 modes.set(mode, value); 155 discardBuffer(); 157 } 158 } | Popular Tags |