Skip to content

Commit ea9d354

Browse files
committed
some files
1 parent 21071ef commit ea9d354

29 files changed

Lines changed: 1184 additions & 2 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javacore.annotation;
2+
3+
/**
4+
* link : http://www.importnew.com/10294.html
5+
*
6+
* link : http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
7+
* 什么是注解?
8+
*
9+
* 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据
10+
*/
11+
public class Test {
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javacore.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+
@Target(ElementType.METHOD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
11+
public @interface Todo {
12+
public enum Priority {LOW, MEDIUM, HIGH}
13+
14+
public enum Status {STARTED, NOT_STARTED}
15+
16+
String author() default "Wu";
17+
18+
Priority priority() default Priority.LOW;
19+
20+
Status status() default Status.NOT_STARTED;
21+
}
22+
23+
@interface Author{
24+
String value();
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.javacore.annotation;
2+
3+
4+
public class UserAnnotationMethod {
5+
6+
@Todo(priority = Todo.Priority.MEDIUM, author = "Wurenjie")
7+
public void method1(){
8+
9+
}
10+
11+
@Author("aaa")
12+
public void method2(){
13+
14+
}
15+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.javacore.annotation.controller;
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+
//public enum ElementType {
9+
// /**标明该注解可以用于类、接口(包括注解类型)或enum声明*/
10+
// TYPE,
11+
//
12+
// /** 标明该注解可以用于字段(域)声明,包括enum实例 */
13+
// FIELD,
14+
//
15+
// /** 标明该注解可以用于方法声明 */
16+
// METHOD,
17+
//
18+
// /** 标明该注解可以用于参数声明 */
19+
// PARAMETER,
20+
//
21+
// /** 标明注解可以用于构造函数声明 */
22+
// CONSTRUCTOR,
23+
//
24+
// /** 标明注解可以用于局部变量声明 */
25+
// LOCAL_VARIABLE,
26+
//
27+
// /** 标明注解可以用于注解声明(应用于另一个注解上)*/
28+
// ANNOTATION_TYPE,
29+
//
30+
// /** 标明注解可以用于包声明 */
31+
// PACKAGE,
32+
//
33+
// /**
34+
// * 标明注解可以用于类型参数声明(1.8新加入)
35+
// * @since 1.8
36+
// */
37+
// TYPE_PARAMETER,
38+
//
39+
// /**
40+
// * 类型使用声明(1.8新加入)
41+
// * @since 1.8
42+
// */
43+
// TYPE_USE
44+
//}
45+
46+
public interface SomeAnnoation {
47+
}
48+
49+
@Target({ElementType.TYPE}) // for the type of class
50+
@Retention(RetentionPolicy.RUNTIME)
51+
@interface MyController {
52+
String value() default "";
53+
}
54+
55+
@Target({ElementType.METHOD, ElementType.TYPE})
56+
@Retention(RetentionPolicy.RUNTIME)
57+
@interface RequestUrl {
58+
String url() default "";
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.javacore.util;
2+
/**
3+
* 数组工具类
4+
*
5+
* @author 随心
6+
*
7+
*/
8+
public final class ArrayUtil {
9+
10+
/**
11+
* 判断数组是否为空
12+
* @param objs [Object[]]数组
13+
* @return
14+
*/
15+
public static boolean isEmpty(Object[] objs) {
16+
return objs == null || objs.length <= 0;
17+
}
18+
19+
/**
20+
* 判断数组是否非空
21+
* @param objs
22+
* @return
23+
*/
24+
public static boolean isNotEmpty(Object[] objs) {
25+
return !isEmpty(objs);
26+
}
27+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.javacore.util;
2+
3+
import java.io.File;
4+
import java.io.FileFilter;
5+
import java.net.JarURLConnection;
6+
import java.net.URL;
7+
import java.util.Enumeration;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import java.util.jar.JarEntry;
11+
import java.util.jar.JarFile;
12+
13+
14+
/**
15+
* 类操作工具类
16+
*
17+
* @author 随心
18+
*
19+
*/
20+
public final class ClassUtil {
21+
22+
/**
23+
* 获取一个类加载器
24+
*
25+
* @return
26+
*/
27+
public static ClassLoader getClassLoader() {
28+
// 获取当前线程的类加载器
29+
return Thread.currentThread().getContextClassLoader();
30+
}
31+
32+
/**
33+
* 加载类
34+
*
35+
* @param className [String]类名
36+
* @return [Class<?>]类对象
37+
*/
38+
public static Class<?> loadClass(String className) {
39+
return loadClass(className, false);
40+
}
41+
42+
/**
43+
* 加载类
44+
*
45+
* @param className [String]类名
46+
* @param isInitialized [boolean]是否初始化
47+
* @return [Class<?>]类对象
48+
*/
49+
public static Class<?> loadClass(String className, boolean isInitialized) {
50+
try {
51+
return Class.forName(className, isInitialized, getClassLoader());
52+
} catch (ClassNotFoundException e) {
53+
System.out.println("加载%s类失败!" + className);
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
58+
/**
59+
* 获取指定包名下的所有类
60+
*
61+
* @param packageName
62+
* @return
63+
*/
64+
public static Set<Class<?>> getClassSet(String packageName) {
65+
Set<Class<?>> classSet = new HashSet<Class<?>>();
66+
try {
67+
// TODO System.getProperty("file.separator")当前系统文件分隔符
68+
Enumeration<URL> urls = getClassLoader().getResources(
69+
packageName.replace(".", "/"));
70+
while(urls.hasMoreElements()) {
71+
URL url = urls.nextElement();
72+
if(url != null) {
73+
// 获取url协议:protocol://host:port/path?query#fragment
74+
String protocol = url.getProtocol();
75+
// 区分文件和引用包的类
76+
if("file".equals(protocol)) {
77+
String packagePath = url.
78+
toURI().getPath().replaceAll("%20", " "); // 这里先转成URI再取路径
79+
addClass(classSet, packagePath, packageName);
80+
} else if("jar".equals(protocol)) {
81+
JarURLConnection jarURLConnection =
82+
(JarURLConnection) url.openConnection();
83+
if(jarURLConnection != null) {
84+
JarFile jarFile = jarURLConnection.getJarFile();
85+
if(jarFile != null) {
86+
Enumeration<JarEntry> jarEntries = jarFile.entries();
87+
while(jarEntries.hasMoreElements()) {
88+
JarEntry jarEntry = jarEntries.nextElement();
89+
String jarEntryName = jarEntry.getName();
90+
if(jarEntryName.endsWith(".class")) {
91+
String className = jarEntryName.substring(0,
92+
jarEntryName.lastIndexOf(".")).
93+
replaceAll("/", ".");
94+
doAddClass(classSet, className);
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
}
102+
} catch (Exception e) {
103+
System.out.println("获取Class Set集合失败!" + e);
104+
throw new RuntimeException(e);
105+
}
106+
return classSet;
107+
}
108+
109+
/**
110+
* 添加指定包中所有类到集合中
111+
*
112+
* @param classSet [Set<Class<?>>]类集合
113+
* @param packagePath [String]包路径
114+
* @param packageName [String]包名
115+
*/
116+
private static void addClass(Set<Class<?>> classSet, String packagePath,
117+
String packageName) {
118+
File[] files = new File(packagePath).listFiles(new FileFilter() {
119+
@Override
120+
public boolean accept(File file) {
121+
return (file.isFile() && file.getName().endsWith(".class"))
122+
|| file.isDirectory();
123+
}
124+
});
125+
if(ArrayUtil.isNotEmpty(files)) {
126+
for(File file : files) {
127+
String fileName = file.getName();
128+
if(file.isFile()) {
129+
String className = fileName.substring(0, fileName.lastIndexOf("."));
130+
if(StringUtil.isNotEmpty(packageName)) {
131+
className = packageName + "." + className;
132+
}
133+
doAddClass(classSet, className);
134+
} else {
135+
String subPackagePath = fileName;
136+
if(StringUtil.isNotEmpty(packagePath)) {
137+
subPackagePath = packagePath + "/" + subPackagePath;
138+
}
139+
String subPackageName = fileName;
140+
if(StringUtil.isNotEmpty(packageName)) {
141+
subPackageName = packageName + "." + subPackageName;
142+
}
143+
addClass(classSet, subPackagePath, subPackageName);
144+
}
145+
}
146+
}
147+
}
148+
149+
/**
150+
* 添加指定类到集合中
151+
*
152+
* @param classSet [Set<Class<?>>]类集合
153+
* @param className [String]待添加类名
154+
*/
155+
private static void doAddClass(Set<Class<?>> classSet, String className) {
156+
Class<?> cls = loadClass(className, false);
157+
classSet.add(cls);
158+
}
159+
}

0 commit comments

Comments
 (0)