Skip to content

Commit

Permalink
Fix Empty Stack and Anonymous Bind var incorrect problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Lynn Dai committed Jul 25, 2022
1 parent 57154e2 commit f0f05fc
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 78 deletions.
Binary file added jar/enre_java_1.1.6.jar
Binary file not shown.
38 changes: 13 additions & 25 deletions src/main/java/TempOutput/ProcessHidden.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,15 @@ public void convertCSV2DB(String csvPath) {
String methodName = process_method(e.split("->", 2)[1]).getLeft();
ArrayList<String> parameter = process_method(e.split("->", 2)[1]).getMiddle();
String returnType = process_method(e.split("->", 2)[1]).getRight();
entity.setKind("Method");
if (methodName.equals("Constructor")){
String[] temp = classQualifiedName.split("\\.");
methodName = temp[temp.length - 1];
}
String entityQualifiedName = classQualifiedName+"."+methodName;
if (methodName.equals("Class_Constructor")){
entityQualifiedName = classQualifiedName;
entity.setKind("Type");
}
entity.setQualifiedName(entityQualifiedName);
entity.setParameter(parameter);
Expand All @@ -314,6 +316,7 @@ public void convertCSV2DB(String csvPath) {
String fieldType = process_field(e.split("->", 2)[1]).getR();
entity.setRawType(fieldType);
entity.setQualifiedName(classQualifiedName+"."+fieldName);
entity.setKind("Field");
}
}else {
if (entity != null && entity.qualifiedName != null){
Expand Down Expand Up @@ -394,14 +397,14 @@ public String checkHidden(TypeEntity entity){
if (this.result.containsKey(qualifiedName)){
if (this.result.get(qualifiedName).size() == 1){
this.result.get(qualifiedName).get(0).setMatch(true);
this.result.get(qualifiedName).get(0).setKind("Type");
// this.result.get(qualifiedName).get(0).setKind("Type");
return refactorHidden(this.result.get(qualifiedName).get(0).getHiddenApi());
}
else {
for (HiddenEntity hiddenEntity: this.result.get(qualifiedName)){
if (hiddenEntity.getOriginalSignature().contains("<clinit>")){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Type");
// hiddenEntity.setKind("Type");
return refactorHidden(hiddenEntity.getHiddenApi());
}
}
Expand All @@ -419,7 +422,7 @@ public String checkHidden(MethodEntity entity, String parType){
if (this.result.containsKey(qualifiedName)){
if (this.result.get(qualifiedName).size() == 1){
this.result.get(qualifiedName).get(0).setMatch(true);
this.result.get(qualifiedName).get(0).setKind("Method");
// this.result.get(qualifiedName).get(0).setKind("Method");
return refactorHidden(this.result.get(qualifiedName).get(0).getHiddenApi());
}
else {
Expand All @@ -428,13 +431,13 @@ public String checkHidden(MethodEntity entity, String parType){
if (entity.isConstructor()){
if (comparePara(hiddenEntity.getParameter(), parType.split(" "))){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Method");
// hiddenEntity.setKind("Method");
return refactorHidden(hiddenEntity.getHiddenApi());
}
} else if (JsonString.processRawType(entity.getRawType()).contains(hiddenEntity.getRawType())){
if (comparePara(hiddenEntity.getParameter(), parType.split(" "))){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Method");
// hiddenEntity.setKind("Method");
return refactorHidden(hiddenEntity.getHiddenApi());
}
}
Expand Down Expand Up @@ -464,7 +467,7 @@ public String checkHidden(VariableEntity entity){
for (HiddenEntity hiddenEntity: this.result.get(qualifiedName)){
if (JsonString.processRawType(entity.getRawType()).contains(hiddenEntity.getRawType())){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Field");
// hiddenEntity.setKind("Field");
return refactorHidden(hiddenEntity.getHiddenApi());
}
}
Expand Down Expand Up @@ -508,7 +511,7 @@ public void outputResult() throws IOException {
fileOs = new FileOutputStream(fileName);
out = new OutputStreamWriter(fileOs, "GBK");
//字符数组是头行
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader( "inBase", "OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader( "inBase", "Type", "OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
List<Object> objects = new ArrayList<>();
for (ArrayList<HiddenEntity> hiddenEntities : this.getResult().values()) {
for (HiddenEntity entity : hiddenEntities){
Expand All @@ -521,6 +524,7 @@ public void outputResult() throws IOException {
&& !entity.getOriginalSignature().startsWith("Llibcore/") && !entity.getOriginalSignature().startsWith("Lorg/")
&& !entity.getOriginalSignature().startsWith("Lsun/") && !entity.getOriginalSignature().startsWith("Landroid/Manifest$")){
objects.add(checkBaseHidden(entity.getOriginalSignature()));
objects.add(entity.getKind());
objects.add(entity.getOriginalSignature());
objects.add(entity.getQualifiedName());
objects.add(entity.getRawType());
Expand All @@ -535,23 +539,6 @@ public void outputResult() throws IOException {
}
out.flush();

// JSONObject obj=new JSONObject();
// List<JSONObject> subCategories = new ArrayList<>();
// for (ArrayList<HiddenEntity> hiddenEntities: this.getResult().values()){
// for (HiddenEntity hidden : hiddenEntities){
// if (!hidden.isMatch){
// JSONObject current = new JSONObject();
// current.put("signature", hidden.getOriginalSignature());
// current.put("qualifiedName", hidden.getQualifiedName());
// current.put("rawType", hidden.getRawType());
// current.put("parameter", hidden.getParameter());
// current.put("hiddenApi", hidden.getHiddenApi());
// subCategories.add(current);
// }
// }
// }
// obj.accumulate("NotMatch", subCategories);
// return obj.toString();
}

public void checkMatch(String qualifiedName, String rawType, String parameterType){
Expand Down Expand Up @@ -584,11 +571,12 @@ public void outputConvertInfo(String outputFilePath) throws IOException {
fileOs = new FileOutputStream(outputFilePath);
out = new OutputStreamWriter(fileOs, "GBK");
//字符数组是头行
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("isBase", "OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("isBase", "Kind","OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
List<Object> objects = new ArrayList<>();
for (ArrayList<HiddenEntity> hiddenEntities : this.getResult().values()) {
for (HiddenEntity entity : hiddenEntities){
objects.add(checkBaseHidden(entity.getOriginalSignature()));
objects.add(entity.getKind());
objects.add(entity.getOriginalSignature());
objects.add(entity.getQualifiedName());
objects.add(entity.getRawType());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/util/Configure.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package util;

import java.io.File;

public class Configure {

private Configure() {}
Expand Down Expand Up @@ -130,6 +128,7 @@ private Configure() {}
public static final String LOCAL_BLOCK_SWITCH_CASE_CLAUSE = "SwitchCaseClauseBlock"; //include default
public static final String LOCAL_BLOCK_TRY = "TryBlock";
public static final String LOCAL_BLOCK_CATCH = "CatchBlock";
public static final String LAMBDA_BLOCK = "LambdaBlock";

//kinds of annotation target type
public static final String ANNOTATION_TYPE = "Another annotation";
Expand Down
137 changes: 89 additions & 48 deletions src/main/java/visitor/EntityVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import util.SingleCollect;
import util.Tuple;

import java.sql.Ref;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
Expand Down Expand Up @@ -212,6 +211,7 @@ public void endVisit(AnonymousClassDeclaration node) {
if(!scopeStack.isEmpty()){
scopeStack.pop();
}
entityStack.pop();
super.endVisit(node);
}

Expand Down Expand Up @@ -386,26 +386,46 @@ public void endVisit(MethodDeclaration node){
//pop entity stack
scopeStack.pop();

entityStack.pop();

//pop block stack
blockStack.pop();

}

@Override
public boolean visit(Block node) {
if (blockStack.isEmpty()){
if (isIf){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_IF);
blockStack.push(localBlockId);
} else if (isEnhancedFor) {
int localBlockId = createABlock(Configure.LOCAL_BLOCK_ENHANCED_FOR);
blockStack.push(localBlockId);
} else if (isFor) {
int localBlockId = createABlock(Configure.LOCAL_BLOCK_FOR);
blockStack.push(localBlockId);
} else if (isCatch){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_CATCH);
blockStack.push(localBlockId);
} else if (isLambda) {
int localBlockId = createABlock(Configure.LAMBDA_BLOCK);
blockStack.push(localBlockId);
} else if (blockStack.isEmpty()){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_STATIC);
blockStack.push(localBlockId);
} else {
int localBlockId = createABlock(Configure.LOCAL_BLOCK_UNNAMED_BLOCK);
blockStack.push(localBlockId);
}
return super.visit(node);
}

@Override
public void endVisit(Block node) {
//pop block stack
if (!blockStack.isEmpty() && blockStack.peek() == -1){
// if (!blockStack.isEmpty() && blockStack.peek() == -1){
blockStack.pop();
}
// }
super.endVisit(node);
}

Expand All @@ -429,10 +449,10 @@ public boolean visit(VariableDeclarationStatement node){

if(blockStack.isEmpty()){
/**
* static initial
* 1. static initial(finish)
* 2. lambda expression contains if block
*/
// System.out.println(node.toString());
// System.out.println(fileFullPath);
System.out.println("Variable declaration in an empty block !!");
}else{
// System.out.println(blockStackForMethod.peek());
// int currentBlock = blockStackForMethod.peek();
Expand Down Expand Up @@ -832,14 +852,20 @@ public void endVisit(Assignment node) {
super.endVisit(node);
}

boolean isFor = false;
boolean isEnhancedFor = false;
boolean isCatch = false;
boolean isIf = false;

@Override
public boolean visit(ForStatement node){
if(scopeStack.peek() != -1){
if(singleCollect.isMethod(scopeStack.peek())){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_FOR);
blockStack.push(localBlockId);
}
}
// if(scopeStack.peek() != -1){
// if(singleCollect.isMethod(scopeStack.peek())){
// int localBlockId = createABlock(Configure.LOCAL_BLOCK_FOR);
// blockStack.push(localBlockId);
// }
// }
isFor = true;

//CK
singleCollect.addCk(Configure.LOOPS, 1);
Expand All @@ -849,23 +875,21 @@ public boolean visit(ForStatement node){

@Override
public void endVisit(ForStatement node){
if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
blockStack.pop();
}
// if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
// blockStack.pop();
// }
isFor = false;
}

@Override
public boolean visit(EnhancedForStatement node){
if(scopeStack.peek() != -1){
/**
* static initial
*/
if(singleCollect.isMethod(scopeStack.peek())){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_ENHANCED_FOR);
blockStack.push(localBlockId);
}
}

// if(scopeStack.peek() != -1){
// if(singleCollect.isMethod(scopeStack.peek())){
// int localBlockId = createABlock(Configure.LOCAL_BLOCK_ENHANCED_FOR);
// blockStack.push(localBlockId);
// }
// }
isEnhancedFor = true;
//CK
singleCollect.addCk(Configure.LOOPS, 1);

Expand All @@ -874,46 +898,51 @@ public boolean visit(EnhancedForStatement node){

@Override
public void endVisit(EnhancedForStatement node){
if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
blockStack.pop();
}
// if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
// blockStack.pop();
// }
isEnhancedFor = false;
}

@Override
public boolean visit(CatchClause node) {
if(scopeStack.peek() != -1){
if(singleCollect.isMethod(scopeStack.peek())){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_CATCH);
blockStack.push(localBlockId);
}
}
// if(scopeStack.peek() != -1){
// if(singleCollect.isMethod(scopeStack.peek())){
// int localBlockId = createABlock(Configure.LOCAL_BLOCK_CATCH);
// blockStack.push(localBlockId);
// }
// }
isCatch = true;
return super.visit(node);
}

@Override
public void endVisit(CatchClause node) {
if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
blockStack.pop();
}
// if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
// blockStack.pop();
// }
isCatch = false;
super.endVisit(node);
}

@Override
public boolean visit(IfStatement node) {
if(scopeStack.peek() != -1){
if(singleCollect.isMethod(scopeStack.peek())){
int localBlockId = createABlock(Configure.LOCAL_BLOCK_IF);
blockStack.push(localBlockId);
}
}
// if(scopeStack.peek() != -1){
// if(singleCollect.isMethod(scopeStack.peek())){
// int localBlockId = createABlock(Configure.LOCAL_BLOCK_IF);
// blockStack.push(localBlockId);
// }
// }
isIf = true;
return super.visit(node);
}

@Override
public void endVisit(IfStatement node){
if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
blockStack.pop();
}
// if(scopeStack.peek() != -1 && !blockStack.isEmpty()) {
// blockStack.pop();
// }
isIf = false;
}

@Override
Expand Down Expand Up @@ -996,11 +1025,23 @@ public boolean visit(SimpleName node) {
return super.visit(node);
}

boolean isLambda = false;
@Override
public boolean visit(LambdaExpression node){

// if (blockStack.isEmpty()){
// int localBlockId = createABlock(Configure.Lambda_BLOCK);
// blockStack.push(localBlockId);
// }
isLambda = true;
return super.visit(node);
}
@Override
public void endVisit(LambdaExpression node){
// if (!blockStack.isEmpty() && blockStack.peek() == -1){
// blockStack.pop();
// }
isLambda = false;
}


/**
Expand Down
Loading

0 comments on commit f0f05fc

Please sign in to comment.