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.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 31 public class JsPrettyNamer { 32 33 public static void exec(JsProgram program) { 34 new JsPrettyNamer(program).execImpl(); 35 } 36 37 40 private Set childIdents = null; 41 42 private final JsProgram program; 43 44 public JsPrettyNamer(JsProgram program) { 45 this.program = program; 46 } 47 48 private void execImpl() { 49 visit(program.getRootScope()); 50 } 51 52 private boolean isLegal(JsScope scope, Set childIdents, String newIdent) { 53 if (JsKeywords.isKeyword(newIdent)) { 54 return false; 55 } 56 if (childIdents.contains(newIdent)) { 57 return false; 59 } 60 65 return (scope.findExistingUnobfuscatableName(newIdent) == null); 66 } 67 68 private void visit(JsScope scope) { 69 Set myChildIdents = childIdents; 71 72 76 childIdents = new HashSet (); 77 List children = scope.getChildren(); 78 for (Iterator it = children.iterator(); it.hasNext();) { 79 visit((JsScope) it.next()); 80 } 81 82 JsRootScope rootScope = program.getRootScope(); 83 if (scope == rootScope) { 84 return; 85 } 86 87 for (Iterator it = scope.getAllNames(); it.hasNext();) { 89 JsName name = (JsName) it.next(); 90 if (!name.isObfuscatable()) { 91 name.setShortIdent(name.getIdent()); 93 continue; 94 } 95 96 String newIdent = name.getShortIdent(); 97 if (!isLegal(scope, childIdents, newIdent)) { 98 String checkIdent = newIdent; 99 for (int i = 0; true; ++i) { 100 checkIdent = newIdent + "_" + i; 101 if (isLegal(scope, childIdents, checkIdent)) { 102 break; 103 } 104 } 105 name.setShortIdent(checkIdent); 106 } else { 107 } 109 childIdents.add(name.getShortIdent()); 110 } 111 myChildIdents.addAll(childIdents); 112 childIdents = myChildIdents; 113 } 114 } 115 | Popular Tags |