Skip to content

Commit

Permalink
Improve anonymous inner class parsing (in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherney committed Jan 2, 2020
1 parent c8c9753 commit 681e652
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 16 deletions.
74 changes: 59 additions & 15 deletions src/main/java/com/chrisney/enigma/parser/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,28 @@ private ArrayList<CodeBlock> parse(String source, CodeBlock parent, ArrayList<Co
block.code = source.substring(block.start, block.end);

// Analyze sub source code:
if (block.code.endsWith(String.valueOf(cCurlyBracketClose)) && !CodeBlock.isComment(currentBlock)) {
int subCodeStart = startOfSubCode(block);
int subCodeEnd = block.code.length() - 1;
block.subCode = block.code.substring(subCodeStart, subCodeEnd);
ArrayList<Integer[]> indexes = null;
if (!CodeBlock.isComment(currentBlock)) indexes = getSubBlockIndexes(block);

// Set block type:
block.type = getBlockType(block);
// Search block name and properties:
parseBlockProperties(block);
if (indexes != null && indexes.size() > 0) {

for (Integer[] subItem : indexes) {
int subCodeStart = subItem[0];
int subCodeEnd = subItem[1];

block.subCode = block.code.substring(subCodeStart, subCodeEnd);

// Set block type:
block.type = getBlockType(block);
// Search block name and properties:
parseBlockProperties(block);

// Compute the sub block offset (chars index):
int subBlockOffset = block.start + block.offset + subCodeStart;
// Compute the sub block offset (chars index):
int subBlockOffset = block.start + block.offset + subCodeStart;

// Parse the sub block:
block.subBlocks = this.parse(block.subCode, block, null, subBlockOffset);
// Parse the sub block:
block.subBlocks.addAll(this.parse(block.subCode, block, null, subBlockOffset));
}

} else {
// Set block type:
Expand Down Expand Up @@ -314,20 +321,57 @@ private ArrayList<CodeBlock> parse(String source, CodeBlock parent, ArrayList<Co
return blocks;
}

private ArrayList<Integer[]> getSubBlockIndexes(CodeBlock block) {
ArrayList<Integer[]> indexes = new ArrayList<>();
int j;
int counterCurlyBracket = 0;
char previousNoneEmptyChar = ' ';
for (j = 0; j < block.code.length(); j++) {

char c = block.code.charAt(j);

if (c == cCurlyBracketOpen) counterCurlyBracket++;
if (c == cCurlyBracketClose) counterCurlyBracket--;

if (c == cCurlyBracketOpen && counterCurlyBracket == 1 && previousNoneEmptyChar != cBracketClose) {
indexes.add(new Integer[] {j + 1, 0});
} else if (c == cCurlyBracketClose && counterCurlyBracket == 0 && indexes.size() > 0) {
indexes.get(indexes.size() -1)[1] = j - 1;
}

if (!TextUtils.isEmptyChar(c)) previousNoneEmptyChar = c;
}
return indexes;
}

/**
* Detect the start of sub code content
* @param block Block of code
* @return Char index of start of the sub code
* @return Char index of the start of the sub code
*/
private int startOfSubCode(CodeBlock block) {
int j;
char previousNoneEmptyChar = ' ';
for (j = 0; j < block.code.length(); j++) {
char c = block.code.charAt(j);
if (c == cCurlyBracketOpen && previousNoneEmptyChar != cBracketClose) break;
if (c == cCurlyBracketOpen && previousNoneEmptyChar != cBracketClose)
return j + 1;
if (!TextUtils.isEmptyChar(c)) previousNoneEmptyChar = c;
}
return j + 1;
return 0;
}

/**
* Detect end of sub code content
* @param block Block of code
* @return Char index of the end of the sub code
*/
private int endOfSubCode(CodeBlock block) {
for (int i = block.code.length() - 1; i > 0; i--) {
char c = block.code.charAt(i);
if (c == cCurlyBracketClose) return i - 1;
}
return block.code.length() - 1;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/TestJavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public static void testJavaParser() {
// File javaFile = Utils.getFileResource("ToggleFlag.java");
// File javaFile = Utils.getFileResource("GridBackupTable.java");
// File javaFile = Utils.getFileResource("Folder.java");
File javaFile = Utils.getFileResource("Annotations.java");
// File javaFile = Utils.getFileResource("Annotations.java");
File javaFile = Utils.getFileResource("AnonymousInnerClass.java");

String code = FileUtils.readFileToString(javaFile, "UTF-8");

Expand Down
28 changes: 28 additions & 0 deletions src/test/resources/AnonymousInnerClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.android.app.activity;

public class MyActivity extends Activity {

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_layout_id);

final Button button = (Button) findViewById(R.id.my_cool_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
}
}, true);

this.fake_method(new MyListener() {
public void onResult(boolean success) {
System.out.println(success);
}
}, 300,
new MyListener() {
public void onResult(boolean success) {
System.out.println(success);
}
}, false);
}

}

0 comments on commit 681e652

Please sign in to comment.