当前版本无接受网络请求功能,不喜勿喷
本文将对代码核心进行讲解,源码已上传到gitee仓库
如果对注解有些忘记了,可以先移步到这里,对注解进行了详解
package Test.mvc;
import Test.Test;
import Test.annotation.Controller;
import Test.annotation.RequestMapping;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @BelongsProject: 3.9.demo
* @Author: yhz
* @CreateTime: 2023-07-22 14:36
* @Description: TODO
* @Version: 1.0
*/
public class YhzMVC {
//存的是该项目文件夹中所有.java结尾文件的全限定名
public static List<String>arr= new ArrayList<>();
//存的是每个controller上requestmapping的value以及该controller下所有的requestmapping的value以及所属方法
public static HashMap<String, Map<String, Method>> map=new HashMap<>();
//controller的requestmapping的value值以及controller对象
public static Map<String,Object>controllerMap = new HashMap<>();
public static void exec(String classPath,String methodPath){
if(controllerMap.get(classPath)==null){
System.out.println("没有这个类 404");
}else {
if(map.get(classPath).get(methodPath)==null){
System.out.println("没有这个方法 404");
}else {
try {
map.get(classPath).get(methodPath).invoke(controllerMap.get(classPath));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public static void getFilePath(File file) {
File[] fs = file.listFiles();
for (File f : fs) {
if (f.isDirectory()){
getFilePath(f);
}
if (f.isFile()) {
String filepath = f.toString();
filepath = filepath.split("src")[1];
filepath = filepath.substring(1,filepath.length());
if( filepath.endsWith(".java")) {
//把是.java文件的全类名放到arr中
arr.add(filepath.replace("\\", ".").replace(".java", ""));
}
}
}
}
//查找所有controller,并创建对象装入Map里(“url”:Object)
public static void chooseController() {
for(String file: arr){
try {
Class<?> aClass = Class.forName(file);
//如果有Controller注解
if(aClass.isAnnotationPresent(Controller.class)){
//如果有RequestMapping注解
if(aClass.isAnnotationPresent(RequestMapping.class)){
RequestMapping requestMapping = getRequestMapping(aClass);
//如果之前已经有了一样的 不同controller的requestmapping的值,说明有冲突。
if(map.containsKey(requestMapping.value())){
throw new RuntimeException("类多注解值:"+requestMapping.value());
}else {
map.put(requestMapping.value(),new HashMap<>());
controllerMap.put(requestMapping.value(),aClass.newInstance());
}
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if(declaredMethod.isAnnotationPresent(RequestMapping.class)){
RequestMapping mapping = getRequestMapping(declaredMethod);
if(map.get(requestMapping.value()).containsKey(mapping.value())){
throw new RuntimeException("方法多注解值:"+requestMapping.value());
}else {
map.get(requestMapping.value()).put(mapping.value(),declaredMethod);
}
}
}
}else {
throw new RuntimeException("类无requestMapping注解");
}
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
//Java基础 : 方法的重载。
public static RequestMapping getRequestMapping(Class cl){
Annotation annotation = cl.getAnnotation(RequestMapping.class);
if(annotation instanceof RequestMapping){
return (RequestMapping) annotation;
}
return null;
}
public static RequestMapping getRequestMapping(Method method){
Annotation annotation = method.getAnnotation(RequestMapping.class);
if(annotation instanceof RequestMapping){
return (RequestMapping) annotation;
}
return null;
}
}