KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > JsObfuscateNamer


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * A namer that uses short, unrecognizable idents to minimize generated code
28  * size.
29  */

30 public class JsObfuscateNamer {
31
32   /**
33    * A lookup table of base-64 chars we use to encode idents.
34    */

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   /**
43    * A temp buffer big enough to hold at least 32 bits worth of base-64 chars.
44    */

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 JavaDoc makeObfuscatedIdent(int id) {
52     // Use base-32 for the first character of the identifier,
53
// so that we don't use any numbers (which are illegal at
54
// the beginning of an identifier).
55
//
56
int i = 0;
57     sIdentBuf[i++] = sBase64Chars[id & 0x1f];
58     id >>= 5;
59
60     // Use base-64 for the rest of the identifier.
61
//
62
while (id != 0) {
63       sIdentBuf[i++] = sBase64Chars[id & 0x3f];
64       id >>= 6;
65     }
66
67     return new String JavaDoc(sIdentBuf, 0, i);
68   }
69
70   /**
71    * Communicates to a parent scope the maximum id used by any of its children.
72    */

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 JavaDoc newIdent) {
85     if (JsKeywords.isKeyword(newIdent)) {
86       return false;
87     }
88     /*
89      * Never obfuscate a name into an identifier that conflicts with an existing
90      * unobfuscatable name! It's okay if it conflicts with an existing
91      * obfuscatable name, since that name will get obfuscated to something else
92      * anyway.
93      */

94     return (scope.findExistingUnobfuscatableName(newIdent) == null);
95   }
96
97   private void visit(JsScope scope) {
98     // Save off the maxChildId which is currently being computed for my parent.
99
int mySiblingsMaxId = maxChildId;
100
101     /*
102      * Visit my children first. Reset maxChildId so that my children will get
103      * a clean slate: I do not communicate to my children.
104      */

105     maxChildId = 0;
106     List JavaDoc children = scope.getChildren();
107     for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
108       visit((JsScope) it.next());
109     }
110     // maxChildId is now the max of all of my children's ids
111

112     JsRootScope rootScope = program.getRootScope();
113     if (scope == rootScope) {
114       return;
115     }
116
117     // Visit my idents.
118
int curId = maxChildId;
119     for (Iterator JavaDoc it = scope.getAllNames(); it.hasNext();) {
120       JsName name = (JsName) it.next();
121       if (!name.isObfuscatable()) {
122         // Unobfuscatable names become themselves.
123
name.setShortIdent(name.getIdent());
124         continue;
125       }
126
127       String JavaDoc newIdent;
128       while (true) {
129         // Get the next possible obfuscated name
130
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