Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrninistrator authored and Adrninistrator committed Aug 28, 2022
1 parent 1c3184c commit dc6ac48
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 164 deletions.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ b. 对于指定目录中的后缀非.jar/.war的文件进行合并

对于某个类调用自身类的类调用关系,也会生成在输出文件中

## 2.7. (0.2.0)

在处理注解的属性值时,支持使用自定义类处理,自定义类需要实现`com.adrninistrator.javacg.extensions.annotation_attributes.AnnotationAttributesFormatorInterface`接口

可使用java-all-call-graph中的`com.adrninistrator.jacg.extensions.annotation_attributes.AllAnnotationAttributesFormator`

# 3. 使用说明

## 3.1. 编译命令:
Expand Down Expand Up @@ -233,30 +239,30 @@ type class_or_method_name annotation_name annotation_attribute_name annotation_a

- annotation_attribute_value

注解属性的值,可能为空

注解属性值中可能会出现影响文件格式的字符,会进行以下替换处理:
注解属性值,与AnnotationAttributesFormatorInterface实现类的处理方式有关,可能为空

|替换前的字符|替换后的字符|
|---|---|
|空格|0x01|
|\r|0x02|
|\n|0x03|
若注解没有属性值,则只有以上前三个字段,占一行

假如属性值类型为数组,则属性值会被大括号包含,如“{test}”
若注解有属性值,则有以上五个字段,每个属性占一行

若注解没有属性值,则只有以上前三个字段,占一行
使用java-all-call-graph中的AllAnnotationAttributesFormator类时,注解属性值前缀的含义如下:

若注解有属性值,则有以上五个字段(若注解属性值为""则是四个字段),每个属性占一行
|注解属性值前缀|含义|属性值的保存格式|
|---|---|---|
|s:|字符串类型,对应简单类型、类、枚举类型|属性值为字符串|
|b:|字符串类型,对应简单类型、类、枚举类型,进行BASE64编码|属性值为字符串BASE64编码后的结果|
|m:|Map类型,对应注解类型|属性值为JSON字符串|
|l:|List类型,对应数组类型|属性值为JSON字符串|

文件示例如下:

```
C: com.test.controller.TestLoaderController org.springframework.stereotype.Controller
C: com.test.controller.TestLoaderController org.springframework.web.bind.annotation.RequestMapping value {test}
M: com.test.controller.TestRest2Controller:get(javax.servlet.http.HttpServletRequest) org.springframework.web.bind.annotation.GetMapping value {get}
M: com.test.controller.TestRest2Controller:get(javax.servlet.http.HttpServletRequest) com.test.common.annotation.TestAttributeAnnotation value abc
M: com.test.controller.TestRest2Controller:get(javax.servlet.http.HttpServletRequest) com.test.common.annotation.TestAttributeAnnotation value2 123
C: test.call_graph.annotation.MethodWithAnnotation test.call_graph.annotation.TestAnnotation strValue b:YWFhDQo=
C: test.call_graph.annotation.MethodWithAnnotation test.call_graph.annotation.TestAnnotation intValue s:111
C: test.call_graph.annotation.MethodWithAnnotation test.call_graph.annotation.TestAnnotation intArrayValue l:["1","2","3","4"]
C: test.call_graph.annotation.MethodWithAnnotation test.call_graph.annotation.TestAnnotation annotation1 m:{"valueB":"Cvb1","valueA":"Cva1"}
M: test.call_graph.annotation.MethodWithAnnotation:test2() test.call_graph.annotation.TestAnnotationOuter annotations l:[{"valueB":"vb1\r\n","valueA":"va1"},{"valueB":"va2","valueA":"va2"}]
M: test.call_graph.annotation.MethodWithAnnotation:test3() test.call_graph.annotation.TestAnnotationOuter2 annotations l:[{"value":"aaa","annotations":[{"valueB":"va1","valueA":"va1"},{"valueB":"va2\r\n","valueA":"va2"}]},{"value":"bbb","annotations":[{"valueB":"vb1","valueA":"vb1"},{"valueB":"vb2","valueA":"vb2"}]}]
```

