1 16 package com.google.gwt.dev.js; 17 18 import com.google.gwt.dev.js.ast.JsName; 19 import com.google.gwt.dev.js.ast.JsProgram; 20 import com.google.gwt.dev.js.ast.JsRootScope; 21 import com.google.gwt.dev.js.ast.JsScope; 22 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 30 public class JsObfuscateNamer { 31 32 35 private static final char[] sBase64Chars = new char[] { 36 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 37 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 38 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 39 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', 40 '4', '5', '6', '7', '8', '9', '$', '_'}; 41 42 45 private static final char[] sIdentBuf = new char[6]; 46 47 public static void exec(JsProgram program) { 48 new JsObfuscateNamer(program).execImpl(); 49 } 50 51 private static String makeObfuscatedIdent(int id) { 52 int i = 0; 57 sIdentBuf[i++] = sBase64Chars[id & 0x1f]; 58 id >>= 5; 59 60 while (id != 0) { 63 sIdentBuf[i++] = sBase64Chars[id & 0x3f]; 64 id >>= 6; 65 } 66 67 return new String (sIdentBuf, 0, i); 68 } 69 70 73 private int maxChildId = 0; 74 private final JsProgram program; 75 76 public JsObfuscateNamer(JsProgram program) { 77 this.program = program; 78 } 79 80 private void execImpl() { 81 visit(program.getRootScope()); 82 } 83 84 private boolean isLegal(JsScope scope, String newIdent) { 85 if (JsKeywords.isKeyword(newIdent)) { 86 return false; 87 } 88 94 return (scope.findExistingUnobfuscatableName(newIdent) == null); 95 } 96 97 private void visit(JsScope scope) { 98 int mySiblingsMaxId = maxChildId; 100 101 105 maxChildId = 0; 106 List children = scope.getChildren(); 107 for (Iterator it = children.iterator(); it.hasNext();) { 108 visit((JsScope) it.next()); 109 } 110 112 JsRootScope rootScope = program.getRootScope(); 113 if (scope == rootScope) { 114 return; 115 } 116 117 int curId = maxChildId; 119 for (Iterator it = scope.getAllNames(); it.hasNext();) { 120 JsName name = (JsName) it.next(); 121 if (!name.isObfuscatable()) { 122 name.setShortIdent(name.getIdent()); 124 continue; 125 } 126 127 String newIdent; 128 while (true) { 129 newIdent = makeObfuscatedIdent(curId++); 131 if (isLegal(scope, newIdent)) { 132 break; 133 } 134 } 135 name.setShortIdent(newIdent); 136 } 137 138 maxChildId = Math.max(mySiblingsMaxId, curId); 139 } 140 } 141 | Popular Tags |