1 16 package com.google.gwt.dev.jjs.impl; 17 18 import com.google.gwt.dev.jjs.ast.Context; 19 import com.google.gwt.dev.jjs.ast.JClassType; 20 import com.google.gwt.dev.jjs.ast.JInterfaceType; 21 import com.google.gwt.dev.jjs.ast.JMethod; 22 import com.google.gwt.dev.jjs.ast.JProgram; 23 import com.google.gwt.dev.jjs.ast.JReferenceType; 24 import com.google.gwt.dev.jjs.ast.JVisitor; 25 import com.google.gwt.dev.jjs.ast.js.JsniMethod; 26 27 import java.util.HashSet ; 28 import java.util.Set ; 29 30 35 public class MethodAndClassFinalizer { 36 37 47 private class FinalizeVisitor extends JVisitor { 48 49 private boolean didChange = false; 50 51 public boolean didChange() { 52 return didChange; 53 } 54 55 public boolean visit(JClassType x, Context ctx) { 57 if (!x.isFinal() && !isSubclassed.contains(x)) { 58 x.setFinal(true); 59 didChange = true; 60 } 61 for (int i = 0; i < x.methods.size(); ++i) { 62 JMethod method = (JMethod) x.methods.get(i); 63 accept(method); 64 } 65 return false; 66 } 67 68 public boolean visit(JInterfaceType x, Context ctx) { 70 for (int i = 0; i < x.methods.size(); ++i) { 71 JMethod method = (JMethod) x.methods.get(i); 72 accept(method); 73 } 74 return false; 75 } 76 77 public boolean visit(JMethod x, Context ctx) { 79 if (!x.isFinal() && !isOverriden.contains(x)) { 80 x.setFinal(true); 81 didChange = true; 82 } 83 return false; 84 } 85 86 public boolean visit(JsniMethod x, Context ctx) { 88 return visit((JMethod) x, ctx); 89 } 90 } 91 94 private class MarkVisitor extends JVisitor { 95 96 public boolean visit(JClassType x, Context ctx) { 98 if (x.extnds != null) { 99 isSubclassed.add(x.extnds); 100 } 101 102 for (int i = 0; i < x.methods.size(); ++i) { 103 JMethod method = (JMethod) x.methods.get(i); 104 accept(method); 105 } 106 return false; 107 } 108 109 public boolean visit(JInterfaceType x, Context ctx) { 111 for (int i = 0; i < x.methods.size(); ++i) { 112 JMethod method = (JMethod) x.methods.get(i); 113 accept(method); 114 } 115 return false; 116 } 117 118 public boolean visit(JMethod x, Context ctx) { 120 for (int i = 0; i < x.overrides.size(); ++i) { 121 JMethod it = (JMethod) x.overrides.get(i); 122 isOverriden.add(it); 123 } 124 return false; 125 } 126 127 public boolean visit(JProgram x, Context ctx) { 129 for (int i = 0; i < x.getDeclaredTypes().size(); ++i) { 130 JReferenceType type = (JReferenceType) x.getDeclaredTypes().get(i); 131 accept(type); 132 } 133 return false; 134 } 135 136 public boolean visit(JsniMethod x, Context ctx) { 138 return visit((JMethod) x, ctx); 139 } 140 } 141 142 public static boolean exec(JProgram program) { 143 return new MethodAndClassFinalizer().execImpl(program); 144 } 145 146 private final Set isOverriden = new HashSet (); 147 148 private final Set isSubclassed = new HashSet (); 149 150 private MethodAndClassFinalizer() { 151 } 152 153 private boolean execImpl(JProgram program) { 154 MarkVisitor marker = new MarkVisitor(); 155 marker.accept(program); 156 157 FinalizeVisitor finalizer = new FinalizeVisitor(); 158 finalizer.accept(program); 159 return finalizer.didChange(); 160 } 161 162 } 163 | Popular Tags |