View Javadoc

1   /*
2    *
3    */
4   package org.softevo.jdynpur.eval;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.util.Collection;
9   import java.util.HashSet;
10  import java.util.Iterator;
11  import java.util.Vector;
12  
13  import org.softevo.util.asm.MethodIdentifier;
14  import org.softevo.util.io.ObjectReaderWriter;
15  
16  public class StaticDynamicComparer {
17  
18  	private String dataDirectoryName;
19  
20  	public StaticDynamicComparer(String dataDirectoryName) {
21  		this.dataDirectoryName = dataDirectoryName;
22  	}
23  
24  	/*private HashSet<MethodIdentifier> readHashSet(String fileName) throws IOException {
25  		FileInputStream fileInputStream = null;
26  		ObjectInputStream objectInputStream;
27  
28  		try {
29  			fileInputStream = new FileInputStream(fileName);
30  			objectInputStream = new ObjectInputStream(fileInputStream);
31  			try {
32  				return (HashSet<MethodIdentifier>) objectInputStream.readObject();
33  			} catch (ClassNotFoundException noSuchClass) {
34  				throw new IOException("Class not found: " + noSuchClass.getMessage());
35  			}
36  		} finally {
37  			if (fileInputStream != null) {
38  				try {
39  					fileInputStream.close();
40  				} catch (IOException finalizeException) {
41  					// ignore exception
42  				}
43  			}
44  		}
45  	}*/
46  
47  	private HashSet<MethodIdentifier> filterNotExecutedMethods(HashSet<MethodIdentifier> identifiers,
48  																															HashSet<MethodIdentifier> executedMethods) {
49  		HashSet<MethodIdentifier> result;
50  		Iterator<MethodIdentifier> idIterator;
51  		MethodIdentifier identifier;
52  
53  		result = new HashSet<MethodIdentifier>(identifiers.size());
54  		idIterator = identifiers.iterator();
55  		while (idIterator.hasNext()) {
56  			identifier = idIterator.next();
57  			if (executedMethods.contains(identifier)) {
58  				result.add(identifier);
59  			}
60  		}
61  		return result;
62  	}
63  
64  	private HashSet<MethodIdentifier> removeAspectJPackageNames(Collection<MethodIdentifier> identifiers) {
65  		HashSet<MethodIdentifier> result;
66  		Iterator<MethodIdentifier> idIterator;
67  		MethodIdentifier identifier;
68  
69  		result = new HashSet<MethodIdentifier>();
70  		idIterator = identifiers.iterator();
71  		while (idIterator.hasNext()) {
72  			identifier = idIterator.next();
73  			/*
74  			 * if
75  			 * (/*identifier.getClassName().endsWith("AbstractSyntaxTreeVisitorAdapter") &&
76  			 * identifier.getMethodName().equals("endVisit")) {
77  			 * System.out.println("Old identifier: " + identifier);
78  			 * System.out.println("New identifier: " + new
79  			 * MethodIdentifier(identifier.getClassName().replaceAll("org\\.aspectj\\.org\\.eclipse",
80  			 * "org.eclipse"), identifier.getMethodName(),
81  			 * identifier.getSignature().replaceAll("org/aspectj/org/eclipse",
82  			 * "org/eclipse"))); }
83  			 */
84  			result.add(new MethodIdentifier(identifier.getClassName().replaceAll("org\\.aspectj\\.org\\.eclipse",
85  																																						"org.eclipse"), identifier.getMethodName(),
86  					identifier.getSignature().replaceAll("org/aspectj/org/eclipse", "org/eclipse"), identifier.getAccess()));
87  		}
88  		return result;
89  	}
90  
91  	private HashSet<MethodIdentifier> getCommonIdentifiers(HashSet<MethodIdentifier> firstSet,
92  																													HashSet<MethodIdentifier> secondSet) {
93  		HashSet<MethodIdentifier> result;
94  		Iterator<MethodIdentifier> idIterator;
95  		MethodIdentifier identifier;
96  
97  		result = new HashSet<MethodIdentifier>();
98  		idIterator = firstSet.iterator();
99  		while (idIterator.hasNext()) {
100 			identifier = idIterator.next();
101 			if (secondSet.contains(identifier)) {
102 				result.add(identifier);
103 			}
104 		}
105 		return result;
106 	}
107 
108 	private void displaySet(HashSet<MethodIdentifier> set, String message) {
109 		Iterator<MethodIdentifier> methodIdIterator;
110 
111 		methodIdIterator = set.iterator();
112 		System.out.println(message);
113 		while (methodIdIterator.hasNext()) {
114 			System.out.println(methodIdIterator.next());
115 		}
116 		System.out.println("========================================================");
117 		System.out.println();
118 	}
119 
120 	public void compare() throws IOException {
121 		HashSet<MethodIdentifier> dynamicImpureMethods, dynamicPureMethods, executedMethods, executedStaticPureMethods, executedStaticImpureMethods, staticAnalyzedMethods, staticPureMethods, staticImpureMethods;
122 		Iterator<MethodIdentifier> methodIdIterator;
123 		MethodIdentifier identifier;
124 
125 		ObjectReaderWriter<HashSet<MethodIdentifier>> hashSetReader;
126 		ObjectReaderWriter<Vector<MethodIdentifier>> vectorReader;
127 
128 		hashSetReader = new ObjectReaderWriter<HashSet<MethodIdentifier>>();
129 		vectorReader = new ObjectReaderWriter<Vector<MethodIdentifier>>();
130 		staticPureMethods = removeAspectJPackageNames(vectorReader.read(new File(dataDirectoryName + File.separator
131 				+ "staticpuremethods.ser")));
132 		staticImpureMethods = removeAspectJPackageNames(vectorReader.read(new File(dataDirectoryName + File.separator
133 				+ "staticimpuremethods.ser")));
134 		dynamicPureMethods = hashSetReader.read(new File(dataDirectoryName + File.separator + "dynamicpuremethods.ser"));
135 		dynamicImpureMethods = hashSetReader
136 				.read(new File(dataDirectoryName + File.separator + "dynamicimpuremethods.ser"));
137 		executedMethods = hashSetReader.read(new File(dataDirectoryName + File.separator + "dynamicexecutedmethods.ser"));
138 		executedStaticImpureMethods = filterNotExecutedMethods(staticImpureMethods, executedMethods);
139 		executedStaticPureMethods = filterNotExecutedMethods(staticPureMethods, executedMethods);
140 		System.out.println("Static impure methods: " + staticImpureMethods.size());
141 		System.out.println("Static pure methods: " + staticPureMethods.size());
142 		System.out.println("Dynamic pure methods: " + dynamicPureMethods.size());
143 		System.out.println("Dynamic impure methods: " + dynamicImpureMethods.size());
144 		System.out.println("Dynamic executed methods: " + executedMethods.size());
145 		System.out.println("Static executed impure: " + executedStaticImpureMethods.size());
146 		System.out.println("Static executed pure: " + executedStaticPureMethods.size());
147 		System.out.println("Static Pure Dynamic Impure: "
148 				+ getCommonIdentifiers(executedStaticPureMethods, dynamicImpureMethods).size());
149 		if (getCommonIdentifiers(executedStaticPureMethods, dynamicImpureMethods).size() > 0) {
150 			displaySet(getCommonIdentifiers(executedStaticPureMethods, dynamicImpureMethods), "List of methods");
151 		}
152 		System.out.println("Static Pure Dynamic Pure: "
153 				+ getCommonIdentifiers(executedStaticPureMethods, dynamicPureMethods).size());
154 		System.out.println("Static Impure Dynamic Impure: "
155 				+ getCommonIdentifiers(executedStaticImpureMethods, dynamicImpureMethods).size());
156 		System.out.println("Static Impure Dynamic Pure: "
157 				+ getCommonIdentifiers(executedStaticImpureMethods, dynamicPureMethods).size());
158 		System.out.println();
159 
160 		staticAnalyzedMethods = new HashSet<MethodIdentifier>(staticImpureMethods);
161 		staticAnalyzedMethods.addAll(staticPureMethods);
162 		System.out.println("Methods executed dynamically but not analyzed:");
163 		methodIdIterator = executedMethods.iterator();
164 		while (methodIdIterator.hasNext()) {
165 			identifier = methodIdIterator.next();
166 			if (!staticAnalyzedMethods.contains(identifier)) {
167 				System.out.println(identifier);
168 			}
169 		}
170 	}
171 
172 	/**
173 	 * @param args
174 	 */
175 	public static void main(String[] args) throws Exception {
176 		StaticDynamicComparer comparer;
177 
178 		if (args.length != 1) {
179 			System.out.println("Usage: java org.softevo.jdynpur.StaticDynamicComparer <dataDirectory>");
180 		} else {
181 			comparer = new StaticDynamicComparer(args[0]);
182 			comparer.compare();
183 		}
184 	}
185 
186 }