📄 bytecodereader.jrag
字号:
/* * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered * by the modified BSD License. You should have received a copy of the * modified BSD license with this compiler. * * Copyright (c) 2005-2008, Torbjorn Ekman * All rights reserved. */aspect BytecodeReader { public class BytecodeParser implements BytecodeReader { public CompilationUnit read(InputStream is, String fullName, Program p) throws FileNotFoundException, IOException { return new BytecodeParser(is, fullName).parse(null, null, p); } public static final boolean VERBOSE = false; private DataInputStream is; public CONSTANT_Class_Info classInfo; public CONSTANT_Class_Info outerClassInfo; public String name; public BytecodeParser(byte[] buffer, int size, String name) { //this.is = new DataInputStream(new DummyInputStream(buffer, size)); this.is = new DataInputStream(new ByteArrayInputStream(buffer, 0, size)); this.name = name; } public BytecodeParser(InputStream in, String name) { //this.is = new DataInputStream(new DummyInputStream(buffer, size)); this.is = new DataInputStream(new DummyInputStream(in)); this.name = name; } public BytecodeParser() { this(""); } public BytecodeParser(String name) { if (!name.endsWith(".class")) { //name = name.replaceAll("\\.", "/") + ".class"; name = name.replace('.', '/') + ".class"; } this.name = name; } private static class DummyInputStream extends InputStream { byte[] bytes; int pos; int size; public DummyInputStream(byte[] buffer, int size) { bytes = buffer; this.size = size; } public DummyInputStream(InputStream is) { bytes = new byte[1024]; int index = 0; size = 1024; try { int status; do { status = is.read(bytes, index, size - index); if(status != -1) { index += status; if(index == size) { byte[] newBytes = new byte[size*2]; System.arraycopy(bytes, 0, newBytes, 0, size); bytes = newBytes; size *= 2; } } } while (status != -1); } catch (IOException e) { System.err.println("Something went wrong trying to read " + is); //System.exit(1); } size = index; pos = 0; } public int available() { return size - pos; } public void close() { } public void mark(int readlimit) { } public boolean markSupported() { return false; } public int read(byte[] b) { int actualLength = Math.min(b.length, size-pos); System.arraycopy(bytes, pos, b, 0, actualLength); pos += actualLength; return actualLength; } public int read(byte[] b, int offset, int length) { int actualLength = Math.min(length, size-pos); System.arraycopy(bytes, pos, b, offset, actualLength); pos += actualLength; return actualLength; } public void reset() { } public long skip(long n) { if(size == pos) return -1; long skipSize = Math.min(n, size-pos); pos += skipSize; return skipSize; } public int read() throws IOException { if(pos < size) { int i = bytes[pos++]; if(i < 0) i = 256 + i; return i; } return -1; } } public int next() { try { return is.read(); } catch (IOException e) { System.exit(1); } return -1; } public int u1() { try { return is.readUnsignedByte(); } catch (IOException e) { System.exit(1); } return -1; } public int u2() { try { return is.readUnsignedShort(); } catch (IOException e) { System.exit(1); } return -1; } public int u4() { try { return is.readInt(); } catch (IOException e) { System.exit(1); } return -1; } public int readInt() { try { return is.readInt(); } catch (IOException e) { System.exit(1); } return -1; } public float readFloat() { try { return is.readFloat(); } catch (IOException e) { System.exit(1); } return -1; } public long readLong() { try { return is.readLong(); } catch (IOException e) { System.exit(1); } return -1; } public double readDouble() { try { return is.readDouble(); } catch (IOException e) { System.exit(1); } return -1; } public String readUTF() { try { return is.readUTF(); } catch (IOException e) { System.exit(1); } return ""; } public void skip(int length) { try { is.skip(length); } catch (IOException e) { System.exit(1); } } public void error(String s) { throw new RuntimeException(s); } public void print(String s) { //System.out.print(s); } public void println(String s) { print(s + "\n"); } public void println() { print("\n"); } public CompilationUnit parse(TypeDecl outerTypeDecl, CONSTANT_Class_Info outerClassInfo, Program classPath) throws FileNotFoundException, IOException { //InputStream file = ClassLoader.getSystemResourceAsStream(name); if(is == null) { FileInputStream file = new FileInputStream("/home/torbjorn/sandbox/jdk/" + name); //System.err.println("/home/torbjorn/sandbox/jdk/" + name); if(file == null) { throw new FileNotFoundException(name); } // // Does not work without DummyInputStream. Why? //is = new DataInputStream(new DummyInputStream(new BufferedInputStream(file))); is = new DataInputStream(new BufferedInputStream(file)); } if(BytecodeParser.VERBOSE) println("Parsing byte codes in " + name); this.outerClassInfo = outerClassInfo; parseMagic(); parseMinor(); parseMajor(); parseConstantPool(); CompilationUnit cu = new CompilationUnit(); TypeDecl typeDecl = parseTypeDecl(); cu.setPackageDecl(classInfo.packageDecl()); cu.addTypeDecl(typeDecl); parseFields(typeDecl); parseMethods(typeDecl); new Attributes(this, typeDecl, outerTypeDecl, classPath);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -