Skip to content

Commit 628e01f

Browse files
committed
feat: Change field and method parsers to both use DEFINE keyword
1 parent c3d9171 commit 628e01f

File tree

8 files changed

+79
-34
lines changed

8 files changed

+79
-34
lines changed

src/main/java/me/coley/recaf/parse/bytecode/Parse.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ public static AbstractParser<?> getParser(int lineNo, String token) throws ASTPa
115115
if(token.endsWith(":"))
116116
return new LabelParser();
117117
if (token.equals("DEFINE"))
118-
return new MethodDefinitionParser();
119-
if (token.equals("FIELD"))
120-
return new FieldDefinitionParser();
118+
return new DefinitionParser();
121119
if (token.equals("VALUE"))
122120
return new DefaultValueParser();
123121
if(token.equals("THROWS"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package me.coley.recaf.parse.bytecode.ast;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Common base for definition AST objects.
8+
*
9+
* @author Matt
10+
*/
11+
public abstract class DefinitionAST extends AST {
12+
protected final List<DefinitionModifierAST> modifiers = new ArrayList<>();
13+
protected final NameAST name;
14+
15+
/**
16+
* @param line
17+
* Line number this node is written on.
18+
* @param start
19+
* Offset from line start this node starts at.
20+
* @param name
21+
* Member name.
22+
*/
23+
public DefinitionAST(int line, int start, NameAST name) {
24+
super(line, start);
25+
this.name = name;
26+
}
27+
28+
/**
29+
* @return Full descriptor of the definition.
30+
*/
31+
abstract public String getDescriptor();
32+
33+
/**
34+
* @return Method name.
35+
*/
36+
public NameAST getName() {
37+
return name;
38+
}
39+
}

src/main/java/me/coley/recaf/parse/bytecode/ast/FieldDefinitionAST.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
*
1111
* @author Matt
1212
*/
13-
public class FieldDefinitionAST extends AST {
14-
private final List<DefinitionModifierAST> modifiers = new ArrayList<>();
15-
private final NameAST name;
13+
public class FieldDefinitionAST extends DefinitionAST {
1614
private final DescAST type;
1715

1816
/**
@@ -26,8 +24,7 @@ public class FieldDefinitionAST extends AST {
2624
* Field return type.
2725
*/
2826
public FieldDefinitionAST(int line, int start, NameAST name, DescAST type) {
29-
super(line, start);
30-
this.name = name;
27+
super(line, start, name);
3128
this.type = type;
3229
}
3330

@@ -47,23 +44,21 @@ public void addModifier(DefinitionModifierAST modifier) {
4744
addChild(modifier);
4845
}
4946

50-
/**
51-
* @return Field name.
52-
*/
53-
public NameAST getName() {
54-
return name;
55-
}
56-
5747
/**
5848
* @return Field type.
5949
*/
6050
public DescAST getType() {
6151
return type;
6252
}
6353

54+
@Override
55+
public String getDescriptor() {
56+
return getType().getDesc();
57+
}
58+
6459
@Override
6560
public String print() {
6661
String modifiersStr = getModifiers().stream().map(AST::print).collect(joining(" "));
67-
return "FIELD " + modifiersStr + " " + getType().print() + " " + getName().print();
62+
return "DEFINE " + modifiersStr + " " + getType().print() + " " + getName().print();
6863
}
6964
}

src/main/java/me/coley/recaf/parse/bytecode/ast/MethodDefinitionAST.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
*
1212
* @author Matt
1313
*/
14-
public class MethodDefinitionAST extends AST {
15-
private final List<DefinitionModifierAST> modifiers = new ArrayList<>();
14+
public class MethodDefinitionAST extends DefinitionAST {
1615
private final List<DefinitionArgAST> arguments = new ArrayList<>();
17-
private final NameAST name;
1816
private final DescAST retType;
1917

2018
/**
@@ -28,8 +26,7 @@ public class MethodDefinitionAST extends AST {
2826
* Method return type.
2927
*/
3028
public MethodDefinitionAST(int line, int start, NameAST name, DescAST retType) {
31-
super(line, start);
32-
this.name = name;
29+
super(line, start, name);
3330
this.retType = retType;
3431
}
3532

@@ -74,13 +71,6 @@ public void addArgument(DefinitionArgAST arg) {
7471
addChild(arg);
7572
}
7673

77-
/**
78-
* @return Method name.
79-
*/
80-
public NameAST getName() {
81-
return name;
82-
}
83-
8474
/**
8575
* @return Method return type.
8676
*/
@@ -92,6 +82,7 @@ public DescAST getReturnType() {
9282
/**
9383
* @return Combined method descriptor of argument children and return type child.
9484
*/
85+
@Override
9586
public String getDescriptor() {
9687
String args = search(DefinitionArgAST.class).stream()
9788
.map(ast -> ast.getDesc().getDesc())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package me.coley.recaf.parse.bytecode.parser;
2+
3+
import me.coley.recaf.parse.bytecode.AbstractParser;
4+
import me.coley.recaf.parse.bytecode.ast.DefinitionAST;
5+
import me.coley.recaf.parse.bytecode.exception.ASTParseException;
6+
7+
/**
8+
* Wrapper parser for all parsers using the {@code DEFINE} keyword.
9+
*
10+
* @author Matt
11+
* @see FieldDefinitionParser
12+
* @see MethodDefinitionParser
13+
*/
14+
public class DefinitionParser extends AbstractParser<DefinitionAST> {
15+
@Override
16+
public DefinitionAST visit(int lineNo, String text) throws ASTParseException {
17+
if (text.contains("("))
18+
return new MethodDefinitionParser().visit(lineNo, text);
19+
else
20+
return new FieldDefinitionParser().visit(lineNo, text);
21+
}
22+
}

src/main/java/me/coley/recaf/parse/bytecode/parser/FieldDefinitionParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Matt
2020
*/
2121
public class FieldDefinitionParser extends AbstractParser<FieldDefinitionAST> {
22-
private static final int DEFINE_LEN = "FIELD ".length();
22+
private static final int DEFINE_LEN = "DEFINE ".length();
2323

2424
@Override
2525
public FieldDefinitionAST visit(int lineNo, String line) throws ASTParseException {

src/main/java/me/coley/recaf/parse/bytecode/parser/FieldInsnParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public FieldInsnAST visit(int lineNo, String line) throws ASTParseException {
1919
try {
2020
String[] trim = line.trim().split("\\s+");
2121
if (trim.length < 3)
22-
throw new ASTParseException(lineNo, "Not enough paramters");
22+
throw new ASTParseException(lineNo, "Not enough parameters");
2323
int start = line.indexOf(trim[0]);
2424
// op
2525
OpcodeParser opParser = new OpcodeParser();
@@ -53,7 +53,7 @@ public FieldInsnAST visit(int lineNo, String line) throws ASTParseException {
5353

5454
@Override
5555
public List<String> suggest(ParseResult<RootAST> lastParse, String text) {
56-
// FIELD owner.name desc
56+
// DEFINE owner.name desc
5757
int space = text.indexOf(' ');
5858
if (space >= 0) {
5959
String sub = text.substring(space + 1);

src/test/java/me/coley/recaf/AssemblyAstTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void testMethodDefine() {
105105

106106
@Test
107107
public void testFieldDefine() {
108-
FieldDefinitionAST def = single("FIELD public static Ljava/util/List; myList");
108+
FieldDefinitionAST def = single("DEFINE public static Ljava/util/List; myList");
109109
assertEquals("myList", def.getName().getName());
110110
assertEquals(2, def.getModifiers().size());
111111
assertEquals("public", def.getModifiers().get(0).getName());
@@ -122,15 +122,15 @@ public void testMethodDefineNoModifiers() {
122122

123123
@Test
124124
public void testFieldDefineNoModifiers() {
125-
FieldDefinitionAST def = single("FIELD Ljava/util/List; myList");
125+
FieldDefinitionAST def = single("DEFINE Ljava/util/List; myList");
126126
assertEquals("myList", def.getName().getName());
127127
assertEquals("Ljava/util/List;", def.getType().getDesc());
128128
assertEquals(0, def.getModifiers().size());
129129
}
130130

131131
@Test
132132
public void testFieldDefinePrimitive() {
133-
FieldDefinitionAST def = single("FIELD J myLong");
133+
FieldDefinitionAST def = single("DEFINE J myLong");
134134
assertEquals("myLong", def.getName().getName());
135135
assertEquals("J", def.getType().getDesc());
136136
assertEquals(0, def.getModifiers().size());

0 commit comments

Comments
 (0)