RegisterPackages -> ignore other files in compiled runtime directory

This commit is contained in:
jeffcheasey88 2024-09-23 03:33:05 +02:00
parent c06cc46787
commit 9f11825f97

View file

@ -5,17 +5,14 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.security.Key; import java.security.Key;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern;
import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLServerSocketFactory;
@ -106,6 +103,7 @@ public class Router<U extends User>{
Route route = method.getAnnotation(Route.class); Route route = method.getAnnotation(Route.class);
this.mappers[route.type().ordinal()].register(response, method, route); this.mappers[route.type().ordinal()].register(response, method, route);
System.out.println("Registered route "+method+" ["+route.type()+" "+route.path()+"]");
}catch(Exception e){ }catch(Exception e){
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
@ -117,13 +115,16 @@ public class Router<U extends User>{
} }
public Router<U> registerClass(Class<?> clazz, DependencyInjector injector) throws Exception{ public Router<U> registerClass(Class<?> clazz, DependencyInjector injector) throws Exception{
Constructor<?>[] constructors = clazz.getDeclaredConstructors(); Object instance = null;
if(constructors.length != 1) throw new IllegalArgumentException("Class with multiple constructor. Not supported by this framework for the moment.");
Object instance = constructors[0].newInstance(injector.apply(constructors[0]));
for(Method method : clazz.getDeclaredMethods()){ for(Method method : clazz.getDeclaredMethods()){
Route route = method.getAnnotation(Route.class); Route route = method.getAnnotation(Route.class);
if(route == null) continue; if(route == null) continue;
if(instance == null){
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if(constructors.length != 1) throw new IllegalArgumentException("Class with multiple constructor. Not supported by this framework for the moment.");
instance = constructors[0].newInstance(injector.apply(constructors[0]));
}
this.mappers[route.type().ordinal()].register(instance, method, route); this.mappers[route.type().ordinal()].register(instance, method, route);
System.out.println("Registered route "+method+" ["+route.type()+" "+route.path()+"]"); System.out.println("Registered route "+method+" ["+route.type()+" "+route.path()+"]");
@ -153,11 +154,11 @@ public class Router<U extends User>{
String line; String line;
while((line = reader.readLine()) != null){ while((line = reader.readLine()) != null){
if(line.endsWith(".class")){ if(line.endsWith(".class")){
Class<?> clazz = Class.forName(name+"."+line.substring(0, line.lastIndexOf('.'))); Class<?> clazz = Class.forName(name+"."+line.substring(0, line.length()-6));
registerClass(clazz, injector); registerClass(clazz, injector);
}else{ continue;
registerPackages(name+"."+line, injector);
} }
if(!line.contains(".")) registerPackages(name+"."+line, injector);
} }
reader.close(); reader.close();
}catch(Exception e){ }catch(Exception e){