[Dashg-commit] SF.net SVN: dashg:[31] trunk/dashg/src/com/mebigfatguy/dashg
Status: Pre-Alpha
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2008-10-28 19:25:20
|
Revision: 31
http://dashg.svn.sourceforge.net/dashg/?rev=31&view=rev
Author: dbrosius
Date: 2008-10-28 19:25:16 +0000 (Tue, 28 Oct 2008)
Log Message:
-----------
start using ASM to build src files
Modified Paths:
--------------
trunk/dashg/src/com/mebigfatguy/dashg/DashGClassSourcePrintingVisitor.java
trunk/dashg/src/com/mebigfatguy/dashg/DashGLineNumberer.java
trunk/dashg/src/com/mebigfatguy/dashg/DashGMethodSourcePrintingVisitor.java
trunk/dashg/src/com/mebigfatguy/dashg/DashGTask.java
Removed Paths:
-------------
trunk/dashg/src/com/mebigfatguy/dashg/DashGSourceWriter.java
Modified: trunk/dashg/src/com/mebigfatguy/dashg/DashGClassSourcePrintingVisitor.java
===================================================================
--- trunk/dashg/src/com/mebigfatguy/dashg/DashGClassSourcePrintingVisitor.java 2008-10-28 19:24:51 UTC (rev 30)
+++ trunk/dashg/src/com/mebigfatguy/dashg/DashGClassSourcePrintingVisitor.java 2008-10-28 19:25:16 UTC (rev 31)
@@ -19,6 +19,7 @@
package com.mebigfatguy.dashg;
import java.io.PrintWriter;
+import java.util.List;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
@@ -26,7 +27,6 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
public class DashGClassSourcePrintingVisitor implements ClassVisitor, DashGLineNumberer {
@@ -41,15 +41,22 @@
pw = srcWriter;
}
+ public void inc() {
+ ++lineNumber;
+ }
+
+ public void inc(int count) {
+ lineNumber += count;
+ }
public int getLineNumber() {
return lineNumber;
}
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
- int dotPos = name.lastIndexOf('.');
- if (dotPos >= 0) {
- packageName = name.substring(0, dotPos);
- clsName = name.substring(dotPos+1);
+ int slashPos = name.lastIndexOf('/');
+ if (slashPos >= 0) {
+ packageName = name.substring(0, slashPos).replace('/', '.');
+ clsName = name.substring(slashPos+1);
pw.println("package " + packageName + ";");
pw.println();
@@ -60,28 +67,12 @@
clsName = name;
}
- if ((access & Opcodes.ACC_PUBLIC) != 0) {
- pw.print("public ");
- } else if ((access & Opcodes.ACC_PRIVATE) != 0) {
- pw.print("private ");
- } else if ((access & Opcodes.ACC_PROTECTED) != 0) {
- pw.print("protected ");
- }
+ pw.print(DashGUtils.getAttribute(access));
- if ((access & Opcodes.ACC_STATIC) != 0) {
- pw.println("static ");
- } else if ((access & Opcodes.ACC_ABSTRACT) != 0) {
- pw.print("abstract ");
- }
-
- if ((access & Opcodes.ACC_FINAL) != 0) {
- pw.print("final ");
- }
-
pw.print(clsName + ' ');
- if (superName != null)
- pw.print("extends " + superName + ' ');
+ if (!superName.equals("java/lang/Object"))
+ pw.print("extends " + superName.replace('/', '.') + ' ');
String comma = "implements ";
for (String inf : interfaces) {
@@ -90,9 +81,9 @@
pw.print(inf);
}
- pw.println();
+ pw.println(" {");
- lineNumber+=1;
+ ++lineNumber;
cw.visit(version, access, name, signature, superName, interfaces);
}
@@ -105,9 +96,19 @@
}
public void visitEnd() {
+ pw.println("}");
+ ++lineNumber;
}
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
+ pw.print('\t' + DashGUtils.getAttribute(access));
+ String type = DashGUtils.convertType(desc);
+ pw.print(type + ' ' + name);
+ if (value != null)
+ pw.print(" = " + value);
+ pw.println(";");
+ ++lineNumber;
+
return null;
}
@@ -115,7 +116,30 @@
}
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
- return new DashGMethodSourcePrintingVisitor(cw, pw);
+ pw.println();
+ pw.print('\t' + DashGUtils.getAttribute(access));
+ pw.print(DashGUtils.getReturn(desc));
+ pw.print(' ');
+ if (name.equals("<init>"))
+ name = clsName;
+ pw.print(name);
+ List<String> args = DashGUtils.getArguments(desc);
+ String comma = "(";
+ for (String a : args) {
+ pw.print(comma);
+ comma = ", ";
+ pw.print(a);
+ pw.print(' ');
+ pw.print('x');
+ }
+ pw.print(") ");
+
+ pw.println("{");
+ pw.println("\t\tint PC;");
+ pw.println();
+ lineNumber += 4;
+
+ return new DashGMethodSourcePrintingVisitor(cw.visitMethod(access, name, desc, signature, exceptions), pw, this);
}
public void visitOuterClass(String owner, String name, String desc) {
@@ -123,5 +147,4 @@
public void visitSource(String source, String debug) {
}
-
}
Modified: trunk/dashg/src/com/mebigfatguy/dashg/DashGLineNumberer.java
===================================================================
--- trunk/dashg/src/com/mebigfatguy/dashg/DashGLineNumberer.java 2008-10-28 19:24:51 UTC (rev 30)
+++ trunk/dashg/src/com/mebigfatguy/dashg/DashGLineNumberer.java 2008-10-28 19:25:16 UTC (rev 31)
@@ -19,5 +19,7 @@
package com.mebigfatguy.dashg;
public interface DashGLineNumberer {
+ void inc();
+ void inc(int count);
int getLineNumber();
}
Modified: trunk/dashg/src/com/mebigfatguy/dashg/DashGMethodSourcePrintingVisitor.java
===================================================================
--- trunk/dashg/src/com/mebigfatguy/dashg/DashGMethodSourcePrintingVisitor.java 2008-10-28 19:24:51 UTC (rev 30)
+++ trunk/dashg/src/com/mebigfatguy/dashg/DashGMethodSourcePrintingVisitor.java 2008-10-28 19:25:16 UTC (rev 31)
@@ -19,101 +19,158 @@
package com.mebigfatguy.dashg;
import java.io.PrintWriter;
+import java.text.NumberFormat;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.util.TraceClassVisitor;
public class DashGMethodSourcePrintingVisitor implements MethodVisitor {
- private ClassWriter cw;
+ private MethodVisitor mv;
private PrintWriter pw;
+ private DashGLineNumberer ln;
+ private NumberFormat nf;
- public DashGMethodSourcePrintingVisitor(ClassWriter cWriter, PrintWriter pWriter) {
- cw = cWriter;
+ public DashGMethodSourcePrintingVisitor(MethodVisitor mVisitor, PrintWriter pWriter, DashGLineNumberer lineNumberer) {
+ mv = mVisitor;
pw = pWriter;
+ ln = lineNumberer;
+ nf = NumberFormat.getIntegerInstance();
+ nf.setMinimumIntegerDigits(5);
+ nf.setGroupingUsed(false);
}
- public AnnotationVisitor visitAnnotation(String arg0, boolean arg1) {
- return null;
+ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+ return mv.visitAnnotation(desc, visible);
}
public AnnotationVisitor visitAnnotationDefault() {
- return null;
+ return mv.visitAnnotationDefault();
}
- public void visitAttribute(Attribute arg0) {
+ public void visitAttribute(Attribute attr) {
+ mv.visitAttribute(attr);
}
public void visitCode() {
+ mv.visitCode();
}
public void visitEnd() {
+ pw.println("\t}");
+ ln.inc(1);
+ mv.visitEnd();
}
- public void visitFieldInsn(int arg0, String arg1, String arg2, String arg3) {
-
+ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ insn += ' ' + name;
+ emitInsn(insn);
+ mv.visitFieldInsn(opcode, owner, name, desc);
}
- public void visitFrame(int arg0, int arg1, Object[] arg2, int arg3,
- Object[] arg4) {
+ public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
+ mv.visitFrame(type, nLocal, local, nStack, stack);
}
- public void visitIincInsn(int arg0, int arg1) {
+ public void visitIincInsn(int var, int increment) {
+ String insn = "IINC";
+ insn += " a" + var + ", " + increment;
+ emitInsn(insn);
+ mv.visitIincInsn(var, increment);
}
- public void visitInsn(int arg0) {
+ public void visitInsn(int opcode) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ emitInsn(insn);
+ mv.visitInsn(opcode);
}
- public void visitIntInsn(int arg0, int arg1) {
+ public void visitIntInsn(int opcode, int operand) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ insn += " " + operand;
+ emitInsn(insn);
+ mv.visitIntInsn(opcode, operand);
}
- public void visitJumpInsn(int arg0, Label arg1) {
+ public void visitJumpInsn(int opcode, Label label) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ emitInsn(insn);
+ mv.visitJumpInsn(opcode, label);
}
- public void visitLabel(Label arg0) {
+ public void visitLabel(Label label) {
+ mv.visitLabel(label);
}
- public void visitLdcInsn(Object arg0) {
+ public void visitLdcInsn(Object cst) {
+ String insn = "LDC";
+ insn += ' ' + cst.toString();
+ emitInsn(insn);
+ mv.visitLdcInsn(cst);
}
- public void visitLineNumber(int arg0, Label arg1) {
+ public void visitLineNumber(int line, Label start) {
}
- public void visitLocalVariable(String arg0, String arg1, String arg2,
- Label arg3, Label arg4, int arg5) {
+ public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
+ mv.visitLocalVariable(name, desc, signature, start, end, index);
}
- public void visitLookupSwitchInsn(Label arg0, int[] arg1, Label[] arg2) {
+ public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
+ mv.visitLookupSwitchInsn(dflt, keys, labels);
}
- public void visitMaxs(int arg0, int arg1) {
+ public void visitMaxs(int maxStack, int maxLocals) {
+ mv.visitMaxs(maxStack, maxLocals);
}
- public void visitMethodInsn(int arg0, String arg1, String arg2, String arg3) {
+ public void visitMethodInsn(int opcode, String owner, String name, String desc) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ insn += ' ' + name + desc;
+ emitInsn(insn);
+ mv.visitMethodInsn(opcode, owner, name, desc);
}
- public void visitMultiANewArrayInsn(String arg0, int arg1) {
+ public void visitMultiANewArrayInsn(String desc, int dims) {
+ mv.visitMultiANewArrayInsn(desc, dims);
}
- public AnnotationVisitor visitParameterAnnotation(int arg0, String arg1,
- boolean arg2) {
- return null;
+ public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
+ return mv.visitParameterAnnotation(parameter, desc, visible);
}
- public void visitTableSwitchInsn(int arg0, int arg1, Label arg2,
- Label[] arg3) {
+ public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
+ mv.visitTableSwitchInsn(min, max, dflt, labels);
}
- public void visitTryCatchBlock(Label arg0, Label arg1, Label arg2,
- String arg3) {
+ public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
+ mv.visitTryCatchBlock(start, end, handler, type);
}
- public void visitTypeInsn(int arg0, String arg1) {
+ public void visitTypeInsn(int opcode, String type) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ insn += ' ' + type;
+ emitInsn(insn);
+ mv.visitTypeInsn(opcode, type);
}
- public void visitVarInsn(int arg0, int arg1) {
+ public void visitVarInsn(int opcode, int var) {
+ String insn = TraceClassVisitor.OPCODES[opcode];
+ insn += " " + var;
+ emitInsn(insn);
+ mv.visitVarInsn(opcode, var);
}
+
+ private void emitInsn(String insn) {
+ Label l = new Label();
+ mv.visitLabel(l);
+ mv.visitLineNumber(ln.getLineNumber(), l);
+ pw.println("\t\tPC = " + nf.format(l.getOffset()) + " // \t\t" + insn);
+ ln.inc();
+ }
}
Deleted: trunk/dashg/src/com/mebigfatguy/dashg/DashGSourceWriter.java
===================================================================
--- trunk/dashg/src/com/mebigfatguy/dashg/DashGSourceWriter.java 2008-10-28 19:24:51 UTC (rev 30)
+++ trunk/dashg/src/com/mebigfatguy/dashg/DashGSourceWriter.java 2008-10-28 19:25:16 UTC (rev 31)
@@ -1,231 +0,0 @@
-/*
- * dashg - A debug symbols (-g) injector
- * Copyright (C) 2008 Dave Brosius
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-package com.mebigfatguy.dashg;
-
-import java.io.PrintWriter;
-import java.text.NumberFormat;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class DashGSourceWriter extends PrintWriter implements DashGLineNumberer {
-
- enum State {BeforeClass, InClass, InMethod};
-
- private static final Pattern classPattern = Pattern.compile("(.*)class\\s*([^\\s]*)(.*)");
- private static final Pattern methodPattern = Pattern.compile("(.*)\\s+([a-zA-Z0-9<>_]+)\\(([^)]*)\\)([^\\s]+)(\\s+([_a-zA-Z0-9/]*))?");
- private static final Pattern fieldPattern = Pattern.compile("([^L]*)\\s+(L[^;]*;)?\\s+(.+)");
- private static final Pattern cruftPattern = Pattern.compile("((L[0-9]+)|(LINENUMBER)|(LOCALVARIABLE)|(MAXSTACK)|(MAXLOCALS)).*");
-
- private final PrintWriter writer;
- private State state = State.BeforeClass;
- private String clsName = null;
- private int lineNumber = 1;
- private NumberFormat nf;
-
- public DashGSourceWriter(PrintWriter pw, String packageName) {
- super(pw);
- writer = pw;
-
- nf = NumberFormat.getIntegerInstance();
- nf.setMinimumIntegerDigits(5);
- nf.setGroupingUsed(false);
-
- if ((packageName != null) && (packageName.length() > 0)) {
- writer.println("import " + packageName + ";");
- ++lineNumber;
- }
- }
-
- public int getLineNumber() {
- return lineNumber;
- }
-
- @Override
- public void print(String s) {
- String[] lines = s.split("\n");
- for (String l : lines) {
- processLine(l);
- ++lineNumber;
- }
- }
-
- private void processLine(String s) {
- State newState = state;
-
- s = s.trim();
- if (!s.startsWith("//")) {
- switch (state) {
- case BeforeClass:
- if (s.contains(" class ")) {
- Matcher m = classPattern.matcher(s);
- if (m.matches()) {
- String atts = m.group(1);
- clsName = m.group(2);
- String ext = m.group(3);
-
- int slashPos = clsName.lastIndexOf('/');
- if (slashPos >= 0)
- clsName = clsName.substring(slashPos+1);
-
- s = atts + "class " + clsName + ext.replace('/', '.');
- newState = State.InClass;
- }
- }
- break;
-
- case InClass:
- Matcher m = methodPattern.matcher(s);
- if (m.matches()) {
- m = methodPattern.matcher(s);
- if (m.matches()) {
- String atts = m.group(1);
- String name = m.group(2);
- String parms = m.group(3);
- String ret = m.group(4);
- String thr = m.group(6);
-
- if ("<init>".equals(name)) {
- name = clsName;
- }
-
- StringBuilder realParms = new StringBuilder();
- String comma = "";
- int parmNum = atts.contains("static") ? 0 : 1;
- while (parms.length() > 0) {
- realParms.append(comma);
- comma = ", ";
- String type = convertType(parms);
- realParms.append(type);
- realParms.append(' ');
- realParms.append(parmName(type, parmNum));
- parmNum += typeSize(type);
- parms = skipType(parms);
- }
- parms = realParms.toString();
-
- if (thr == null)
- thr = "";
- else
- thr = thr.replace('/', '.');
- s = atts + ' ' + convertType(ret) + ' ' + name + '(' + parms + ')' + thr;
- }
- newState = State.InMethod;
- } else {
- m = fieldPattern.matcher(s);
- if (m.matches()) {
- String type = m.group(2);
- if (type != null) {
- type = convertType(type);
- s = m.group(1) + ' ' + type + ' ' + m.group(3);
- }
-
- s += ";";
- }
- }
- break;
-
- case InMethod:
- if (s.length() == 0) {
- newState = State.InClass;
- }
- if (s.startsWith("//"))
- return;
-
- m = cruftPattern.matcher(s);
- if (m.matches()) {
- return;
- }
-
- s = "\tPC = " + nf.format(0) + ";\t// " + s;
- break;
- }
- }
-
- if ((newState == State.InClass) && (state == State.InMethod)) {
- super.print("}\n");
- ++lineNumber;
- }
-
- super.print(s + "\n");
-
- if ((state == State.InClass) && (newState == State.InMethod)) {
- super.print("{\n");
- ++lineNumber;
- super.print("\tint PC;\n");
- ++lineNumber;
- super.print("\n");
- ++lineNumber;
- }
-
- state = newState;
- }
-
- private String convertType(String type) {
- char c = type.charAt(0);
- switch (c) {
- case 'L':
- return type.substring(1, type.indexOf(';')).replace('/', '.');
- case '[':
- return convertType(type.substring(1)) + "[]";
- case 'C':
- return "char";
- case 'B':
- return "byte";
- case 'S':
- return "short";
- case 'I':
- return "int";
- case 'J':
- return "long";
- case 'F':
- return "float";
- case 'D':
- return "double";
- case 'Z':
- return "boolean";
- case 'V':
- return "void";
- }
- throw new RuntimeException("Type not processed: " + type);
- }
-
- private int typeSize(String typeName) {
- return ("long".equals(typeName) || "double".equals(typeName)) ? 2 : 1;
- }
-
- private String parmName(String type, int parmNum) {
- int dotPos = type.lastIndexOf('.');
- if (dotPos >= 0)
- type = type.substring(dotPos+1);
- return type.toLowerCase() + parmNum;
- }
-
- private String skipType(String type) {
- if (type.length() == 0)
- return type;
-
- if (type.charAt(0) == '[')
- return skipType(type.substring(1));
-
- if (type.charAt(0) == 'L')
- return type.substring(type.indexOf(';') + 1);
-
- return type.substring(1);
- }
-}
Modified: trunk/dashg/src/com/mebigfatguy/dashg/DashGTask.java
===================================================================
--- trunk/dashg/src/com/mebigfatguy/dashg/DashGTask.java 2008-10-28 19:24:51 UTC (rev 30)
+++ trunk/dashg/src/com/mebigfatguy/dashg/DashGTask.java 2008-10-28 19:25:16 UTC (rev 31)
@@ -115,8 +115,8 @@
ClassReader cr = new ClassReader(bis);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
- DashGSourceWriter sw = new DashGSourceWriter(pw, packageName);
- ClassAdapter ca = new DashGClassAdapter(new TraceClassVisitor(cw, sw), sw, baseName);
+ DashGClassSourcePrintingVisitor spv = new DashGClassSourcePrintingVisitor(cw, pw);
+ ClassAdapter ca = new DashGClassAdapter(spv, spv, baseName);
cr.accept(ca, 0);
byte[] classData = cw.toByteArray();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|