-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
408 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
## [反射](src/main/java/com/cpucode/java/reflect) | ||
|
||
- [x] [Get_class_object__获取class对象](src/main/java/com/cpucode/java/reflect/Get_class_object.java) | ||
- [x] [Get_class_methods__获取class方法](src/main/java/com/cpucode/java/reflect/Get_class_methods.java) | ||
- [x] [Get_structure__获取class构造方法](src/main/java/com/cpucode/java/reflect/Get_structure.java) | ||
- [x] [Get_methods__获取成员方法](src/main/java/com/cpucode/java/reflect/Get_methods.java) | ||
- [x] [Reflect_test__反射测试框架](src/main/java/com/cpucode/java/reflect/Reflect_test.java) | ||
|
||
- [返回目录](#文件目录) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.cpucode</groupId> | ||
<artifactId>reflect</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<!-- others start --> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.6</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjweaver</artifactId> | ||
<version>1.9.6</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>5.3.8</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<!-- java编译插件 --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.2</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="Spring" name="Spring"> | ||
<configuration /> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" /> | ||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.6" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.8" level="project" /> | ||
</component> | ||
</module> |
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
reflect/src/main/java/com/cpucode/java/aop/aspect/AnnotaionAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.cpucode.java.aop.aspect; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Annotation版Aspect切面Bean | ||
* | ||
* @Component 声明这是一个组件 | ||
* @Aspect 声明这是一个切面Bean,AnnotaionAspect是一个面,由框架实现的 | ||
* | ||
* @author : cpucode | ||
* @date : 2021/7/22 | ||
* @time : 16:23 | ||
* @github : https://github.com/CPU-Code | ||
* @csdn : https://blog.csdn.net/qq_44226094 | ||
*/ | ||
@Component | ||
@Aspect | ||
public class AnnotaionAspect { | ||
|
||
private final static Logger log = Logger.getLogger(AnnotaionAspect.class); | ||
|
||
/** | ||
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点 | ||
* 切点的集合,这个表达式所描述的是一个虚拟面(规则) | ||
* 就是为了Annotation扫描时能够拿到注解中的内容 | ||
*/ | ||
@Pointcut("execution(* com.cpucode.java.aop.aspect..*(..))") | ||
public void aspect(){} | ||
|
||
/** | ||
* 配置前置通知,使用在方法aspect()上注册的切入点 | ||
* 同时接受JoinPoint切入点对象,可以没有该参数 | ||
* @param joinPoint | ||
*/ | ||
@Before("aspect()") | ||
public void before(JoinPoint joinPoint){ | ||
log.info("before " + joinPoint); | ||
} | ||
|
||
/** | ||
* 配置后置通知,使用在方法aspect()上注册的切入点 | ||
* @param joinPoint | ||
*/ | ||
@After("aspect()") | ||
public void after(JoinPoint joinPoint){ | ||
log.info("after " + joinPoint); | ||
} | ||
|
||
/** | ||
* 配置环绕通知,使用在方法aspect()上注册的切入点 | ||
* @param joinPoint | ||
*/ | ||
@Around("aspect()") | ||
public void around(JoinPoint joinPoint){ | ||
long start = System.currentTimeMillis(); | ||
|
||
try { | ||
((ProceedingJoinPoint) joinPoint).proceed(); | ||
long end = System.currentTimeMillis(); | ||
|
||
log.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!"); | ||
} catch (Throwable e) { | ||
long end = System.currentTimeMillis(); | ||
|
||
log.info("around " + joinPoint + | ||
"\tUse time : " + (end - start) + | ||
" ms with exception : " + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 配置后置返回通知,使用在方法aspect()上注册的切入点 | ||
* @param joinPoint | ||
*/ | ||
@AfterReturning("aspect()") | ||
public void afterReturn(JoinPoint joinPoint){ | ||
log.info("afterReturn " + joinPoint); | ||
} | ||
|
||
/** | ||
* 配置抛出异常后通知,使用在方法aspect()上注册的切入点 | ||
* @param joinPoint | ||
* @param ex | ||
*/ | ||
@AfterThrowing(pointcut="aspect()", throwing="ex") | ||
public void afterThrow(JoinPoint joinPoint, Exception ex){ | ||
log.info("afterThrow " + joinPoint + "\t" + ex.getMessage()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
reflect/src/main/java/com/cpucode/java/aop/aspect/ArgsAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.cpucode.java.aop.aspect; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 获取参数Aspect切面Bean | ||
* @Component 声明这是一个组件 | ||
* @Aspect 声明这是一个切面Bean | ||
* @author : cpucode | ||
* @date : 2021/7/22 | ||
* @time : 16:16 | ||
* @github : https://github.com/CPU-Code | ||
* @csdn : https://blog.csdn.net/qq_44226094 | ||
*/ | ||
@Component | ||
@Aspect | ||
public class ArgsAspect { | ||
private final static Logger log = Logger.getLogger(ArgsAspect.class); | ||
|
||
/** | ||
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点 | ||
*/ | ||
@Pointcut("execution(* com.cpucode.java.aop.service..*(..))") | ||
public void aspect(){ | ||
System.out.println("aspect() @Pointcut"); | ||
} | ||
|
||
/** | ||
* 配置前置通知,拦截返回值为 com.cpucode.java.aop.model.Member 的方法 | ||
* @param joinPoint | ||
*/ | ||
@Before("execution(com.cpucode.java.aop.model.Member com.cpucode.java.aop.service..*(..))") | ||
public void beforeReturnUser(JoinPoint joinPoint){ | ||
log.info("beforeReturnUser " + joinPoint); | ||
} | ||
|
||
/** | ||
* 配置前置通知,拦截参数为 com.cpucode.java.aop.model.Member 的方法 | ||
* @param joinPoint | ||
*/ | ||
@Before("execution(* com.cpucode.java.aop.service..*(com.cpucode.java.aop.model.Member))") | ||
public void beforeArgUser(JoinPoint joinPoint){ | ||
log.info("beforeArgUser " + joinPoint); | ||
} | ||
|
||
/** | ||
* 配置前置通知,拦截含有long类型参数的方法,并将参数值注入到当前方法的形参id中 | ||
* @param joinPoint | ||
* @param id | ||
*/ | ||
@Before("aspect()&&args(id)") | ||
public void beforeArgId(JoinPoint joinPoint, long id){ | ||
log.info("beforeArgId " + joinPoint + "\tID:" + id); | ||
} | ||
} |
Oops, something went wrong.