| 1 20 21 package com.tonicsystems.jarjar; 22 23 import java.util.*; 24 import org.objectweb.asm.Attribute; 25 26 class RulesImpl implements Rules 27 { 28 private static final String RESOURCE_SUFFIX = "RESOURCE"; 29 30 private Wildcard[] wildcards; 31 private HashMap cache = new HashMap(); 32 private boolean verbose; 33 34 public RulesImpl(List ruleList, boolean verbose) { 35 this.verbose = verbose; 36 wildcards = PatternElement.createWildcards(ruleList); 37 } 38 39 public String fixPath(String path) { 40 int slash = path.lastIndexOf('/'); 41 String end; 42 if (slash < 0) { 43 end = path; 44 path = RESOURCE_SUFFIX; 45 } else { 46 end = path.substring(slash + 1); 47 path = path.substring(0, slash + 1) + RESOURCE_SUFFIX; 48 } 49 boolean absolute = path.startsWith("/"); 50 if (absolute) 51 path = path.substring(1); 52 path = fixName(path); 53 if (absolute) 54 path = "/" + path; 55 path = path.substring(0, path.length() - RESOURCE_SUFFIX.length()) + end; 56 return path; 57 } 58 59 public String fixDesc(String desc) { 60 return fixDesc(desc, false); 61 } 62 63 private String fixDesc(String desc, boolean allowGenerics) { 64 if (desc.charAt(desc.length() - 1) != ';') 65 return desc; 66 String value = (String )cache.get(desc); 67 if (value == null) { 68 if (allowGenerics && (desc.charAt(desc.length() - 2) == '>')) { 69 int lt = desc.indexOf('<'); 71 String main = replaceHelper(desc.substring(0, lt) + ";", Wildcard.STYLE_DESC); 72 String param = replaceHelper(desc.substring(lt + 1, desc.length() - 2), Wildcard.STYLE_DESC); 73 value = main.substring(0, main.length() - 1) + "<" + param + ">;"; 74 } else { 75 value = replaceHelper(desc, Wildcard.STYLE_DESC); 76 } 77 cache.put(desc, value); 78 } 79 return value; 80 } 81 82 private String replaceHelper(String value, int style) { 83 for (int i = 0; i < wildcards.length; i++) { 84 String test = wildcards[i].replace(value, style); 85 if (test != null) 86 return test; 87 } 88 return value; 89 } 90 91 public String fixName(String name) { 92 if (name == null) 93 return null; 94 String desc = fixDesc("L" + name + ";"); 95 return desc.substring(1, desc.length() - 1); 96 } 97 98 public String fixMethodDesc(String desc) { 99 return fixMethodDesc(desc, false); 100 } 101 102 private String fixMethodDesc(String desc, boolean allowGenerics) { 103 String value = (String )cache.get(desc); 104 if (value == null) { 105 if (desc.indexOf('L') < 0) { 106 value = desc; 107 } else { 108 StringBuffer sb = new StringBuffer (); 109 sb.append('('); 110 int end = desc.lastIndexOf(')'); 111 for (int i = 1; i < end; i++) { 112 char c = desc.charAt(i); 113 if (c == 'L') { 114 for (int j = i + 1; j < end; j++) { 115 if (desc.charAt(j) == ';') { 116 if (allowGenerics && j + 1 < end && desc.charAt(j + 1) == '>') 117 j += 2; 118 sb.append(fixDesc(desc.substring(i, j + 1), allowGenerics)); 119 i = j; 120 break; 121 } 122 } 123 } else { 124 sb.append(c); 125 } 126 } 127 sb.append(')'); 128 sb.append(fixDesc(desc.substring(end + 1), allowGenerics)); 129 value = sb.toString(); 130 } 131 cache.put(desc, value); 132 } 133 return value; 134 } 135 136 public String fixString(String className, String value) { 137 String newValue = fixClassForName(value); 138 if (newValue.equals(value)) 139 newValue = fixPath(value); 140 if (newValue.equals(value)) 141 newValue = replaceHelper(newValue, Wildcard.STYLE_IDENTIFIER); 142 if (verbose && !newValue.equals(value)) 143 System.err.println("Changed " + className + " \"" + value + "\" -> \"" + newValue + "\""); 144 return newValue; 145 } 146 147 private String fixClassForName(String value) 148 { 149 if (value.indexOf('.') >= 0) { 150 String desc1 = value.replace('.', '/'); 151 String desc2 = fixDesc(desc1); 152 if (!desc2.equals(desc1)) 153 return desc2.replace('/', '.'); 154 } 155 return value; 156 } 157 158 public Attribute fixAttribute(Attribute attr) { 159 return attr; 161 } 162 163 public String fixSignature(String signature) { 164 if (signature == null) 165 return null; 166 if (signature.charAt(0) == '(') 167 return fixMethodDesc(signature, true); 168 return fixDesc(signature, true); 169 } 170 } 171 | Popular Tags |