| 3 | import java.io.InputStream; |
| 4 | |
| 5 | public class ClassFiles { |
| 6 | public static String classAsFile(final Class<?> clazz) { |
| 7 | return classAsFile(clazz, true); |
| 8 | } |
| 9 | |
| 10 | public static String classAsFile(final Class<?> clazz, boolean suffix) { |
| 11 | String str; |
| 12 | if (clazz.getEnclosingClass() == null) { |
| 13 | str = clazz.getName().replace(".", "/"); |
| 14 | } else { |
| 15 | str = classAsFile(clazz.getEnclosingClass(), false) + "$" + clazz.getSimpleName(); |
| 16 | } |
| 17 | if (suffix) { |
| 18 | str += ".class"; |
| 19 | } |
| 20 | return str; |
| 21 | } |
| 22 | |
| 23 | public static byte[] classAsBytes(final Class<?> clazz) { |
| 24 | try { |
| 25 | final byte[] buffer = new byte[1024]; |
| 26 | final String file = classAsFile(clazz); |
| 27 | final InputStream in = ClassFiles.class.getClassLoader().getResourceAsStream(file); |
| 28 | if (in == null) { |
| 29 | throw new IOException("couldn't find '" + file + "'"); |
| 30 | } |
| 31 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 32 | int len; |
| 33 | while ((len = in.read(buffer)) != -1) { |
| 34 | out.write(buffer, 0, len); |
| 35 | } |
| 36 | return out.toByteArray(); |
| 37 | } catch (IOException e) { |
| 38 | throw new RuntimeException(e); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | } |
nothing calls this directly
no outgoing calls
no test coverage detected