wildcard matching

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@603 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent a147a6dbe3
commit e47813570c
  1. 7
      jode/jode/obfuscator/ClassBundle.java
  2. 31
      jode/jode/obfuscator/ClassIdentifier.java
  3. 228
      jode/jode/obfuscator/PackageIdentifier.java

@ -74,8 +74,8 @@ public class ClassBundle {
return ident.getIdentifier(ref.getName(), ref.getType()); return ident.getIdentifier(ref.getName(), ref.getType());
} }
public void loadClasses(String packageOrClass) { public void loadClasses(String wildcard) {
basePackage.loadClasses(packageOrClass); basePackage.loadMatchingClasses(new WildCard(wildcard));
} }
public void reachableIdentifier(String fqn, boolean isVirtual) { public void reachableIdentifier(String fqn, boolean isVirtual) {
@ -88,7 +88,8 @@ public class ClassBundle {
basePackage.applyPreserveRule(preserveRule); basePackage.applyPreserveRule(preserveRule);
Enumeration enum = fullqualifiednames.elements(); Enumeration enum = fullqualifiednames.elements();
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
basePackage.preserveIdentifier((String) enum.nextElement()); basePackage.preserveMatchingIdentifier
(new WildCard((String) enum.nextElement()));
} }
analyze(); analyze();
} }

