diff --git a/baksmali/pom.xml b/baksmali/pom.xml
index b2f8bad6..419e4ee1 100644
--- a/baksmali/pom.xml
+++ b/baksmali/pom.xml
@@ -6,20 +6,66 @@
baksmali
0.91-SNAPSHOT
- org.jf
- smali-pom
- 0.91-SNAPSHOT
+ org.jf
+ smali-pom
+ 0.91-SNAPSHOT
+
+
+
+ org.jf
+ maven-smali-plugin
+
+
+ junit-tests
+ test-compile
+
+ assemble
+
+
+ src/test/smali
+ target/test/classes.dex
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 1.1
+
+
+ test
+
+ exec
+
+
+
+
+ java
+ ${basedir}
+
+ -classpath
+
+ org.jf.baksmali.main
+ --disassemble
+ ${project.build.directory}/test/classes.dex
+ ${project.build.directory}/test/out
+
+
+
+
+
- org.antlr
- stringtemplate
- 3.2
+ org.antlr
+ stringtemplate
+ 3.2
+
+
+ org.jf
+ dexlib
+ 0.91-SNAPSHOT
-
- org.jf
- dexlib
- 0.91-SNAPSHOT
-
\ No newline at end of file
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodDefinition.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodDefinition.java
index bd87aeda..ee78b814 100644
--- a/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodDefinition.java
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodDefinition.java
@@ -31,12 +31,18 @@ package org.jf.baksmali.wrappers;
import org.jf.dexlib.ClassDataItem;
import org.jf.dexlib.CodeItem;
import org.jf.dexlib.MethodIdItem;
-import org.jf.dexlib.code.Instruction;
import org.jf.dexlib.code.InstructionField;
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Instruction10x;
+import org.jf.dexlib.code.Format.Instruction35c;
+import org.jf.dexlib.code.Format.Instruction21c;
+import org.jf.dexlib.code.Format.Instruction11x;
import org.jf.dexlib.util.AccessFlags;
+import org.jf.baksmali.wrappers.format.*;
import java.util.List;
import java.util.ArrayList;
+import java.util.Collections;
public class MethodDefinition {
private ClassDataItem.EncodedMethod encodedMethod;
@@ -97,17 +103,50 @@ public class MethodDefinition {
return registerCount;
}
- //TODO: how best to abstract this? I don't like exposing dexlib's "Instruction" directly
- private List instructions = null;
- public List getInstructions() {
- if (instructions == null) {
- instructions = new ArrayList();
- if (codeItem != null) {
- for (InstructionField instruction: codeItem.getInstructions()) {
- instructions.add(instruction.getInstruction());
- }
+
+ private List methodItems = null;
+ public List getMethodItems() {
+ if (methodItems == null) {
+ methodItems = generateMethodItemList();
+ }
+ return methodItems;
+ }
+
+
+ private List generateMethodItemList() {
+ if (codeItem == null) {
+ return new ArrayList();
+ }
+
+ List methodItems = new ArrayList();
+
+
+ int offset = 0;
+ for (InstructionField instructionField: codeItem.getInstructions()) {
+ for (MethodItem methodItem: getMethodItemsForInstruction(offset, instructionField)) {
+ methodItems.add(methodItem);
}
}
- return instructions;
+
+ Collections.sort(methodItems);
+
+ return methodItems;
+ }
+
+ private MethodItem[] getMethodItemsForInstruction(int offset, InstructionField instructionField) {
+ Instruction instruction = instructionField.getInstruction();
+
+ switch (instruction.getFormat()) {
+ case Format10x:
+ return new MethodItem[] {new Instruction10xMethodItem(offset, (Instruction10x)instruction)};
+ case Format11x:
+ return new MethodItem[] {new Instruction11xMethodItem(offset, (Instruction11x)instruction)};
+ case Format21c:
+ return new MethodItem[] {new Instruction21cMethodItem(offset, (Instruction21c)instruction)};
+ case Format35c:
+ return new MethodItem[] {new Instruction35cMethodItem(offset, (Instruction35c)instruction)};
+ default:
+ return null;
+ }
}
}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodItem.java
new file mode 100644
index 00000000..5a260f18
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/MethodItem.java
@@ -0,0 +1,57 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers;
+
+public abstract class MethodItem implements Comparable {
+ private int offset;
+
+ protected MethodItem(int offset) {
+ this.offset = offset;
+ }
+
+
+ public int getOffset() {
+ return offset;
+ }
+
+ //return the name of the template that should be used to render this item
+ public abstract String getTemplate();
+ //return an arbitrary integer that determines how this item will be sorted with
+ //others at the same offset
+ public abstract int getSortOrder();
+
+ public int compareTo(MethodItem methodItem) {
+ int result = ((Integer)offset).compareTo(methodItem.offset);
+
+ if (result == 0){
+ return ((Integer)getSortOrder()).compareTo(methodItem.getSortOrder());
+ }
+ return result;
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction10xMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction10xMethodItem.java
new file mode 100644
index 00000000..f37246c3
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction10xMethodItem.java
@@ -0,0 +1,38 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.format;
+
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Instruction10x;
+
+public class Instruction10xMethodItem extends InstructionFormatMethodItem {
+ public Instruction10xMethodItem(int offset, Instruction10x instruction) {
+ super(offset, instruction);
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction11xMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction11xMethodItem.java
new file mode 100644
index 00000000..d34e182a
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction11xMethodItem.java
@@ -0,0 +1,43 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.format;
+
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Instruction11x;
+
+public class Instruction11xMethodItem extends InstructionFormatMethodItem {
+ public Instruction11xMethodItem(int offset, Instruction11x instruction) {
+ super(offset, instruction);
+ }
+
+ public int getRegister() {
+ return instruction.getRegister();
+ }
+}
+
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction21cMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction21cMethodItem.java
new file mode 100644
index 00000000..54646bc2
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction21cMethodItem.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.format;
+
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Instruction21c;
+
+public class Instruction21cMethodItem extends InstructionFormatMethodItem {
+ public Instruction21cMethodItem(int offset, Instruction21c instruction) {
+ super(offset, instruction);
+ }
+
+ public int getRegister() {
+ return instruction.getRegister();
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction35cMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction35cMethodItem.java
new file mode 100644
index 00000000..acc12ef1
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/Instruction35cMethodItem.java
@@ -0,0 +1,57 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.format;
+
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Instruction35c;
+
+public class Instruction35cMethodItem extends InstructionFormatMethodItem {
+ public Instruction35cMethodItem(int offset, Instruction35c instruction) {
+ super(offset, instruction);
+ }
+
+ public int[] getRegisters() {
+ switch (instruction.getRegCount()) {
+ case 1:
+ return new int[] {instruction.getRegisterD()};
+ case 2:
+ return new int[] {instruction.getRegisterD(), instruction.getRegisterE()};
+ case 3:
+ return new int[] {instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF()};
+ case 4:
+ return new int[] {instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF(),
+ instruction.getRegisterG()};
+ case 5:
+ return new int[] {instruction.getRegisterD(), instruction.getRegisterE(), instruction.getRegisterF(),
+ instruction.getRegisterG(), instruction.getRegisterA()};
+ default:
+ return new int[0];
+ }
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/format/InstructionFormatMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/InstructionFormatMethodItem.java
new file mode 100644
index 00000000..c7dbea8c
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/format/InstructionFormatMethodItem.java
@@ -0,0 +1,60 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.format;
+
+import org.jf.baksmali.wrappers.MethodItem;
+import org.jf.baksmali.wrappers.reference.Reference;
+import org.jf.dexlib.code.Instruction;
+import org.jf.dexlib.code.Format.Format;
+
+public abstract class InstructionFormatMethodItem extends MethodItem {
+ protected T instruction;
+
+ public InstructionFormatMethodItem(int offset, T instruction) {
+ super(offset);
+ this.instruction = instruction;
+ }
+
+ public int getSortOrder() {
+ //instructions should appear after everything except an "end try" label
+ return 100;
+ }
+
+ public String getOpcode() {
+ return instruction.getOpcode().name;
+ }
+
+ public String getTemplate() {
+ return instruction.getFormat().name();
+ }
+
+ public Reference getReference() {
+ return Reference.makeReference(instruction.getReferencedItem());
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/FieldReference.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/FieldReference.java
new file mode 100644
index 00000000..5cb9b013
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/FieldReference.java
@@ -0,0 +1,61 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.reference;
+
+import org.jf.dexlib.FieldIdItem;
+
+public class FieldReference extends Reference {
+ public FieldReference(FieldIdItem item) {
+ super(item);
+ }
+
+ private String containingClass = null;
+ public String getContainingClass() {
+ if (containingClass == null) {
+ containingClass = item.getContainingClass().getTypeDescriptor();
+ }
+ return containingClass;
+ }
+
+ private String fieldName = null;
+ public String getFieldName() {
+ if (fieldName == null) {
+ fieldName = item.getFieldName().getStringValue();
+ }
+ return fieldName;
+ }
+
+ private String fieldType = null;
+ public String getFieldType() {
+ if (fieldType == null) {
+ fieldType = item.getFieldType().getTypeDescriptor();
+ }
+ return fieldType;
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/MethodReference.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/MethodReference.java
new file mode 100644
index 00000000..74998c1a
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/MethodReference.java
@@ -0,0 +1,49 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.reference;
+
+import org.jf.dexlib.MethodIdItem;
+
+public class MethodReference extends Reference {
+ public MethodReference(MethodIdItem item) {
+ super(item);
+ }
+
+ public String getContainingClass() {
+ return item.getContainingClass().getTypeDescriptor();
+ }
+
+ public String getMethodName() {
+ return item.getMethodName();
+ }
+
+ public String getPrototype() {
+ return item.getPrototype().getPrototypeString();
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/Reference.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/Reference.java
new file mode 100644
index 00000000..a3efb19a
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/Reference.java
@@ -0,0 +1,57 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.reference;
+
+import org.jf.dexlib.*;
+
+public abstract class Reference {
+ protected T item;
+
+ protected Reference(T item) {
+ this.item = item;
+ }
+
+ public static Reference makeReference(IndexedItem item) {
+ switch (item.getItemType()) {
+ case TYPE_METHOD_ID_ITEM:
+ return new MethodReference((MethodIdItem)item);
+ case TYPE_FIELD_ID_ITEM:
+ return new FieldReference((FieldIdItem)item);
+ case TYPE_STRING_ID_ITEM:
+ return new StringReference((StringIdItem)item);
+ case TYPE_TYPE_ID_ITEM:
+ return new TypeReference((TypeIdItem)item);
+ }
+ return null;
+ }
+
+ public String getTemplate() {
+ return this.getClass().getSimpleName();
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/StringReference.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/StringReference.java
new file mode 100644
index 00000000..f34b2ad0
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/StringReference.java
@@ -0,0 +1,42 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.reference;
+
+import org.jf.dexlib.StringIdItem;
+import org.jf.dexlib.util.Utf8Utils;
+
+public class StringReference extends Reference {
+ public StringReference(StringIdItem item) {
+ super(item);
+ }
+
+ public String getEscapedValue() {
+ return Utf8Utils.escapeString(item.getStringValue());
+ }
+}
diff --git a/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/TypeReference.java b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/TypeReference.java
new file mode 100644
index 00000000..ff7a7413
--- /dev/null
+++ b/baksmali/src/main/java/org/jf/baksmali/wrappers/reference/TypeReference.java
@@ -0,0 +1,41 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2009 Ben Gruver
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.baksmali.wrappers.reference;
+
+import org.jf.dexlib.TypeIdItem;
+
+public class TypeReference extends Reference {
+ public TypeReference(TypeIdItem item) {
+ super(item);
+ }
+
+ public String getTypeDescriptor() {
+ return item.getTypeDescriptor();
+ }
+}
diff --git a/baksmali/src/resources/templates/baksmali.stg b/baksmali/src/main/resources/templates/baksmali.stg
similarity index 55%
rename from baksmali/src/resources/templates/baksmali.stg
rename to baksmali/src/main/resources/templates/baksmali.stg
index 5365ef82..4e0375b5 100644
--- a/baksmali/src/resources/templates/baksmali.stg
+++ b/baksmali/src/main/resources/templates/baksmali.stg
@@ -69,37 +69,59 @@ method(methodDef) ::=
.registers
-
+
.end method
>>
-Instruction(Instruction) ::=
+MethodItem(MethodItem) ::=
<<
-
+
>>
-Format10x() ::=
+
+
+
+
+Format10x(Instruction) ::=
<<
-
+
>>
-Format11x() ::=
+Format11x(Instruction) ::=
<<
- v
+ v
>>
-Format21c() ::=
+Format21c(Instruction) ::=
<<
- v, <({Reference_})(it.ReferencedItem)>
+ v,
>>
-Format35c() ::=
+Format35c(Instruction) ::=
<<
-
+ {}; separator=", ">},
>>
-Reference_string(StringItem) ::=
+
+
+
+Reference(ReferencedItem) ::=
<<
-""
+<(ReferencedItem.Template)(ReferencedItem)>
+>>
+
+StringReference(StringReference) ::=
+<<
+""
+>>
+
+FieldReference(FieldReference) ::=
+<<
+->:
+>>
+
+MethodReference(MethodReference) ::=
+<<
+->
>>
\ No newline at end of file
diff --git a/baksmali/src/test/smali/baksmali_test_class.smali b/baksmali/src/test/smali/baksmali_test_class.smali
new file mode 100644
index 00000000..354416b2
--- /dev/null
+++ b/baksmali/src/test/smali/baksmali_test_class.smali
@@ -0,0 +1,24 @@
+.class public Lbaksmali/test/class;
+.super Ljava/lang/Object;
+
+.implements Lsome/interface;
+.implements Lsome/other/interface;
+
+.field public static staticField:I
+.field public instanceField:Ljava/lang/String;
+
+.method public constructor ()V
+ .registers 1
+ invoke-direct {p0}, Ljava/lang/Object;->()V
+ return-void
+.end method
+
+.method public testMethod(ILjava/lang/String;)Ljava/lang/String;
+ .registers 3
+ const-string v0, "testing\n123"
+
+ sget v0, Lbaksmali/test/class;->staticField:I
+
+ return-object v0
+.end method
+