KickJava   Java API By Example, From Geeks To Geeks.

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


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 long, fully qualified names for maximum unambiguous
28  * debuggability.
29  */

30 public class JsVerboseNamer {
31
32   public static void exec(JsProgram program) {
33     new JsVerboseNamer(program).execImpl();
34   }
35
36   private final JsProgram program;
37
38   public JsVerboseNamer(JsProgram program) {
39     this.program = program;
40   }
41
42   private void execImpl() {
43     visit(program.getRootScope());
44   }
45
46   private boolean isLegal(JsScope scope, String JavaDoc newIdent) {
47     // only keywords are forbidden
48
return !JsKeywords.isKeyword(newIdent);
49   }
50
51   private void visit(JsScope scope) {
52     // Visit children.
53
List JavaDoc children = scope.getChildren();
54     for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
55       visit((JsScope) it.next());
56     }
57
58     JsRootScope rootScope = program.getRootScope();
59     if (scope == rootScope) {
60       return;
61     }
62
63     // Visit all my idents.
64
for (Iterator JavaDoc it = scope.getAllNames(); it.hasNext();) {
65       JsName name = (JsName) it.next();
66       if (!name.isObfuscatable()) {
67         // Unobfuscatable names become themselves.
68
name.setShortIdent(name.getIdent());
69         continue;
70       }
71
72       String JavaDoc fullIdent = name.getIdent();
73       if (!isLegal(scope, fullIdent)) {
74         String JavaDoc checkIdent = fullIdent;
75         for (int i = 0; true; ++i) {
76           checkIdent = fullIdent + "_" + i;
77           if (isLegal(scope, checkIdent)) {
78             break;
79           }
80         }
81         name.setShortIdent(checkIdent);
82       } else {
83         // set each name's short ident to its full ident
84
name.setShortIdent(fullIdent);
85       }
86     }
87   }
88 }
89
Popular Tags