@ -57,11 +57,25 @@ public class ClassIdentifier extends Identifier {
} }
} }
public void preserveMatchingIdentifier(WildCard wildcard) {
System.err.println("preserve "+getFullName()+"; "+wildcard);
String fullName = getFullName() + ".";
for (int i=0; i< identifiers.length; i++) {
if (wildcard.matches(fullName + identifiers[i].getName())
|| wildcard.matches(fullName + identifiers[i].getName()
+ "." +identifiers[i].getType())) {
System.err.println("preserving "+identifiers[i]);
setPreserved();
identifiers[i].setPreserved();
identifiers[i].setReachable();
}
}
}
public void preserveIdentifier(String name, String typeSig) { public void preserveIdentifier(String name, String typeSig) {
for (int i=0; i< identifiers.length; i++) { for (int i=0; i< identifiers.length; i++) {
if (WildCard.matches(name, identifiers[i].getName()) if (name.equals(identifiers[i].getName())
&& (typeSig == null && typeSig.equals(identifiers[i].getType()))
|| WildCard.matches(typeSig, identifiers[i].getType())))
identifiers[i].setPreserved(); identifiers[i].setPreserved();
} }
} }
@ -79,10 +93,8 @@ public class ClassIdentifier extends Identifier {
boolean isVirtual) { boolean isVirtual) {
// if (!isVirtual || (info.getModifiers() & Modifier.ABSTRACT) == 0) { // if (!isVirtual || (info.getModifiers() & Modifier.ABSTRACT) == 0) {
for (int i=0; i< identifiers.length; i++) { for (int i=0; i< identifiers.length; i++) {
if (WildCard.matches(name, identifiers[i].getName()) if (name.equals(identifiers[i].getName())
&& (typeSig == null && typeSig.equals(identifiers[i].getType()))
|| WildCard.matches(typeSig,
identifiers[i].getType())))
identifiers[i].setReachable(); identifiers[i].setReachable();
} }
// } // }
@ -401,7 +413,10 @@ public class ClassIdentifier extends Identifier {
* @return the full qualified name, excluding trailing dot. * @return the full qualified name, excluding trailing dot.
*/ */
public String getFullName() { public String getFullName() {
return pack.getFullName() + getName(); if (pack.parent == null)
return getName();
else
return pack.getFullName() + "." + getName();
} }
/** /**

@ -43,14 +43,20 @@ public class PackageIdentifier extends Identifier {
this.bundle = bundle; this.bundle = bundle;
this.parent = parent; this.parent = parent;
this.name = name; this.name = name;
this.loadOnDemand = loadOnDemand;
this.loadedClasses = new Hashtable(); this.loadedClasses = new Hashtable();
if (loadOnDemand && !Obfuscator.shouldStrip) { if (loadOnDemand)
setLoadOnDemand();
}
public void setLoadOnDemand() {
if (loadOnDemand)
return;
loadOnDemand = true;
if (!Obfuscator.shouldStrip) {
// Load all classes and packages now, so they don't get stripped // Load all classes and packages now, so they don't get stripped
Vector v = new Vector(); Vector v = new Vector();
Enumeration enum = Enumeration enum =
ClassInfo.getClassesAndPackages(parent.getFullName() ClassInfo.getClassesAndPackages(getFullName());
+getName());
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
//insert sorted and remove double elements; //insert sorted and remove double elements;
String subclazz = (String)enum.nextElement(); String subclazz = (String)enum.nextElement();
@ -70,7 +76,10 @@ public class PackageIdentifier extends Identifier {
enum = v.elements(); enum = v.elements();
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
String subclazz = (String) enum.nextElement(); String subclazz = (String) enum.nextElement();
String fullname = getFullName() + subclazz; String fullname = getFullName();
fullname = (fullname.length() > 0)
? fullname + "."+ subclazz
: subclazz;
if (ClassInfo.isPackage(fullname)) { if (ClassInfo.isPackage(fullname)) {
Identifier ident = new PackageIdentifier Identifier ident = new PackageIdentifier
(bundle, this, subclazz, true); (bundle, this, subclazz, true);
@ -111,12 +120,15 @@ public class PackageIdentifier extends Identifier {
if (index == -1) { if (index == -1) {
Identifier ident = (Identifier) loadedClasses.get(name); Identifier ident = (Identifier) loadedClasses.get(name);
if (ident == null) { if (ident == null) {
String fullname = getFullName() + name; String fullname = getFullName();
fullname = (fullname.length() > 0)
? fullname + "."+ name
: name;
if (ClassInfo.isPackage(fullname)) { if (ClassInfo.isPackage(fullname)) {
ident = new PackageIdentifier(bundle, this, name, true); ident = new PackageIdentifier(bundle, this, name, true);
loadedClasses.put(name, ident); loadedClasses.put(name, ident);
} else if (!ClassInfo.exists(fullname)) { } else if (!ClassInfo.exists(fullname)) {
System.err.println("Warning: Can't find class "+fullname); Obfuscator.err.println("Warning: Can't find class "+fullname);
} else { } else {
ident = new ClassIdentifier(bundle, this, name, ident = new ClassIdentifier(bundle, this, name,
ClassInfo.forName(fullname)); ClassInfo.forName(fullname));
@ -144,20 +156,91 @@ public class PackageIdentifier extends Identifier {
} }
} }
public Identifier loadClasses(String packageOrClass) { public void loadMatchingClasses(WildCard wildcard) {
int index = packageOrClass.indexOf('.'); String component = wildcard.getNextComponent(getFullName());
if (index == -1) { if (component != null) {
return loadClass(packageOrClass); Identifier ident = (Identifier) loadedClasses.get(component);
if (ident == null) {
String fullname = getFullName();
fullname = (fullname.length() > 0)
? fullname + "."+ name
: name;
if (ClassInfo.isPackage(fullname)) {
ident = new PackageIdentifier(bundle, this,
component, loadOnDemand);
loadedClasses.put(component, ident);
} else if (!ClassInfo.exists(fullname)) {
Obfuscator.err.println("Warning: Can't find class "+fullname);
} else if (wildcard.matches(fullname)) {
if (Obfuscator.verboseLevel > 1)
Obfuscator.err.println("loading Class " +fullname);
ident = new ClassIdentifier(bundle, this, name,
ClassInfo.forName(fullname));
loadedClasses.put(name, ident);
((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1)
ident.applyPreserveRule(bundle.preserveRule);
}
}
if (ident instanceof PackageIdentifier) {
if (wildcard.matches(ident.getFullName())) {
if (Obfuscator.verboseLevel > 0)
Obfuscator.err.println("loading Package " +ident.getFullName());
((PackageIdentifier) ident).setLoadOnDemand();
}
if (wildcard.startsWith(ident.getFullName()+"."))
((PackageIdentifier) ident).loadMatchingClasses(wildcard);
}
} else { } else {
String subpack = packageOrClass.substring(0, index); String fullname = getFullName();
PackageIdentifier pack = (PackageIdentifier) if (fullname.length() > 0)
loadedClasses.get(subpack); fullname += ".";
if (pack == null) { /* Load all matching classes and packages */
pack = new PackageIdentifier(bundle, this, Enumeration enum =
subpack, loadOnDemand); ClassInfo.getClassesAndPackages(getFullName());
loadedClasses.put(subpack, pack); while (enum.hasMoreElements()) {
String subclazz = (String)enum.nextElement();
if (loadedClasses.contains(subclazz))
continue;
String subFull = fullname + subclazz;
if (wildcard.matches(subFull)) {
if (ClassInfo.isPackage(subFull)) {
if (Obfuscator.verboseLevel > 0)
Obfuscator.err.println("loading Package " +subFull);
Identifier ident = new PackageIdentifier
(bundle, this, subclazz, true);
loadedClasses.put(subclazz, ident);
} else {
if (Obfuscator.verboseLevel > 1)
Obfuscator.err.println("loading Class " +subFull);
ClassIdentifier ident = new ClassIdentifier
(bundle, this,
subclazz, ClassInfo.forName(subFull));
loadedClasses.put(subclazz, ident);
((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1)
ident.applyPreserveRule(bundle.preserveRule);
}
} else if (ClassInfo.isPackage(subFull)
&& wildcard.startsWith(subFull + ".")) {
Identifier ident = new PackageIdentifier
(bundle, this, subclazz, loadOnDemand);
loadedClasses.put(subclazz, ident);
}
}
enum = loadedClasses.elements();
while (enum.hasMoreElements()) {
Identifier ident = (Identifier) enum.nextElement();
if (ident instanceof PackageIdentifier) {
if (wildcard.matches(ident.getFullName()))
((PackageIdentifier) ident).setLoadOnDemand();
if (wildcard.startsWith(ident.getFullName()+"."))
((PackageIdentifier) ident).loadMatchingClasses(wildcard);
}
} }
return pack.loadClasses(packageOrClass.substring(index+1));
} }
} }
@ -196,45 +279,94 @@ public class PackageIdentifier extends Identifier {
} }
} }
public void preserveIdentifier(String fqn) { public void preserveMatchingIdentifier(WildCard wildcard) {
int index = fqn.indexOf('.'); String component = wildcard.getNextComponent(getFullName());
String component = index == -1 ? fqn : fqn.substring(0, index); if (component != null) {
Identifier ident = getIdentifier(component); Identifier ident = (Identifier) loadedClasses.get(component);
if (ident == null) if (ident == null && loadOnDemand) {
return; String fullname = getFullName();
ident.setReachable(); fullname = (fullname.length() > 0)
ident.setPreserved(); ? fullname + "."+ component
if (index == -1) : component;
return; if (ClassInfo.isPackage(fullname)) {
ident = new PackageIdentifier(bundle, this,
component, loadOnDemand);
loadedClasses.put(component, ident);
} else if (!ClassInfo.exists(fullname)) {
Obfuscator.err.println("Warning: Can't find class "+fullname);
} else {
ident = new ClassIdentifier(bundle, this, name,
ClassInfo.forName(fullname));
loadedClasses.put(name, ident);
((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1)
ident.applyPreserveRule(bundle.preserveRule);
}
}
if (wildcard.startsWith(ident.getFullName()+".")) {
if (ident instanceof PackageIdentifier) if (ident instanceof PackageIdentifier)
((PackageIdentifier) ident).preserveIdentifier ((PackageIdentifier) ident)
(fqn.substring(index+1)); .preserveMatchingIdentifier(wildcard);
else { else
String method = fqn.substring(index+1); ((ClassIdentifier) ident)
index = method.indexOf('.'); .preserveMatchingIdentifier(wildcard);
if (index == -1) { }
((ClassIdentifier) ident).reachableIdentifier
(method, null, false);
((ClassIdentifier) ident).preserveIdentifier
(method, null);
} else { } else {
((ClassIdentifier) ident).reachableIdentifier String fullname = getFullName();
(method.substring(0, index), method.substring(index+1), if (fullname.length() > 0)
false); fullname += ".";
((ClassIdentifier) ident).preserveIdentifier if (loadOnDemand) {
(method.substring(0, index), method.substring(index+1)); /* Load all matching classes and packages */
Enumeration enum =
ClassInfo.getClassesAndPackages(getFullName());
while (enum.hasMoreElements()) {
String subclazz = (String)enum.nextElement();
if (loadedClasses.contains(subclazz))
continue;
String subFull = fullname + subclazz;
if (wildcard.startsWith(subFull + ".")) {
if (ClassInfo.isPackage(subFull)) {
Identifier ident = new PackageIdentifier
(bundle, this, subclazz, true);
loadedClasses.put(subclazz, ident);
} else {
ClassIdentifier ident = new ClassIdentifier
(bundle, this,
subclazz, ClassInfo.forName(subFull));
loadedClasses.put(subclazz, ident);
((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1)
ident.applyPreserveRule(bundle.preserveRule);
}
}
}
}
Enumeration enum = loadedClasses.elements();
while (enum.hasMoreElements()) {
Identifier ident = (Identifier) enum.nextElement();
if (wildcard.startsWith(ident.getFullName()+".")) {
if (ident instanceof PackageIdentifier)
((PackageIdentifier) ident)
.preserveMatchingIdentifier(wildcard);
else
((ClassIdentifier) ident)
.preserveMatchingIdentifier(wildcard);
}
} }
} }
} }
/** /**
* @return the full qualified name, including trailing dot. * @return the full qualified name.
*/ */
public String getFullName() { public String getFullName() {
if (parent != null) if (parent != null) {
return parent.getFullName() + getName() + "."; if (parent.getFullName().length() > 0)
return parent.getFullName() + "." + getName();
else else
return getName();
} else
return ""; return "";
} }

Loading…
Cancel
Save