## 4.3. 方法代码行号信息文件
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'maven-publish'
apply plugin: 'signing'

group 'com.github.adrninistrator'
version = "0.1.9"
version = "0.2.0"
def projectName = "java-callgraph2"
description = "${projectName}"
sourceCompatibility = 1.8
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.adrninistrator.javacg.common;

import java.util.concurrent.Callable;

/**
* @author adrninistrator
* @date 2022/5/14
* @description:
*/
public class ClassNameConstants {

public static final String CLASS_NAME_RUNNABLE = Runnable.class.getName();
public static final String CLASS_NAME_CALLABLE = Callable.class.getName();
public static final String CLASS_NAME_THREAD = Thread.class.getName();
public static final String CLASS_NAME_OBJECT = Object.class.getName();
public static final String CLASS_NAME_STRING = String.class.getName();
public static final String CLASS_NAME_CLASS = Class.class.getName();
public static final String CLASS_NAME_NULL_POINTER_EXCEPTION = NullPointerException.class.getName();

private ClassNameConstants() {
throw new IllegalStateException("illegal");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class JavaCGConstants {
// 合并jar/war包中的class文件时,需要合并的包名
public static final String PROPERTY_MERGE_CLASS_IN_JAR_PACKAGE = "merge.class.in.jar.package";

// 文件列的分隔符
public static final String FILE_COLUMN_SEPARATOR = "\t";

public static final String FILE_KEY_CLASS_PREFIX = "C:";
public static final String FILE_KEY_METHOD_PREFIX = "M:";
public static final String FILE_KEY_JAR_INFO_PREFIX = "J:";
Expand All @@ -32,8 +35,6 @@ public class JavaCGConstants {

public static final String NEW_LINE = "\n";

public static final String OBJECT_CLASS_NAME = Object.class.getName();

public static final String MERGED_JAR_FLAG = "-javacg_merged.jar";

public static final String FILE_FLAG_ANNOTATION = "-annotation";
Expand All @@ -52,12 +53,10 @@ public class JavaCGConstants {
// 输出的注解信息文件,不包含属性时的列数
public static final int ANNOTATION_COLUMN_NUM_WITHOUT_ATTRIBUTE = 3;

// 将注解属性值写入文件时,空格替换后的字符
public static final char ANNOTATION_ATTRIBUTE_VALUE_REPLACE_BACKSPACE = 0x01;
// 将注解属性值写入文件时,\r替换后的字符
public static final char ANNOTATION_ATTRIBUTE_VALUE_REPLACE_CARRIAGE_RETURN = 0x02;
public static final char ANNOTATION_ATTRIBUTE_VALUE_REPLACE_CARRIAGE_RETURN = 0x01;
// 将注解属性值写入文件时,\n替换后的字符
public static final char ANNOTATION_ATTRIBUTE_VALUE_REPLACE_LINE_FEED = 0x03;
public static final char ANNOTATION_ATTRIBUTE_VALUE_REPLACE_LINE_FEED = 0x02;

// 输出的方法行号信息文件的列数
public static final int LINE_NUMBER_COLUMN_NUM = 3;
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/com/adrninistrator/javacg/dto/ChildrenClassInfo.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.classes;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.classes;

import com.adrninistrator.javacg.dto.method.MethodAttribute;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.counter;

/**
* @author Adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.jar;

/**
* @author adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.method;

/**
* @author adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.method;

/**
* @author adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.method;

import org.apache.bcel.generic.Type;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.method;

/**
* @author adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adrninistrator.javacg.dto;
package com.adrninistrator.javacg.dto.node;

/**
* @author adrninistrator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.adrninistrator.javacg.extensions.annotation_attributes;

import org.apache.bcel.classfile.ElementValuePair;

/**
* @author adrninistrator
* @date 2022/8/28
* @description: 对注解属性的元素值进行格式化的接口
*/
public interface AnnotationAttributesFormatorInterface {

String format(ElementValuePair elementValuePair);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.adrninistrator.javacg.extensions.annotation_attributes;

import com.adrninistrator.javacg.common.JavaCGConstants;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.ElementValuePair;
import org.apache.bcel.classfile.Utility;

import java.io.Writer;

/**
* @author adrninistrator
* @date 2022/8/28
* @description: 对注解属性值进行处理的类
*/
public class AnnotationAttributesHandler {

/**
* 将注解信息写入文件
*
* @param type 当前注解对应的元素类型,类还是方法
* @param classOrMethod 类名或方法
* @param annotationEntries
* @param writer
*/
public static void writeAnnotationInfo(String type,
String classOrMethod,
AnnotationEntry[] annotationEntries,
AnnotationAttributesFormatorInterface annotationAttributesFormator,
Writer writer) {
if (annotationEntries == null || annotationEntries.length == 0) {
return;
}

try {
StringBuilder stringBuilder = new StringBuilder();
for (AnnotationEntry annotationEntry : annotationEntries) {
String annotationClassName = Utility.typeSignatureToString(annotationEntry.getAnnotationType(), false);
// TODO 后面把空格改成\t
String data = type + " " + classOrMethod + " " + annotationClassName;

if (annotationEntry.getElementValuePairs() == null || annotationEntry.getElementValuePairs().length == 0) {
// 注解属性为空
stringBuilder.append(data).append(JavaCGConstants.NEW_LINE);
continue;
}

// 注解属性非空
for (ElementValuePair elementValuePair : annotationEntry.getElementValuePairs()) {
String formattedValue = annotationAttributesFormator.format(elementValuePair);

stringBuilder.append(data).append(" ").append(formattedValue).append(JavaCGConstants.NEW_LINE);
}
}

writer.write(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

private AnnotationAttributesHandler() {
throw new IllegalStateException("illegal");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.adrninistrator.javacg.extensions.annotation_attributes;

import com.adrninistrator.javacg.common.JavaCGConstants;
import org.apache.bcel.classfile.ElementValuePair;

/**
* @author adrninistrator
* @date 2022/8/28
* @description:
*/
public class DefaultAnnotationAttributesFormator implements AnnotationAttributesFormatorInterface {

@Override
public String format(ElementValuePair elementValuePair) {
String key = elementValuePair.getNameString();
String value = elementValuePair.getValue().toString();

return encodeAnnotationValue(key + JavaCGConstants.FILE_COLUMN_SEPARATOR + value);
}

public static String encodeAnnotationValue(String value) {
return value.replace('\r', JavaCGConstants.ANNOTATION_ATTRIBUTE_VALUE_REPLACE_CARRIAGE_RETURN)
.replace('\n', JavaCGConstants.ANNOTATION_ATTRIBUTE_VALUE_REPLACE_LINE_FEED);
}

public static String decodeAnnotationValue(String value) {
return value.replace(JavaCGConstants.ANNOTATION_ATTRIBUTE_VALUE_REPLACE_CARRIAGE_RETURN, '\r')
.replace(JavaCGConstants.ANNOTATION_ATTRIBUTE_VALUE_REPLACE_LINE_FEED, '\n');
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.adrninistrator.javacg.extensions.code_parser;

import com.adrninistrator.javacg.dto.CallIdCounter;
import com.adrninistrator.javacg.dto.ClassInterfaceMethodInfo;
import com.adrninistrator.javacg.dto.ExtendsClassMethodInfo;
import com.adrninistrator.javacg.dto.MethodCallDto;
import com.adrninistrator.javacg.dto.counter.CallIdCounter;
import com.adrninistrator.javacg.dto.classes.ClassInterfaceMethodInfo;
import com.adrninistrator.javacg.dto.classes.ExtendsClassMethodInfo;
import com.adrninistrator.javacg.dto.method.MethodCallDto;
import com.adrninistrator.javacg.extensions.dto.ExtendedData;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.InstructionHandle;
Expand Down
Loading

0 comments on commit dc6ac48

Please sign in to comment.