Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion JFunc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ dependencies {
compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.5.4"
compile 'org.ow2.asm:asm-all:5.1'
compile 'com.github.javaparser:javaparser-core:2.3.0'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.12'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.12'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.7.12'
compile group: 'commons-io', name: 'commons-io', version:'2.5'


testCompile group: 'junit', name: 'junit', version: '4.+'
Expand Down
14 changes: 14 additions & 0 deletions JFunc/src/main/java/com/jfunc/asm/AppMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jfunc.asm;

import java.util.ArrayList;
import java.util.List;

public class AppMetadata {

private final List<ClassMetadata> classMetaData = new ArrayList<ClassMetadata>();

public List<ClassMetadata> getClassMetaData() {
return classMetaData;
}

}
46 changes: 46 additions & 0 deletions JFunc/src/main/java/com/jfunc/asm/ClassMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.jfunc.asm;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

public class ClassMetadata {

private final String className;

private final ClassNode classNode;

private final List<MethodMetaData> methodMetadataList = new ArrayList<MethodMetaData>();

private final List<FieldNode> fieldNodes = new ArrayList<FieldNode>();

public ClassMetadata(ClassNode classNode) {
this.className = classNode.sourceFile;
this.classNode = classNode;
List<MethodNode> methodnodes = classNode.methods;
for (MethodNode methodNode : methodnodes) {
this.getMethodMetadataList().add(new MethodMetaData(methodNode, classNode));
}
this.fieldNodes.addAll(classNode.fields);
}

public String getClassName() {
return className;
}

public ClassNode getClassNode() {
return classNode;
}

public List<MethodMetaData> getMethodMetadataList() {
return methodMetadataList;
}

public List<FieldNode> getFieldNodes() {
return fieldNodes;
}

}
165 changes: 86 additions & 79 deletions JFunc/src/main/java/com/jfunc/asm/MethodMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.LineNumberNode;
Expand All @@ -21,83 +22,89 @@

public class MethodMetaData {

private static Printer printer = new Textifier();
private static TraceMethodVisitor mp = new TraceMethodVisitor(printer);

private final MethodNode methodNode;
private final List<String> byteCode;
private final String returnType;
private final Map<LineNumberNode, List<AbstractInsnNode>> lineToOperationsMap;
private final List<InternalMethod> internallyCalledMethods = new ArrayList<>();
private final List<InternalFeild> internallyRefferedFields = new ArrayList<>();

public MethodMetaData(MethodNode methodNode) {
this.methodNode = methodNode;
this.byteCode = generateByteCodeOfMethod(methodNode);
this.returnType = Type.getReturnType(methodNode.desc).getClassName();
this.lineToOperationsMap = getLineToOperationsMap(methodNode.instructions);
}

public String getMethodNode() {
return this.methodNode.name;
}

public String getMethodReturnType() {
return this.returnType;
}

public List<String> getByteCode() {
return Collections.unmodifiableList(this.byteCode);
}

public Map<LineNumberNode, List<AbstractInsnNode>> getLineToOperationsMap() {
return Collections.unmodifiableMap(this.lineToOperationsMap);
}

public List<InternalMethod> getInternallyCalledMethods() {
return Collections.unmodifiableList(internallyCalledMethods);
}

public List<InternalFeild> getInternallyRefferedFields() {
return Collections.unmodifiableList(internallyRefferedFields);
}

private List<String> generateByteCodeOfMethod(MethodNode methodNode) {
List<String> byteCodeList = new ArrayList<>();
InsnList inList = methodNode.instructions;
for (int i = 0; i < inList.size(); i++) {
byteCodeList.add(insnToString(inList.get(i)));
}
return byteCodeList;
}

private String insnToString(AbstractInsnNode insn) {
insn.accept(mp);
StringWriter sw = new StringWriter();
printer.print(new PrintWriter(sw));
printer.getText().clear();
return sw.toString();
}

private Map<LineNumberNode, List<AbstractInsnNode>> getLineToOperationsMap(InsnList insList) {
Map<LineNumberNode, List<AbstractInsnNode>> lineToOperationsMap = new LinkedHashMap<>();
LineNumberNode key = null;
for (int i = 0; i < insList.size(); i++) {
if (insList.get(i) instanceof LineNumberNode) {
key = (LineNumberNode) insList.get(i);
lineToOperationsMap.put(key, new ArrayList<AbstractInsnNode>());
} else {
if (key != null && lineToOperationsMap.containsKey(key)) {
lineToOperationsMap.get(key).add(insList.get(i));
if (insList.get(i) instanceof FieldInsnNode) {
internallyRefferedFields.add(new InternalFeild((FieldInsnNode) insList.get(i), key));
}
if (insList.get(i) instanceof MethodInsnNode) {
internallyCalledMethods.add(new InternalMethod((MethodInsnNode) insList.get(i), key));
}
}
}
}
return lineToOperationsMap;
}
private static Printer printer = new Textifier();
private static TraceMethodVisitor mp = new TraceMethodVisitor(printer);

private final String className;
private final MethodNode methodNode;
private final List<String> byteCode;
private final String returnType;
private final Map<LineNumberNode, List<AbstractInsnNode>> lineToOperationsMap;
private final List<InternalMethod> internallyCalledMethods = new ArrayList<>();
private final List<InternalFeild> internallyRefferedFields = new ArrayList<>();

public MethodMetaData(MethodNode methodNode, ClassNode classNode) {
this.className = classNode.name;
this.methodNode = methodNode;
this.byteCode = generateByteCodeOfMethod(methodNode);
this.returnType = Type.getReturnType(methodNode.desc).getClassName();
this.lineToOperationsMap = getLineToOperationsMap(methodNode.instructions);
}

public String getClassName() {
return className;
}

public MethodNode getMethodNode() {
return this.methodNode;
}

public String getMethodReturnType() {
return this.returnType;
}

public List<String> getByteCode() {
return Collections.unmodifiableList(this.byteCode);
}

public Map<LineNumberNode, List<AbstractInsnNode>> getLineToOperationsMap() {
return Collections.unmodifiableMap(this.lineToOperationsMap);
}

public List<InternalMethod> getInternallyCalledMethods() {
return Collections.unmodifiableList(internallyCalledMethods);
}

public List<InternalFeild> getInternallyRefferedFields() {
return Collections.unmodifiableList(internallyRefferedFields);
}

private List<String> generateByteCodeOfMethod(MethodNode methodNode) {
List<String> byteCodeList = new ArrayList<>();
InsnList inList = methodNode.instructions;
for (int i = 0; i < inList.size(); i++) {
byteCodeList.add(insnToString(inList.get(i)));
}
return byteCodeList;
}

private String insnToString(AbstractInsnNode insn) {
insn.accept(mp);
StringWriter sw = new StringWriter();
printer.print(new PrintWriter(sw));
printer.getText().clear();
return sw.toString();
}

private Map<LineNumberNode, List<AbstractInsnNode>> getLineToOperationsMap(InsnList insList) {
Map<LineNumberNode, List<AbstractInsnNode>> lineToOperationsMap = new LinkedHashMap<>();
LineNumberNode key = null;
for (int i = 0; i < insList.size(); i++) {
if (insList.get(i) instanceof LineNumberNode) {
key = (LineNumberNode) insList.get(i);
lineToOperationsMap.put(key, new ArrayList<AbstractInsnNode>());
} else {
if (key != null && lineToOperationsMap.containsKey(key)) {
lineToOperationsMap.get(key).add(insList.get(i));
if (insList.get(i) instanceof FieldInsnNode) {
internallyRefferedFields.add(new InternalFeild((FieldInsnNode) insList.get(i), key));
}
if (insList.get(i) instanceof MethodInsnNode) {
internallyCalledMethods.add(new InternalMethod((MethodInsnNode) insList.get(i), key));
}
}
}
}
return lineToOperationsMap;
}
}
8 changes: 7 additions & 1 deletion JFunc/src/main/java/com/jfunc/util/FieldUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ public static ObjectNode getReasonsForLogStatements(InternalFeild internalField)
resonsNode.put(JfuncConstants.LINENUMBER + internalField.getLineNumber(), JfuncConstants.LOGGER);
return resonsNode;
}


public static ObjectNode getReasonsForGlobalFields(InternalFeild internalField) {
ObjectMapper mapper = JsonUtils.getObjectMapper();
ObjectNode resonsNode = mapper.createObjectNode();
resonsNode.put(JfuncConstants.LINENUMBER + internalField.getLineNumber(), JfuncConstants.LOGGER);
return resonsNode;
}
}
Loading