Skip to content

Commit 246d5f8

Browse files
author
monsoon
committed
add classHelper and annotation support
1 parent d363557 commit 246d5f8

File tree

11 files changed

+318
-9
lines changed

11 files changed

+318
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ target
99
.metadata/
1010
.classpath
1111
.project
12-
Servers/
12+
Servers/
13+
src/main/resources/
14+
src/test/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.xfj.frameworktest;
2+
3+
import org.junit.Test;
4+
import simple.xfj.framework.annotation.Controller;
5+
import simple.xfj.framework.constant.ConfigConstant;
6+
import simple.xfj.framework.util.ClassUtil;
7+
import simple.xfj.framework.util.PropsUtil;
8+
9+
import java.util.Properties;
10+
11+
/**
12+
* Created by asus on 2017/4/17.
13+
*/
14+
@Controller
15+
public class PropertiesLoad {
16+
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.xfj.frameworktest.util;
2+
3+
/**
4+
* Created by asus on 2017/4/18.
5+
*/
6+
public class world {
7+
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
1+
import org.junit.Before;
12
import org.junit.Test;
23
import simple.xfj.framework.constant.ConfigConstant;
4+
import simple.xfj.framework.helper.ClassHelper;
5+
import simple.xfj.framework.util.ClassUtil;
36
import simple.xfj.framework.util.PropsUtil;
47

8+
import java.io.IOException;
59
import java.util.Properties;
10+
import java.util.Set;
611

712
/**
813
* Created by asus on 2017/4/17.
914
*/
1015
public class PropertiesLoadTest {
1116

17+
private Properties properties;
18+
19+
@Before
20+
public void before()throws Exception{
21+
properties = PropsUtil.loadProperties(ConfigConstant.CONFIG_FILENAME);
22+
}
23+
1224
@Test
1325
public void testConfig() throws Exception{
14-
Properties properties = PropsUtil.loadProperties(ConfigConstant.CONFIG_FILENAME);
1526
System.out.println(properties.getProperty(ConfigConstant.JDBC_DRIVER));
27+
}
1628

29+
@Test
30+
public void testGetClassSet() throws IOException {
31+
Set<Class<?>> clazzSet = ClassUtil.getClassSet(properties.getProperty(ConfigConstant.APP_BASE_PACKAGE));
32+
for(Class cls : clazzSet){
33+
System.out.println(cls.getPackage());
34+
System.out.println(cls.getName());
35+
}
36+
}
37+
38+
@Test
39+
public void testGetControllerBean(){
40+
Set<Class<?>> controllerSet = ClassHelper.getControllerSet();
41+
for(Class cls : controllerSet){
42+
System.out.println(cls.getPackage());
43+
System.out.println(cls.getName());
44+
}
1745
}
1846

1947
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package simple.xfj.framework.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by asus on 2017/4/18.
10+
*/
11+
@Target(ElementType.METHOD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface Action {
14+
String value();
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package simple.xfj.framework.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by asus on 2017/4/18.
10+
*/
11+
@Target(ElementType.FIELD)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface Autowired {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package simple.xfj.framework.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by asus on 2017/4/18.
10+
*/
11+
@Target(ElementType.TYPE) //标明该注解用在类上
12+
@Retention(RetentionPolicy.RUNTIME) //注解作用范围运行时
13+
public @interface Controller {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package simple.xfj.framework.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by asus on 2017/4/18.
10+
*/
11+
@Target(ElementType.TYPE) //标明该注解用在类上
12+
@Retention(RetentionPolicy.RUNTIME) //注解作用范围运行时
13+
public @interface Service {
14+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package simple.xfj.framework.helper;
2+
3+
/**
4+
* Created by asus on 2017/4/18.
5+
*/
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import simple.xfj.framework.annotation.Controller;
10+
import simple.xfj.framework.annotation.Service;
11+
import simple.xfj.framework.util.ClassUtil;
12+
13+
import java.util.HashSet;
14+
import java.util.Set;
15+
16+
/**
17+
* 获取指定包名下的各种注解类的helper类
18+
*/
19+
public class ClassHelper {
20+
21+
private static Logger LOGGER = LoggerFactory.getLogger(ClassHelper.class);
22+
private static Set<Class<?>> CLAZZ_SET;
23+
24+
static {
25+
try{
26+
CLAZZ_SET = ClassUtil.getClassSet(ConfigHelper.getAPPBasePackage());
27+
}catch (Exception e){
28+
LOGGER.error("inited class set failure");
29+
throw new RuntimeException(e);
30+
}
31+
}
32+
33+
public static Set<Class<?>> getClazzSet() {
34+
return CLAZZ_SET;
35+
}
36+
37+
/**
38+
* 获得所有带有@controller注解的类
39+
* @return
40+
*/
41+
public static Set<Class<?>> getControllerSet(){
42+
Set<Class<?>> controllerSet = new HashSet<Class<?>>();
43+
if(null != CLAZZ_SET && CLAZZ_SET.size() > 0){
44+
for(Class clazz:CLAZZ_SET){
45+
if(clazz.isAnnotationPresent(Controller.class)){
46+
controllerSet.add(clazz);
47+
}
48+
}
49+
}
50+
return controllerSet;
51+
}
52+
53+
/**
54+
* 获得所有带有@Service注解的类
55+
* @return
56+
*/
57+
public static Set<Class<?>> getServiceSet(){
58+
Set<Class<?>> serviceSet = new HashSet<Class<?>>();
59+
if(null != CLAZZ_SET && CLAZZ_SET.size() > 0){
60+
for(Class clazz:CLAZZ_SET){
61+
if(clazz.isAnnotationPresent(Service.class)){
62+
serviceSet.add(clazz);
63+
}
64+
}
65+
}
66+
return serviceSet;
67+
}
68+
69+
/*
70+
获得所有bean实列
71+
*/
72+
public static Set<Class<?>> getBeanSet(){
73+
Set<Class<?>> beanSet = new HashSet<Class<?>>();
74+
beanSet.addAll(getServiceSet());
75+
beanSet.addAll(getControllerSet());
76+
return beanSet;
77+
}
78+
79+
}

src/main/java/simple/xfj/framework/util/ClassUtil.java

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
import java.io.File;
7+
import java.io.FileFilter;
8+
import java.io.IOException;
9+
import java.net.JarURLConnection;
10+
import java.net.URL;
11+
import java.util.Enumeration;
12+
import java.util.HashSet;
613
import java.util.Set;
14+
import java.util.jar.JarEntry;
15+
import java.util.jar.JarFile;
716

817
/**
918
* Created by asus on 2017/4/18.
@@ -17,7 +26,7 @@ public class ClassUtil {
1726
* @return
1827
*/
1928
public static ClassLoader getClassLoader(){
20-
return ClassUtil.getClassLoader();
29+
return Thread.currentThread().getContextClassLoader();
2130
}
2231

2332
/**
@@ -42,14 +51,70 @@ public static Class<?> loadClass(String className,boolean isInitialized){
4251
* @param packageName
4352
* @return
4453
*/
45-
public static Set<Class<?>> getClassSet(String packageName){
46-
47-
return null;
54+
public static Set<Class<?>> getClassSet(String packageName) throws IOException {
55+
Set<Class<?>> classSet = new HashSet<Class<?>>();
56+
Enumeration<URL> urls = getClassLoader().getResources(packageName.replace(".", "/"));
57+
while (urls.hasMoreElements()){
58+
URL url = urls.nextElement();
59+
if(null != url){
60+
String protocol = url.getProtocol();
61+
if(protocol.equals("file")){
62+
String packagePath = url.getPath().replace("%20", " ");
63+
addClass(classSet,packagePath,packageName);
64+
}else if(protocol.equals("jar")){
65+
JarURLConnection connection = (JarURLConnection)url.openConnection();
66+
url.openConnection();
67+
if(connection != null){
68+
JarFile jarFile = connection.getJarFile();
69+
if(jarFile != null){
70+
Enumeration<JarEntry> entries = jarFile.entries();
71+
while (entries.hasMoreElements()){
72+
JarEntry entry = entries.nextElement();
73+
String name = entry.getName();
74+
if(name.endsWith(".class")){
75+
String className = name.substring(0,name.lastIndexOf(".")).replaceAll("/",".");
76+
doAddClass(classSet,className);
77+
System.out.println(className);
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
return classSet;
4886
}
4987

88+
//加载路径下的所有class文件
89+
private static void addClass(Set<Class<?>> set,String packagePath,String packageName){
90+
File file = new File(packagePath);
91+
File[] files = file.listFiles(new FileFilter(){
92+
public boolean accept(File f) {
93+
return f.isFile() && f.getName().endsWith(".class") || f.isDirectory();
94+
}});
95+
for(File f: files){
96+
String fileName = f.getName();
97+
if(f.isFile()){
98+
String className = fileName.substring(0,fileName.lastIndexOf("."));
99+
if(packageName != null && packageName.length() >0){
100+
className = packageName + "." + className;
101+
}
102+
doAddClass(set,className);
103+
}else {
104+
//如果存在子文件夹,则递归读取子文件夹下的所有class文件,并加载
105+
String subPackageName = fileName;
106+
if(packageName != null && packageName.length() >0){
107+
packageName = packageName + "." + subPackageName;
108+
packagePath = packagePath + "/" + subPackageName;
109+
}
110+
addClass(set,packagePath,packageName);
111+
}
112+
}
113+
}
50114

51-
52-
53-
115+
private static void doAddClass(Set<Class<?>> set,String clazz){
116+
Class<?> cls = loadClass(clazz, false);
117+
set.add(cls);
118+
}
54119

55120
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package simple.xfj.framework.util;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.lang.reflect.Field;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
10+
/**
11+
* Created by asus on 2017/4/18.
12+
*/
13+
public class ReflectionUtil {
14+
15+
16+
private static Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);
17+
18+
public static Object newInstance(Class<?> clazz){
19+
Object obj = null;
20+
try {
21+
obj = clazz.newInstance();
22+
} catch (Exception e) {
23+
LOGGER.error("can new " + clazz.getName());
24+
throw new RuntimeException(e);
25+
}
26+
return obj;
27+
}
28+
29+
public static Object methodInvoke(Method method,Object obj,Object... args){
30+
Object result = null;
31+
if(!method.isAccessible()){
32+
method.setAccessible(true);
33+
try {
34+
result = method.invoke(obj,args);
35+
} catch (Exception e) {
36+
LOGGER.error("can not invoke method:" + method.getName());
37+
throw new RuntimeException("invoke method failure:" + method.getName());
38+
}
39+
}
40+
return result;
41+
}
42+
43+
public static void setField(Object obj, Field field, Object value){
44+
try {
45+
field.setAccessible(true);
46+
field.set(obj,value);
47+
} catch (IllegalAccessException e) {
48+
LOGGER.error("can not set filed:" + field.getName());
49+
throw new RuntimeException("set filed failure:" + field.getName());
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)