Dependency checker now cuts off unused methods

This commit is contained in:
Alexey Andreev 2013-11-18 17:28:02 +04:00
parent 6efbb75783
commit f88b868c39
5 changed files with 43 additions and 10 deletions

View File

@ -41,12 +41,16 @@ public class ClasslibTestGenerator {
renderer = new Renderer(writer, classSource);
DependencyChecker dependencyChecker = new DependencyChecker(classSource);
for (String testClass : testClasses) {
findTests(classSource.getClassHolder(testClass));
ClassHolder classHolder = classSource.getClassHolder(testClass);
findTests(classHolder);
MethodReference cons = new MethodReference(testClass, new MethodDescriptor("<init>", ValueType.VOID));
dependencyChecker.addEntryPoint(cons);
}
for (MethodReference methodRef : testMethods) {
dependencyChecker.addEntryPoint(methodRef);
}
dependencyChecker.checkDependencies();
dependencyChecker.cutUnachievableClasses();
for (String className : dependencyChecker.getAchievableClasses()) {
decompileClass(className);
}

View File

@ -235,4 +235,24 @@ public class DependencyChecker {
}
plugin.methodAchieved(this, methodRef);
}
public ListableClassHolderSource cutUnachievableClasses() {
MutableClassHolderSource cutClasses = new MutableClassHolderSource();
for (String className : achievableClasses.keySet()) {
ClassHolder classHolder = classSource.getClassHolder(className);
for (MethodHolder method : classHolder.getMethods().toArray(new MethodHolder[0])) {
MethodReference methodRef = new MethodReference(className, method.getDescriptor());
if (!methodCache.getCachedPreimages().contains(methodRef)) {
classHolder.removeMethod(method);
}
}
for (FieldHolder field : classHolder.getFields().toArray(new FieldHolder[0])) {
FieldReference fieldRef = new FieldReference(className, field.getName());
if (!fieldCache.getCachedPreimages().contains(fieldRef)) {
classHolder.removeField(field);
}
}
}
return cutClasses;
}
}

View File

@ -64,10 +64,6 @@ public class ClassHolder extends ElementHolder {
}
public void removeMethod(MethodHolder method) {
if (method.getOwner() != null) {
throw new IllegalArgumentException("Method " + method.getDescriptor() +
" is not a member of " + getName());
}
if (method.getOwner() != this) {
throw new IllegalArgumentException("Method " + method.getOwner().getName() +
"." + method.getDescriptor() + " is not a member of " + getName());
@ -97,10 +93,6 @@ public class ClassHolder extends ElementHolder {
}
public void removeField(FieldHolder field) {
if (field.getOwner() != null) {
throw new IllegalArgumentException("Field " + field.getName() +
" is not a member of " + getName());
}
if (field.getOwner() != this) {
throw new IllegalArgumentException("Field " + field.getOwner().getName() + "." +
field.getName() + " is not a member of " + getName());

View File

@ -0,0 +1,11 @@
package org.teavm.model;
import java.util.Set;
/**
*
* @author Alexey Andreev
*/
public interface ListableClassHolderSource extends ClassHolderSource {
Set<String> getClassNames();
}

View File

@ -15,6 +15,7 @@
*/
package org.teavm.model;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -22,9 +23,14 @@ import java.util.concurrent.ConcurrentMap;
*
* @author Alexey Andreev
*/
public class MutableClassHolderSource implements ClassHolderSource {
public class MutableClassHolderSource implements ListableClassHolderSource {
private ConcurrentMap<String, ClassHolder> classes = new ConcurrentHashMap<>();
@Override
public Set<String> getClassNames() {
return classes.keySet();
}
@Override
public ClassHolder getClassHolder(String name) {
return classes.get(name);