package extraction; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Map; // AVERTISSEMENT : ce code peut etre incomplet, il vous donne seulement des exemples de ce que l'on peut obtenir // avec le package reflect; Vous le completerez par ce qui peut vous etre utile pour le TP. public class ExtractionInterfaces2016 { private ArrayList CharacteristicList; private ArrayList EntityList; private boolean[][] table; public void createTable(int i) throws SecurityException, ClassNotFoundException{ // Characteristic list and entity list are assigned if (CharacteristicList==null || EntityList==null) return; table = new boolean[EntityList.size()][CharacteristicList.size()]; for (String e:EntityList) for (String c:CharacteristicList){ switch(i){ case 0: if (methodNames(e).contains(c)) table[EntityList.indexOf(e)][CharacteristicList.indexOf(c)] = true; break; case 1: if (finalStaticFieldNamesTypes(e).contains(c)) table[EntityList.indexOf(e)][CharacteristicList.indexOf(c)] = true; break; case 2: if (signatures(e).contains(c)) table[EntityList.indexOf(e)][CharacteristicList.indexOf(c)] = true; break; case 3: if (implementNames(e).contains(c)){ table[EntityList.indexOf(e)][CharacteristicList.indexOf(c)] = true; } } } } public void afficheTable(){ if (table == null) return; System.out.print("FormalContext Collections"+"\n"+"| |"); for (int k=0; k< table[0].length; k++){ System.out.print(CharacteristicList.get(k)+"|"); } System.out.println(); for (int i=0; i< table.length; i++){ System.out.print("|"+EntityList.get(i)+"|"); for (int j=0; j< table[0].length; j++){ if (table[i][j]) System.out.print("x"); System.out.print("|"); } System.out.println(); } } public void ecrireTable(String nomFichier) throws IOException{ if (table == null) return; BufferedReader fc = new BufferedReader (new InputStreamReader (System.in)); BufferedWriter ff = new BufferedWriter (new FileWriter (nomFichier)); ff.write("FormalContext Collections"+"\n"+"| |"); for (int k=0; k< table[0].length; k++){ ff.write(CharacteristicList.get(k)+"|"); } ff.newLine();; for (int i=0; i< table.length; i++){ ff.write("|"+EntityList.get(i)+"|"); for (int j=0; j< table[0].length; j++){ if (table[i][j]) ff.write("x"); ff.write("|"); } ff.newLine();; } ff.close(); } public ArrayList getCharacteristicList() { return CharacteristicList; } public void setCharacteristicList(ArrayList characteristicList) { CharacteristicList = characteristicList; } public ArrayList getEntityList() { return EntityList; } public void setEntityList(ArrayList entityList) { EntityList = entityList; } public boolean[][] getTable() { return table; } public void setTable(boolean[][] table) { this.table = table; } public static List getAllFields(List fields, String className) throws ClassNotFoundException { fields.addAll(Arrays.asList(Class.forName(className).getDeclaredFields())); if (Class.forName(className).getSuperclass() != null) { fields = getAllFields(fields, Class.forName(className).getSuperclass().getName()); } return fields; } public static boolean isCollectionOrMap(String className) throws ClassNotFoundException { if(Class.forName(className).isAssignableFrom(Collection.class) || Class.forName(className).isAssignableFrom(Map.class)){ return true; } if(Class.forName(className).getInterfaces().length != 0){ if(isCollectionOrMap(Class.forName(className).getInterfaces()[0].getName())){ return true; } } return false; } public static ArrayList finalStaticFieldNamesTypes(String className) throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); //Object objectInstance = new Object(); for (Field a : getAllFields(new LinkedList(), className)) if (Modifier.isFinal(a.getModifiers()) && Modifier.isStatic(a.getModifiers())){ /* if(!a.isAccessible()){ a.setAccessible(true); } s+="\t" + a.getName()+" "+a.getType().getName()+" = "+a.get(objectInstance)+"\n"; */ liste.add(a.getName()+" "+a.getType().getName()); } return liste; } public static void createFile(String filename, String s) throws IOException{ PrintWriter writer = new PrintWriter(filename, "UTF-8"); writer.println(s); writer.close(); } public static void createFileFromList(String filename, ArrayList l) throws FileNotFoundException, UnsupportedEncodingException{ PrintWriter writer = new PrintWriter(filename, "UTF-8"); for(String s : l){ writer.println(s); } writer.close(); } public static void createFileAppend(String filename, String s) throws IOException{ PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename, true))); writer.println(s); writer.close(); } public static ArrayList methodNames(String className)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>();; for (Method m : Class.forName(className).getMethods()) liste.add(m.getName()); return liste; } public static ArrayList implementNames(String className)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); Class current = Class.forName(className); while(current != null){ for(Class i : current.getInterfaces()){ if(!isCollectionOrMap(i.getName())){ if(!liste.contains(i.getName())){ liste.add(i.getName()); } } } current = current.getSuperclass(); } return liste; } public static ArrayList implementNamesSet(ArrayList classNameList)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); for(String c : classNameList){ Class current = Class.forName(c); while(current != null){ for(Class i : current.getInterfaces()){ if(!isCollectionOrMap(i.getName())){ if(!liste.contains(i.getName())){ liste.add(i.getName()); } } } current = current.getSuperclass(); } } return liste; } public static ArrayList attributeNamesSet(ArrayList classNameList)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); for(String c : classNameList){ for (Field f : getAllFields(new LinkedList(), c)){ if (Modifier.isFinal(f.getModifiers()) && Modifier.isStatic(f.getModifiers())){ if (!liste.contains(f.getName()+" "+f.getType().getName())){ liste.add(f.getName()+" "+f.getType().getName()); } } } } return liste; } public static ArrayList methodNameSet(ArrayList classNameList)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); for (String c: classNameList) for (Method m : Class.forName(c).getMethods()) if (! liste.contains(m.getName())) liste.add(m.getName()); return liste; } // extrait toutes les signatures de toutes les classes de la liste passée en parametre public static ArrayList signatureSet(ArrayList classNameList)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); for (String c: classNameList) for (String signature : signatures(c)) if (! liste.contains(signature)) liste.add(signature); liste.removeAll(signatures("java.lang.Object")); return liste; } public static ArrayList signatures(String className)throws SecurityException, ClassNotFoundException { ArrayList liste = new ArrayList<>(); for (Method m : Class.forName(className).getMethods()) { String result =""; result += m.getReturnType() + " "; result += m.getName() + "("; for (Class paramClass : m.getParameterTypes()) result += paramClass.getName() + ", "; if(m.getParameterTypes().length != 0) result = result.substring(0, result.length()-2); result += ") "; /*for (Class exceptionType : m.getExceptionTypes()) result += exceptionType.getName() + " "; if(m.getExceptionTypes().length != 0) result = result.substring(0, result.length()-1); */ liste.add(result); } return liste; } public static String methodNamesParamTypes(String className)throws SecurityException, ClassNotFoundException { String s=""; System.out.println(Class.forName(className).getMethods().length); for (Method m : Class.forName(className).getMethods()) { s+=m.getName()+"("; for (Class paramType : m.getParameterTypes()) s+=paramType.getName()+","; s+=")\n"; } return s; } public static String interfaceListNames(String[] classNameList)throws SecurityException, ClassNotFoundException, FileNotFoundException, UnsupportedEncodingException { String s=""; for (String c : classNameList) if (Class.forName(c).isInterface()){ s+=c+"\n"; } return s; } private static ArrayList interfaceList(String[] nameList) throws ClassNotFoundException { ArrayList liste = new ArrayList<>(); for (String c : nameList) if (Class.forName(c).isInterface()) {liste.add(c);} return liste; } public static String abstractClassListNames(String[] classNameList)throws SecurityException, ClassNotFoundException, FileNotFoundException, UnsupportedEncodingException { String s=""; for (String c : classNameList) if (! Class.forName(c).isInterface() && Modifier.isAbstract(Class.forName(c).getModifiers())){ s+=c+"\n"; } return s; } public static ArrayList abstractClassList(String[] classNameList)throws SecurityException, ClassNotFoundException, FileNotFoundException, UnsupportedEncodingException { ArrayList liste = new ArrayList<>(); for (String c : classNameList) if (! Class.forName(c).isInterface() && Modifier.isAbstract(Class.forName(c).getModifiers())) {liste.add(c);} return liste; } private static ArrayList concreteClassList(String[] classNameList) throws ClassNotFoundException, IOException { ArrayList liste = new ArrayList<>(); for (String c : classNameList) if (! Modifier.isAbstract(Class.forName(c).getModifiers())){ liste.add(c); } return liste; } private static ArrayList concreteAbstractClassList(String[] classNameList) throws ClassNotFoundException, IOException { ArrayList liste = new ArrayList<>(); for (String c : classNameList) if (!Class.forName(c).isInterface()){ liste.add(c); } return liste; } public static void ajoutPrefixe(String prefixe, String[] noms){ for (int i=0; i < noms.length; i++) if (noms[i].equals("Object")) noms[i]= "java.lang."+noms[i]; else noms[i]= prefixe+noms[i]; } public static int lireEntier () { BufferedReader clavier = null; int ilu ; try { String s = clavier.readLine(); ilu = Integer.parseInt(s); } catch (IOException e) { ilu = 0; //par defaut } return ilu; } public static void main(String[] args) throws SecurityException, ClassNotFoundException, IOException, IllegalArgumentException, IllegalAccessException { // on se concentre sur quelques séquences String[] listeDesClassesInterfaces = {"java.util.concurrent.ArrayBlockingQueue", "java.util.ArrayDeque", "java.util.ArrayList", "javax.management.AttributeList", "java.util.concurrent.ConcurrentLinkedDeque", "java.util.concurrent.ConcurrentLinkedQueue", "java.util.concurrent.CopyOnWriteArrayList", "java.util.concurrent.DelayQueue", "java.util.concurrent.LinkedBlockingDeque", "java.util.concurrent.LinkedBlockingQueue", "java.util.LinkedList", "java.util.concurrent.LinkedTransferQueue", "java.util.concurrent.PriorityBlockingQueue", "java.util.PriorityQueue", "javax.management.relation.RoleList", "javax.management.relation.RoleUnresolvedList", "java.util.Stack", "java.util.concurrent.SynchronousQueue", "java.util.Vector"}; //ajoutPrefixe("java.util.",listeDesClassesInterfaces); ArrayList listeComplete = new ArrayList<>(); listeComplete.addAll(Arrays.asList(listeDesClassesInterfaces)); /* Create File containing the names of concrete classes*/ //DataExtractionClassSet dFile = new DataExtractionClassSet(); ExtractionInterfaces2016 dFile = new ExtractionInterfaces2016(); dFile.setEntityList(concreteClassList(listeDesClassesInterfaces)); createFileFromList("resultats/concrete2016.txt", dFile.getEntityList()); /* Create table for concrete classes signatures*/ // cette table servira de fichier d'entree pour un outil dans un prochain TP // elle peut aider à comprendre quelle classe contient quelle signature de methode // le fichier csv peut etre ouvert dans open office (separateur '|') pour manipuler les colonnes // et trouver des groupes de signatures communes à des groupes de classes concretes ArrayList classesConcretes = new ArrayList<>(); classesConcretes.addAll(concreteClassList(listeDesClassesInterfaces)); ExtractionInterfaces2016 d4 = new ExtractionInterfaces2016(); d4.setEntityList(classesConcretes); d4.setCharacteristicList(signatureSet(d4.getEntityList())); d4.createTable(2); d4.afficheTable(); d4.ecrireTable("resultats/sequencesConc2016.rcft"); d4.ecrireTable("resultats/sequencesConc2016.csv"); } }