Skip to content

Commit

Permalink
rename annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjun committed Feb 16, 2022
1 parent 854865c commit 2709b36
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ annotationProcessor 'com.github.CodingGay.BlackReflection:compiler:1.0.0'
### Demo
#### 1. 如果你需要反射 android.app.ActivityThread 中的各种方法,参考:[ActivityThread.java](https://github.com/CodingGay/BlackReflection/blob/main/app/src/main/java/top/niunaijun/blackreflection/ref/ActivityThread.java)
```java
@BStrClass("android.app.ActivityThread")
@BClassName("android.app.ActivityThread")
public interface ActivityThread {

@BStaticMethod
Expand All @@ -42,7 +42,7 @@ public interface ActivityThread {
@BMethod
void sendActivityResult(@BParamClass(IBinder.class) IBinder token, String id, int requestCode, int resultCode, Intent data);

@BStrClass("android.app.ActivityThread$H")
@BClassName("android.app.ActivityThread$H")
interface H {
@BStaticField
int CREATE_SERVICE();
Expand All @@ -67,13 +67,13 @@ BRActivityThread是程序自动生成的类,生成规则是BR + ClassName
注解 | 注解方式 | 解释
---|---|---
@BClass | Class | 指定需要反射的类
@BStrClass | Class | 指定需要反射的类
@BClassName | Class | 指定需要反射的类
@BStaticMethod | Method | 注明是静态方法
@BMethod | Method | 注明是非静态方法
@BStaticField | Method | 注明是静态变量
@BField | Method | 注明是非静态变量
@BParamClass | Parameter | 注明该参数的Class,用于反射时寻找方法
@BStrParamClass | Parameter | 注明该参数的Class,用于反射时寻找方法
@BParamClassName | Parameter | 注明该参数的Class,用于反射时寻找方法

### License

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import top.niunaijun.blackreflection.annotation.BMethod;
import top.niunaijun.blackreflection.annotation.BParamClass;
import top.niunaijun.blackreflection.annotation.BStaticField;
import top.niunaijun.blackreflection.annotation.BStrClass;
import top.niunaijun.blackreflection.annotation.BClassName;
import top.niunaijun.blackreflection.annotation.BStaticMethod;

/**
Expand All @@ -17,7 +17,7 @@
* しーJ
* 此处无Bug
*/
@BStrClass("android.app.ActivityThread")
@BClassName("android.app.ActivityThread")
public interface ActivityThread {

@BStaticMethod
Expand All @@ -32,7 +32,7 @@ public interface ActivityThread {
@BMethod
void sendActivityResult(@BParamClass(IBinder.class) IBinder token, String id, int requestCode, int resultCode, Intent data);

@BStrClass("android.app.ActivityThread$H")
@BClassName("android.app.ActivityThread$H")
interface H {
@BStaticField
int CREATE_SERVICE();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import top.niunaijun.blackreflection.annotation.BMethod;
import top.niunaijun.blackreflection.annotation.BStaticField;
import top.niunaijun.blackreflection.annotation.BStaticMethod;
import top.niunaijun.blackreflection.annotation.BStrClass;
import top.niunaijun.blackreflection.annotation.BClassName;
import top.niunaijun.blackreflection.proxy.BlackReflectionInterfaceProxy;
import top.niunaijun.blackreflection.proxy.BlackReflectionProxy;

Expand Down Expand Up @@ -67,7 +67,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
public Set<String> getSupportedAnnotationTypes() {
HashSet<String> supportTypes = new LinkedHashSet<>();
supportTypes.add(BClass.class.getCanonicalName());
supportTypes.add(BStrClass.class.getCanonicalName());
supportTypes.add(BClassName.class.getCanonicalName());

supportTypes.add(BField.class.getCanonicalName());
supportTypes.add(BStaticField.class.getCanonicalName());
Expand All @@ -88,8 +88,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
mBlackReflectionInterfaceProxies.clear();
mRealMaps.clear();

for (Element element : roundEnv.getElementsAnnotatedWith(BStrClass.class)) {
BStrClass annotation = element.getAnnotation(BStrClass.class);
for (Element element : roundEnv.getElementsAnnotatedWith(BClassName.class)) {
BClassName annotation = element.getAnnotation(BClassName.class);
doProcess(element, annotation.value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import top.niunaijun.blackreflection.annotation.BFieldNotProcess;
import top.niunaijun.blackreflection.annotation.BFieldSetNotProcess;
import top.niunaijun.blackreflection.annotation.BParamClass;
import top.niunaijun.blackreflection.annotation.BStrClassNotProcess;
import top.niunaijun.blackreflection.annotation.BStrParamClass;
import top.niunaijun.blackreflection.utils.ClassUtils;
import top.niunaijun.blackreflection.annotation.BClassNameNotProcess;
import top.niunaijun.blackreflection.annotation.BParamClassName;

/**
* Created by sunwanquan on 2020/1/8.
Expand Down Expand Up @@ -54,7 +53,7 @@ public JavaFile generateInterfaceCode() {
String finalClass = mClassName
.replace(mPackageName + ".", "")
.replace(".", "");
AnnotationSpec annotationSpec = AnnotationSpec.builder(BStrClassNotProcess.class)
AnnotationSpec annotationSpec = AnnotationSpec.builder(BClassNameNotProcess.class)
.addMember("value","$S", realMaps.get(mOrigClassName))
.build();

Expand All @@ -69,8 +68,8 @@ public JavaFile generateInterfaceCode() {

for (VariableElement typeParameter : reflection.getExecutableElement().getParameters()) {
ParameterSpec.Builder builder = ParameterSpec.builder(ClassName.get(typeParameter.asType()), typeParameter.getSimpleName().toString());
if (typeParameter.getAnnotation(BStrParamClass.class) != null) {
BStrParamClass annotation = typeParameter.getAnnotation(BStrParamClass.class);
if (typeParameter.getAnnotation(BParamClassName.class) != null) {
BParamClassName annotation = typeParameter.getAnnotation(BParamClassName.class);
builder.addAnnotation(AnnotationSpec.get(annotation));
}
if (typeParameter.getAnnotation(BParamClass.class) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import top.niunaijun.blackreflection.annotation.BFieldNotProcess;
import top.niunaijun.blackreflection.annotation.BFieldSetNotProcess;
import top.niunaijun.blackreflection.annotation.BParamClass;
import top.niunaijun.blackreflection.annotation.BStrClass;
import top.niunaijun.blackreflection.annotation.BStrClassNotProcess;
import top.niunaijun.blackreflection.annotation.BStrParamClass;
import top.niunaijun.blackreflection.annotation.BClassName;
import top.niunaijun.blackreflection.annotation.BClassNameNotProcess;
import top.niunaijun.blackreflection.annotation.BParamClassName;
import top.niunaijun.blackreflection.utils.Reflector;

/**
Expand Down Expand Up @@ -150,9 +150,9 @@ private static Class<?>[] getParamClass(Method method) throws Throwable {
Annotation[] parameterAnnotation = parameterAnnotations[i];
boolean found = false;
for (Annotation annotation : parameterAnnotation) {
if (annotation instanceof BStrParamClass) {
if (annotation instanceof BParamClassName) {
found = true;
param[i] = Class.forName(((BStrParamClass) annotation).value());
param[i] = Class.forName(((BParamClassName) annotation).value());
break;
} else if (annotation instanceof BParamClass) {
found = true;
Expand All @@ -170,18 +170,18 @@ private static Class<?>[] getParamClass(Method method) throws Throwable {

private static Class<?> getClassNameByBlackClass(Class<?> clazz) throws ClassNotFoundException {
BClass bClass = clazz.getAnnotation(BClass.class);
BStrClass bStrClass = clazz.getAnnotation(BStrClass.class);
BStrClassNotProcess bStrClassNotProcess = clazz.getAnnotation(BStrClassNotProcess.class);
if (bClass == null && bStrClass == null && bStrClassNotProcess == null) {
BClassName bClassName = clazz.getAnnotation(BClassName.class);
BClassNameNotProcess bClassNameNotProcess = clazz.getAnnotation(BClassNameNotProcess.class);
if (bClass == null && bClassName == null && bClassNameNotProcess == null) {
throw new RuntimeException("Not found @BlackClass or @BlackStrClass");
}

if (bClass != null) {
return bClass.value();
} else if (bStrClass != null) {
return Class.forName(bStrClass.value());
} else if (bClassName != null) {
return Class.forName(bClassName.value());
} else {
return Class.forName(bStrClassNotProcess.value());
return Class.forName(bClassNameNotProcess.value());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*/
@Retention(RUNTIME)
@Target({TYPE})
public @interface BStrClass {
public @interface BClassName {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*/
@Retention(RUNTIME)
@Target({TYPE})
public @interface BStrClassNotProcess {
public @interface BClassNameNotProcess {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*/
@Retention(RUNTIME)
@Target({PARAMETER})
public @interface BStrParamClass {
public @interface BParamClassName {
String value();
}

0 comments on commit 2709b36

Please sign in to comment.