KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > impl > RuntimeTools


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.impl;
33
34 import java.io.*;
35 import java.lang.reflect.*;
36 import java.util.MissingResourceException JavaDoc;
37 import net.sf.retrotranslator.runtime.asm.Type;
38
39 /**
40  * @author Taras Puchko
41  */

42 public class RuntimeTools {
43
44     public static final String JavaDoc CONSTRUCTOR_NAME = "<init>";
45     public static final String JavaDoc STATIC_NAME = "<clinit>";
46     public static final String JavaDoc CLASS_EXTENSION = ".class";
47
48     public static Class JavaDoc getBaseClass(char type) {
49         return getBaseClass(Type.getType(new String JavaDoc(new char[]{type})));
50     }
51
52     public static Class JavaDoc getBaseClass(Type type) {
53         switch (type.getSort()) {
54             case Type.VOID:
55                 return void.class;
56             case Type.BOOLEAN:
57                 return boolean.class;
58             case Type.CHAR:
59                 return char.class;
60             case Type.BYTE:
61                 return byte.class;
62             case Type.SHORT:
63                 return short.class;
64             case Type.INT:
65                 return int.class;
66             case Type.FLOAT:
67                 return float.class;
68             case Type.LONG:
69                 return long.class;
70             case Type.DOUBLE:
71                 return double.class;
72         }
73         return null;
74     }
75
76     public static String JavaDoc getConstructorDescriptor(final Constructor c) {
77         Class JavaDoc[] parameters = c.getParameterTypes();
78         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
79         for (Class JavaDoc parameter : parameters) {
80             buf.append(Type.getDescriptor(parameter));
81         }
82         return buf.append(")V").toString();
83     }
84
85     public static Object JavaDoc cloneNonEmptyArray(Object JavaDoc value) {
86         if (!value.getClass().isArray() || Array.getLength(value) == 0) return value;
87         if (value instanceof Object JavaDoc[]) return ((Object JavaDoc[]) value).clone();
88         if (value instanceof boolean[]) return ((boolean[]) value).clone();
89         if (value instanceof byte[]) return ((byte[]) value).clone();
90         if (value instanceof char[]) return ((char[]) value).clone();
91         if (value instanceof double[]) return ((double[]) value).clone();
92         if (value instanceof float[]) return ((float[]) value).clone();
93         if (value instanceof int[]) return ((int[]) value).clone();
94         if (value instanceof long[]) return ((long[]) value).clone();
95         if (value instanceof short[]) return ((short[]) value).clone();
96         throw new IllegalStateException JavaDoc();
97     }
98
99     public static String JavaDoc getString(java.lang.reflect.Type JavaDoc type) {
100         if (!(type instanceof Class JavaDoc)) return type.toString();
101         Class JavaDoc aClass = (Class JavaDoc) type;
102         int dimensionCount = 0;
103         for (; aClass.isArray(); dimensionCount++) {
104             aClass = aClass.getComponentType();
105         }
106         if (dimensionCount == 0) return aClass.getName();
107         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
108         builder.append(aClass.getName());
109         for (; dimensionCount > 0; dimensionCount--) {
110             builder.append("[]");
111         }
112         return builder.toString();
113     }
114
115     public static StringBuilder JavaDoc append(StringBuilder JavaDoc builder, java.lang.reflect.Type JavaDoc[] types) {
116         for (int i = 0; i < types.length; i++) {
117             if (i > 0) builder.append(',');
118             builder.append(getString(types[i]));
119         }
120         return builder;
121     }
122
123     public static byte[] readResourceToByteArray(Class JavaDoc loader, String JavaDoc resourceName) throws MissingResourceException JavaDoc {
124         InputStream inputStream = loader.getResourceAsStream(resourceName);
125         if (inputStream == null) return null;
126         try {
127             try {
128                 byte[] buffer = new byte[0x1000];
129                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
130                 int count;
131                 while((count = inputStream.read(buffer)) > 0) {
132                     outputStream.write(buffer, 0, count);
133                 }
134                 return outputStream.toByteArray();
135             } finally {
136                 inputStream.close();
137             }
138         } catch (IOException e) {
139             throw new RuntimeException JavaDoc(e);
140         }
141     }
142
143     public static byte[] getBytecode(Class JavaDoc target) {
144         if (target.isPrimitive() || target.isArray()) return null;
145         String JavaDoc targetName = target.getName();
146         int index = targetName.lastIndexOf('.');
147         String JavaDoc simpleName = index < 0 ? targetName : targetName.substring(index + 1);
148         return readResourceToByteArray(target, simpleName + CLASS_EXTENSION);
149     }
150
151 }
152
Popular Tags