KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > compare > TypedElement


1 /*****************************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the BSD License which
4  * accompanies this distribution, and is available at
5  * http://www.opensource.org/licenses/bsd-license.php Contributor: Andrei Loskutov -
6  * initial API and implementation
7  ****************************************************************************************/

8
9 package de.loskutov.bco.compare;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.BitSet JavaDoc;
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 /**
33  * @author Andrei
34  */

35 public class TypedElement extends BufferedContent
36     implements
37         ITypedElement,
38         IStructureComparator {
39
40     private final String JavaDoc name;
41
42     private String JavaDoc type;
43
44     private final String JavaDoc methodName;
45
46     private final IJavaElement element;
47
48     /** used by Eclipse to recognize appropriated viewer */
49     public static final String JavaDoc TYPE_BYTECODE = "bytecode";
50
51     /** used by Eclipse to recognize appropriated viewer */
52     public static final String JavaDoc TYPE_ASM_IFIER = "java";
53
54     private final BitSet JavaDoc modes;
55
56     /**
57      * Constructor for TypedElement.
58      * @param name
59      * @param type
60      * @param element
61      * @param modes
62      */

63     public TypedElement(String JavaDoc name, String JavaDoc methodName, String JavaDoc type, IJavaElement element, BitSet JavaDoc 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     /**
73      * @see org.eclipse.compare.ITypedElement#getName()
74      */

75     public String JavaDoc getName() {
76         return name;
77     }
78
79
80     /**
81      * @see org.eclipse.compare.ITypedElement#getType()
82      */

83     public String JavaDoc getType() {
84         return type;
85     }
86
87     /**
88      * @param type The type to set.
89      */

90     protected void setType(String JavaDoc type) {
91         this.type = type;
92     }
93
94     /**
95      * @return name
96      */

97     public String JavaDoc getElementName() {
98         return JdtUtils.getElementName(element);
99     }
100
101     public Image getImage() {
102         // default image for .class files
103
return CompareUI.getImage("class");
104     }
105
106     public Object JavaDoc[] getChildren() {
107         return new TypedElement[0];
108     }
109
110     protected InputStream JavaDoc createStream() throws CoreException {
111         InputStream JavaDoc 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 JavaDoc e) {
122             throw new CoreException(new Status(
123                 IStatus.ERROR, "de.loskutov.bco", -1,
124                 "cannot get bytecode dump", e));
125         } catch (UnsupportedClassVersionError JavaDoc 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 JavaDoc e) {
134                 BytecodeOutlinePlugin.log(e, IStatus.WARNING);
135             }
136         }
137         final byte[] bytes = decompiledClass.getText().getBytes();
138         // use internal buffering to prevent multiple calls to this method
139
Display.getDefault().syncExec(new Runnable JavaDoc(){
140             public void run() {
141                 setContent(bytes);
142             }
143         });
144
145         return new ByteArrayInputStream JavaDoc(bytes);
146     }
147
148     /**
149      *
150      * @param mode one of BCOConstants.F_* modes
151      * @param value
152      */

153     public void setMode(int mode, boolean value){
154         modes.set(mode, value);
155         // force create new stream
156
discardBuffer();
157     }
158 }
Popular Tags