1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import tools.Scenario;
/**
* Servlet implementation class Reflexion
*/
@WebServlet("/TestReflexion")
public class TestReflexion extends HttpServlet {
private static final long serialVersionUID = 1L;
static Logger logger=java.util.logging.Logger.getLogger(TestReflexion.class.getName());
private static boolean debug = true;
/**
* @see HttpServlet#HttpServlet()
*/
public TestReflexion() throws ClassNotFoundException, IOException{
super();
logger.log(Level.INFO,"Nom de la classe : ");
Collection<Class<?>> classes = new ArrayList<Class<?>>();
JarFile jar = new JarFile("/home/blackallsun/workspace/GenieLogicielProject/WebContent/WEB-INF/lib/sweetHome3D5NonFinie.jar");
for (Enumeration<JarEntry> entries = jar.entries() ; entries.hasMoreElements() ;){
JarEntry entry = entries.nextElement();
String file = entry.getName();
if (file.endsWith(".class")){
String classname = file.replace('/', '.').substring(0, file.length() - 6);
try {
Class<?> c = Class.forName(classname);classes.add(c);}
catch (Throwable e) { System.out.println("WARNING: failed to instantiate " + classname + " from " + file);}}}
for (Class<?> c : classes){
logger.log(Level.ALL,"Classe : "+c);
getReflexionClass(c);
} }
private static void getReflexionClass(Class<?> classe) throws ClassNotFoundException{
// affiche la superclasse
System.out.println("Classe de base --------------------------------");
System.out.println(" "+classe.getSuperclass());
// affiche tous les attributs
System.out.println("Attributs -------------------------------------");
for (Field attribut : classe.getDeclaredFields()) {
System.out.print(" "+Modifier.toString(attribut.getModifiers()));
System.out.println(" "+attribut.getType()+" "+attribut.getName()+";");
Annotation[] parameterAnnotations = attribut.getDeclaredAnnotations();
for(Annotation anno:parameterAnnotations){
System.out.println(anno);
}
}
// affiche tous les consructeurs
System.out.println("Constructeurs ---------------------------------");
for (Constructor constructeur : classe.getDeclaredConstructors()) {
System.out.print(" "+Modifier.toString(constructeur.getModifiers()));
System.out.print(" "+constructeur.getName()+"(");
Class[] typeParamètres = constructeur.getParameterTypes();
for (int i=0; i<typeParamètres.length; i++) {
if (i>0) System.out.print(", ");
System.out.print(typeParamètres[i].getName());
}
System.out.println(");");
Annotation[][] parameterAnnotations = constructeur.getParameterAnnotations();
Class[] parameterTypes = constructeur.getParameterTypes();
int i=0;
for(Annotation[] annotations : parameterAnnotations){
Class parameterType = parameterTypes[i++];
for(Annotation annotation : annotations){
if(annotation instanceof Scenario){
Scenario myAnnot = (Scenario) annotation;
System.out.println("param: " + parameterType.getName());
System.out.println("name : " + myAnnot.assigneA());
System.out.println("apparait dans le scenario : " + myAnnot.apparaitDsLeScenario());
System.out.println("importance: " + myAnnot.importance());
System.out.println("type: " + myAnnot.typeDeClasse());
System.out.println("type enum: " + myAnnot.getType());}}}}
// affiche toutes les méthodes
System.out.println("Méthodes --------------------------------------");
for (Method methode : classe.getDeclaredMethods()) {
System.out.print(" "+methode.getReturnType()+" ");
System.out.print(Modifier.toString(methode.getModifiers()));
System.out.print(" "+methode.getName()+"(");
Class[] typeParamètres = methode.getParameterTypes();
for (int i=0; i<typeParamètres.length; i++) {
if (i>0) System.out.print(", ");
System.out.print(typeParamètres[i].getName());
}
System.out.println(");");
Annotation[][] parameterAnnotations = methode.getParameterAnnotations();
Class[] parameterTypes = methode.getParameterTypes();
int i=0;
for(Annotation[] annotations : parameterAnnotations){
Class parameterType = parameterTypes[i++];
for(Annotation annotation : annotations){
if(annotation instanceof Scenario){
Scenario myAnnot = (Scenario) annotation;
System.out.println("param: " + parameterType.getName());
System.out.println("name : " + myAnnot.assigneA());
System.out.println("apparait dans le scenario : " + myAnnot.apparaitDsLeScenario());
System.out.println("importance: " + myAnnot.importance());
System.out.println("type: " + myAnnot.typeDeClasse());
System.out.println("type enum: " + myAnnot.getType());
}
}}}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter(); //ouverture du flot de sortie
out.println("<HTML>"); //préparation de l'envoi de données
out.println("<HEAD>");
out.println("<TITLE>Titre de la page !!!</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
} |
Partager