make heavy use of collection classes

some fixes in loadMatching and preserveMatching


git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@987 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent f2213c4994
commit 8bec672b88
  1. 191
      jode/jode/obfuscator/PackageIdentifier.java

@ -19,23 +19,33 @@
package jode.obfuscator; package jode.obfuscator;
import jode.GlobalOptions; import jode.GlobalOptions;
import jode.Obfuscator;
import jode.bytecode.ClassInfo; import jode.bytecode.ClassInfo;
import jode.bytecode.FieldInfo; import jode.bytecode.FieldInfo;
import jode.bytecode.MethodInfo; import jode.bytecode.MethodInfo;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.*;
import java.io.*; import java.io.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
///#ifdef JDK12
///import java.util.Map;
///import java.util.HashMap;
///import java.util.Iterator;
///#else
import jode.util.Map;
import jode.util.HashMap;
import jode.util.Iterator;
///#endif
public class PackageIdentifier extends Identifier { public class PackageIdentifier extends Identifier {
ClassBundle bundle; ClassBundle bundle;
PackageIdentifier parent; PackageIdentifier parent;
String name; String name;
boolean loadOnDemand; boolean loadOnDemand;
Hashtable loadedClasses; Map loadedClasses;
public PackageIdentifier(ClassBundle bundle, public PackageIdentifier(ClassBundle bundle,
PackageIdentifier parent, PackageIdentifier parent,
@ -44,7 +54,7 @@ public class PackageIdentifier extends Identifier {
this.bundle = bundle; this.bundle = bundle;
this.parent = parent; this.parent = parent;
this.name = name; this.name = name;
this.loadedClasses = new Hashtable(); this.loadedClasses = new HashMap();
if (loadOnDemand) if (loadOnDemand)
setLoadOnDemand(); setLoadOnDemand();
} }
@ -61,7 +71,7 @@ public class PackageIdentifier extends Identifier {
if (loadOnDemand) if (loadOnDemand)
return; return;
loadOnDemand = true; loadOnDemand = true;
if (!Obfuscator.shouldStrip) { if ((Main.stripping & Main.STRIP_UNREACH) == 0) {
// 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 =
@ -95,10 +105,10 @@ public class PackageIdentifier extends Identifier {
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
} else { } else {
Identifier ident = new ClassIdentifier Identifier ident = new ClassIdentifier
(bundle, this, subclazz, ClassInfo.forName(fullname)); (this, subclazz, ClassInfo.forName(fullname));
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} }
} }
@ -141,11 +151,11 @@ public class PackageIdentifier extends Identifier {
+ fullname); + fullname);
Thread.dumpStack(); Thread.dumpStack();
} else { } else {
ident = new ClassIdentifier(bundle, this, name, ident = new ClassIdentifier(this, name,
ClassInfo.forName(fullname)); ClassInfo.forName(fullname));
loadedClasses.put(name, ident); loadedClasses.put(name, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} }
} }
@ -155,9 +165,14 @@ public class PackageIdentifier extends Identifier {
PackageIdentifier pack = PackageIdentifier pack =
(PackageIdentifier) loadedClasses.get(subpack); (PackageIdentifier) loadedClasses.get(subpack);
if (pack == null) { if (pack == null) {
pack = new PackageIdentifier(bundle, this, String fullname = getFullName();
subpack, loadOnDemand); fullname = (fullname.length() > 0)
loadedClasses.put(subpack, pack); ? fullname + "."+ subpack : subpack;
if (ClassInfo.isPackage(fullname)) {
pack = new PackageIdentifier(bundle, this,
subpack, loadOnDemand);
loadedClasses.put(subpack, pack);
}
} }
if (pack != null) if (pack != null)
@ -174,30 +189,32 @@ public class PackageIdentifier extends Identifier {
if (ident == null) { if (ident == null) {
String fullname = getFullName(); String fullname = getFullName();
fullname = (fullname.length() > 0) fullname = (fullname.length() > 0)
? fullname + "."+ name ? fullname + "." + component
: name; : component;
if (ClassInfo.isPackage(fullname)) { if (ClassInfo.isPackage(fullname)) {
ident = new PackageIdentifier(bundle, this, ident = new PackageIdentifier(bundle, this,
component, loadOnDemand); component, loadOnDemand);
loadedClasses.put(component, ident); loadedClasses.put(component, ident);
} else if (!ClassInfo.exists(fullname)) { } else if (ClassInfo.exists(fullname)
GlobalOptions.err.println("Warning: Can't find class " && wildcard.matches(fullname)) {
+fullname);
} else if (wildcard.matches(fullname)) {
if (GlobalOptions.verboseLevel > 1) if (GlobalOptions.verboseLevel > 1)
GlobalOptions.err.println("loading Class " +fullname); GlobalOptions.err.println("loading Class " +fullname);
ident = new ClassIdentifier(bundle, this, name, ident = new ClassIdentifier(this, component,
ClassInfo.forName(fullname)); ClassInfo.forName(fullname));
loadedClasses.put(name, ident); loadedClasses.put(component, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} else {
GlobalOptions.err.println
("Warning: Can't find class/package " + fullname);
} }
} }
if (ident instanceof PackageIdentifier) { if (ident instanceof PackageIdentifier) {
if (wildcard.matches(ident.getFullName())) { if (wildcard.matches(ident.getFullName())) {
if (GlobalOptions.verboseLevel > 0) if (GlobalOptions.verboseLevel > 0)
GlobalOptions.err.println("loading Package " +ident.getFullName()); GlobalOptions.err.println("loading Package "
+ident.getFullName());
((PackageIdentifier) ident).setLoadOnDemand(); ((PackageIdentifier) ident).setLoadOnDemand();
} }
@ -213,7 +230,7 @@ public class PackageIdentifier extends Identifier {
ClassInfo.getClassesAndPackages(getFullName()); ClassInfo.getClassesAndPackages(getFullName());
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
String subclazz = (String)enum.nextElement(); String subclazz = (String)enum.nextElement();
if (loadedClasses.contains(subclazz)) if (loadedClasses.containsKey(subclazz))
continue; continue;
String subFull = fullname + subclazz; String subFull = fullname + subclazz;
@ -228,11 +245,10 @@ public class PackageIdentifier extends Identifier {
if (GlobalOptions.verboseLevel > 1) if (GlobalOptions.verboseLevel > 1)
GlobalOptions.err.println("loading Class " +subFull); GlobalOptions.err.println("loading Class " +subFull);
ClassIdentifier ident = new ClassIdentifier ClassIdentifier ident = new ClassIdentifier
(bundle, this, (this, subclazz, ClassInfo.forName(subFull));
subclazz, ClassInfo.forName(subFull));
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} }
} else if (ClassInfo.isPackage(subFull) } else if (ClassInfo.isPackage(subFull)
@ -242,9 +258,9 @@ public class PackageIdentifier extends Identifier {
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
} }
} }
enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator();
while (enum.hasMoreElements()) { i.hasNext(); ) {
Identifier ident = (Identifier) enum.nextElement(); Identifier ident = (Identifier) i.next();
if (ident instanceof PackageIdentifier) { if (ident instanceof PackageIdentifier) {
if (wildcard.matches(ident.getFullName())) if (wildcard.matches(ident.getFullName()))
((PackageIdentifier) ident).setLoadOnDemand(); ((PackageIdentifier) ident).setLoadOnDemand();
@ -256,10 +272,9 @@ public class PackageIdentifier extends Identifier {
} }
} }
public void applyPreserveRule(int preserveRule) { public void applyPreserveRule(ModifierMatcher preserveRule) {
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); )
while(enum.hasMoreElements()) ((Identifier) i.next()).applyPreserveRule(preserveRule);
((Identifier) enum.nextElement()).applyPreserveRule(preserveRule);
} }
public void reachableIdentifier(String fqn, boolean isVirtual) { public void reachableIdentifier(String fqn, boolean isVirtual) {
@ -298,21 +313,27 @@ public class PackageIdentifier extends Identifier {
fullname = (fullname.length() > 0) fullname = (fullname.length() > 0)
? fullname + "."+ component : component; ? fullname + "."+ component : component;
Identifier ident = (Identifier) loadedClasses.get(component); Identifier ident = (Identifier) loadedClasses.get(component);
if (ident == null && loadOnDemand) { if (ident == null) {
if (!loadOnDemand) {
GlobalOptions.err.println
("Warning: Didn't load package/class "+ fullname);
return;
}
if (ClassInfo.isPackage(fullname)) { if (ClassInfo.isPackage(fullname)) {
ident = new PackageIdentifier(bundle, this, ident = new PackageIdentifier(bundle, this,
component, loadOnDemand); component, loadOnDemand);
loadedClasses.put(component, ident); loadedClasses.put(component, ident);
} else if (!ClassInfo.exists(fullname)) { } else if (ClassInfo.exists(fullname)) {
GlobalOptions.err.println("Warning: Can't find class " ident = new ClassIdentifier(this, component,
+ fullname);
} else {
ident = new ClassIdentifier(bundle, this, name,
ClassInfo.forName(fullname)); ClassInfo.forName(fullname));
loadedClasses.put(name, ident); loadedClasses.put(component, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} else {
GlobalOptions.err.println("Warning: Can't find class "
+ fullname);
return;
} }
} }
if (wildcard.matches(fullname)) { if (wildcard.matches(fullname)) {
@ -338,30 +359,30 @@ public class PackageIdentifier extends Identifier {
ClassInfo.getClassesAndPackages(getFullName()); ClassInfo.getClassesAndPackages(getFullName());
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
String subclazz = (String)enum.nextElement(); String subclazz = (String)enum.nextElement();
if (loadedClasses.contains(subclazz)) if (loadedClasses.containsKey(subclazz))
continue; continue;
String subFull = fullname + subclazz; String subFull = fullname + subclazz;
if (wildcard.startsWith(subFull)) { if (wildcard.startsWith(subFull)) {
if (ClassInfo.isPackage(subFull)) { if (ClassInfo.isPackage(subFull)) {
System.err.println("is package: "+subFull);
Identifier ident = new PackageIdentifier Identifier ident = new PackageIdentifier
(bundle, this, subclazz, true); (bundle, this, subclazz, true);
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
} else { } else {
ClassIdentifier ident = new ClassIdentifier ClassIdentifier ident = new ClassIdentifier
(bundle, this, (this, subclazz, ClassInfo.forName(subFull));
subclazz, ClassInfo.forName(subFull));
loadedClasses.put(subclazz, ident); loadedClasses.put(subclazz, ident);
((ClassIdentifier) ident).initClass(); ((ClassIdentifier) ident).initClass();
if (bundle.preserveRule != -1) if (bundle.preserveRule != null)
ident.applyPreserveRule(bundle.preserveRule); ident.applyPreserveRule(bundle.preserveRule);
} }
} }
} }
} }
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator();
while (enum.hasMoreElements()) { i.hasNext(); ) {
Identifier ident = (Identifier) enum.nextElement(); Identifier ident = (Identifier) i.next();
if (wildcard.matches(ident.getFullName())) { if (wildcard.matches(ident.getFullName())) {
if (GlobalOptions.verboseLevel > 1) if (GlobalOptions.verboseLevel > 1)
GlobalOptions.err.println("Preserving "+ident); GlobalOptions.err.println("Preserving "+ident);
@ -420,11 +441,11 @@ public class PackageIdentifier extends Identifier {
return className; return className;
} }
public void buildTable(int renameRule) { public void buildTable(Renamer renameRule) {
super.buildTable(renameRule); super.buildTable(renameRule);
Enumeration enum = loadedClasses.elements(); loadOnDemand = false;
while (enum.hasMoreElements()) { for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
Identifier ident = (Identifier) enum.nextElement(); Identifier ident = (Identifier) i.next();
if (ident instanceof ClassIdentifier) if (ident instanceof ClassIdentifier)
((ClassIdentifier) ident).buildTable(renameRule); ((ClassIdentifier) ident).buildTable(renameRule);
else else
@ -433,9 +454,8 @@ public class PackageIdentifier extends Identifier {
} }
public void doTransformations() { public void doTransformations() {
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier) i.next();
Identifier ident = (Identifier) enum.nextElement();
if (ident instanceof ClassIdentifier) { if (ident instanceof ClassIdentifier) {
((ClassIdentifier) ident).doTransformations(); ((ClassIdentifier) ident).doTransformations();
} else } else
@ -443,24 +463,24 @@ public class PackageIdentifier extends Identifier {
} }
} }
public void readTable(Hashtable table) { public void readTable(Map table) {
if (parent != null) if (parent != null)
setAlias((String) table.get(getFullName())); setAlias((String) table.get(getFullName()));
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier) i.next();
Identifier ident = (Identifier) enum.nextElement(); if ((Main.stripping & Main.STRIP_UNREACH) == 0
if (!Obfuscator.shouldStrip || ident.isReachable()) || ident.isReachable())
ident.readTable(table); ident.readTable(table);
} }
} }
public void writeTable(Hashtable table) { public void writeTable(Map table) {
if (parent != null) if (parent != null)
table.put(getFullAlias(), getName()); table.put(getFullAlias(), getName());
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier) i.next();
Identifier ident = (Identifier) enum.nextElement(); if ((Main.stripping & Main.STRIP_UNREACH) == 0
if (!Obfuscator.shouldStrip || ident.isReachable()) || ident.isReachable())
ident.writeTable(table); ident.writeTable(table);
} }
} }
@ -477,11 +497,15 @@ public class PackageIdentifier extends Identifier {
return "package"; return "package";
} }
public Iterator getChilds() {
return loadedClasses.values().iterator();
}
public void storeClasses(ZipOutputStream zip) { public void storeClasses(ZipOutputStream zip) {
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier) i.next();
Identifier ident = (Identifier) enum.nextElement(); if ((Main.stripping & Main.STRIP_UNREACH) != 0
if (Obfuscator.shouldStrip && !ident.isReachable()) { && !ident.isReachable()) {
if (GlobalOptions.verboseLevel > 4) if (GlobalOptions.verboseLevel > 4)
GlobalOptions.err.println("Class/Package " GlobalOptions.err.println("Class/Package "
+ ident.getFullName() + ident.getFullName()
@ -495,7 +519,8 @@ public class PackageIdentifier extends Identifier {
String filename = ident.getFullAlias().replace('.','/') String filename = ident.getFullAlias().replace('.','/')
+ ".class"; + ".class";
zip.putNextEntry(new ZipEntry(filename)); zip.putNextEntry(new ZipEntry(filename));
DataOutputStream out = new DataOutputStream(zip); DataOutputStream out = new DataOutputStream
(new BufferedOutputStream(zip));
((ClassIdentifier) ident).storeClass(out); ((ClassIdentifier) ident).storeClass(out);
out.flush(); out.flush();
zip.closeEntry(); zip.closeEntry();
@ -515,10 +540,10 @@ public class PackageIdentifier extends Identifier {
GlobalOptions.err.println("Could not create directory " GlobalOptions.err.println("Could not create directory "
+newDest.getPath()+", check permissions."); +newDest.getPath()+", check permissions.");
} }
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier) i.next();
Identifier ident = (Identifier) enum.nextElement(); if ((Main.stripping & Main.STRIP_UNREACH) != 0
if (Obfuscator.shouldStrip && !ident.isReachable()) { && !ident.isReachable()) {
if (GlobalOptions.verboseLevel > 4) if (GlobalOptions.verboseLevel > 4)
GlobalOptions.err.println("Class/Package " GlobalOptions.err.println("Class/Package "
+ ident.getFullName() + ident.getFullName()
@ -538,7 +563,8 @@ public class PackageIdentifier extends Identifier {
// return; // return;
// } // }
DataOutputStream out = new DataOutputStream DataOutputStream out = new DataOutputStream
(new FileOutputStream(file)); (new BufferedOutputStream
(new FileOutputStream(file)));
((ClassIdentifier) ident).storeClass(out); ((ClassIdentifier) ident).storeClass(out);
out.close(); out.close();
} catch (java.io.IOException ex) { } catch (java.io.IOException ex) {
@ -554,18 +580,19 @@ public class PackageIdentifier extends Identifier {
return (parent == null) ? "base package" : getFullName(); return (parent == null) ? "base package" : getFullName();
} }
public boolean contains(String newAlias) { public boolean contains(String newAlias, Identifier except) {
Enumeration enum = loadedClasses.elements(); for (Iterator i = loadedClasses.values().iterator(); i.hasNext(); ) {
while (enum.hasMoreElements()) { Identifier ident = (Identifier)i.next();
Identifier ident = (Identifier)enum.nextElement(); if (((Main.stripping & Main.STRIP_UNREACH) == 0
if ((!Obfuscator.shouldStrip || ident.isReachable()) || ident.isReachable())
&& ident != except
&& ident.getAlias().equals(newAlias)) && ident.getAlias().equals(newAlias))
return true; return true;
} }
return false; return false;
} }
public boolean conflicting(String newAlias, boolean strong) { public boolean conflicting(String newAlias) {
return parent.contains(newAlias); return parent.contains(newAlias, this);
} }
} }

Loading…
Cancel
Save