|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2007 java2script.org and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Zhou Renjian - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package net.sf.j2s.core.astvisitors; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Iterator; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * Final variables inside anonymous class is a big thing for Java2Script |
| 20 | + * compiler. |
| 21 | + * |
| 22 | + * @author zhou renjian |
| 23 | + * |
| 24 | + * 2006-12-3 |
| 25 | + */ |
| 26 | +public class VariableAdapter extends VisitorAdapter { |
| 27 | + |
| 28 | + /** |
| 29 | + * FinalVariable that is used to record variable state, which will provide |
| 30 | + * information for compiler to decide the generated name in *.js. |
| 31 | + * |
| 32 | + * @author zhou renjian |
| 33 | + * |
| 34 | + * 2006-12-6 |
| 35 | + */ |
| 36 | + public static class FinalVariable { |
| 37 | + |
| 38 | + /** |
| 39 | + * Level of the block |
| 40 | + */ |
| 41 | + public int blockLevel; |
| 42 | + |
| 43 | + /** |
| 44 | + * Final variable may be in a very deep anonymous class |
| 45 | + */ |
| 46 | + public String methodScope; |
| 47 | + |
| 48 | + /** |
| 49 | + * Variable name that is defined in Java sources |
| 50 | + */ |
| 51 | + public String variableName; |
| 52 | + |
| 53 | + /** |
| 54 | + * Variable name that is to be generated in the compiled *.js |
| 55 | + */ |
| 56 | + public String toVariableName; |
| 57 | + |
| 58 | + public FinalVariable(int blockLevel, String variableName, String methodScope) { |
| 59 | + super(); |
| 60 | + this.blockLevel = blockLevel; |
| 61 | + this.variableName = variableName; |
| 62 | + this.methodScope = methodScope; |
| 63 | + } |
| 64 | + |
| 65 | + public String toString() { |
| 66 | + return variableName + ":" + variableName; |
| 67 | + } |
| 68 | + |
| 69 | + public int hashCode() { |
| 70 | + final int prime = 31; |
| 71 | + int result = 1; |
| 72 | + result = prime * result + blockLevel; |
| 73 | + result = prime * result |
| 74 | + + ((methodScope == null) ? 0 : methodScope.hashCode()); |
| 75 | + result = prime * result |
| 76 | + + ((toVariableName == null) ? 0 : toVariableName.hashCode()); |
| 77 | + result = prime * result |
| 78 | + + ((variableName == null) ? 0 : variableName.hashCode()); |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + public boolean equals(Object obj) { |
| 83 | + if (this == obj) |
| 84 | + return true; |
| 85 | + if (obj == null || getClass() != obj.getClass()) |
| 86 | + return false; |
| 87 | + final FinalVariable other = (FinalVariable) obj; |
| 88 | + if (blockLevel != other.blockLevel) |
| 89 | + return false; |
| 90 | + if (methodScope == null) { |
| 91 | + if (other.methodScope != null) |
| 92 | + return false; |
| 93 | + } else if (!methodScope.equals(other.methodScope)) |
| 94 | + return false; |
| 95 | + if (toVariableName == null) { |
| 96 | + if (other.toVariableName != null) |
| 97 | + return false; |
| 98 | + } else if (!toVariableName.equals(other.toVariableName)) |
| 99 | + return false; |
| 100 | + if (variableName == null) { |
| 101 | + if (other.variableName != null) |
| 102 | + return false; |
| 103 | + } else if (!variableName.equals(other.variableName)) |
| 104 | + return false; |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Final variables only make senses (need "this.$finals[...]") inside anonymous |
| 112 | + * class. |
| 113 | + */ |
| 114 | + public boolean isAnonymousClass = true; |
| 115 | + |
| 116 | + /** |
| 117 | + * List of variables that are declared as final. |
| 118 | + */ |
| 119 | + public List<FinalVariable> finalVars = new ArrayList<FinalVariable>(); |
| 120 | + |
| 121 | + /** |
| 122 | + * Normal (non-final) variables may be affected by final variable names. |
| 123 | + */ |
| 124 | + public List<FinalVariable> normalVars = new ArrayList<FinalVariable>(); |
| 125 | + |
| 126 | + /** |
| 127 | + * Only those final variables that are referenced inside anonymous class |
| 128 | + * need to be passed into anonymous class. |
| 129 | + */ |
| 130 | + public List<FinalVariable> visitedVars = new ArrayList<FinalVariable>(); |
| 131 | + |
| 132 | + public String getNormalVariableName(String name) { |
| 133 | + for (int i = normalVars.size() - 1; i >= 0; i--) { |
| 134 | + String var = normalVars.get(i).variableName; |
| 135 | + if (name.equals(var)) |
| 136 | + return var; |
| 137 | + } |
| 138 | + return name; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Generated final variable list for anonymous class creation. |
| 143 | + * |
| 144 | + * @param list |
| 145 | + * @param seperator |
| 146 | + * @param scope |
| 147 | + * @return |
| 148 | + */ |
| 149 | + public static String listFinalVariables(List<FinalVariable> list, String seperator, String scope) { |
| 150 | + if (list.size() == 0) { |
| 151 | + return "null"; |
| 152 | + } |
| 153 | + StringBuffer buf = new StringBuffer(); |
| 154 | + buf.append("{"); |
| 155 | + for (Iterator<FinalVariable> iter = list.iterator(); iter.hasNext();) { |
| 156 | + FinalVariable fv = iter.next(); |
| 157 | + String name = fv.variableName; |
| 158 | + if (fv.toVariableName != null) { |
| 159 | + name = fv.toVariableName; |
| 160 | + } |
| 161 | + //buf.append("\""); |
| 162 | + buf.append(name); |
| 163 | + //buf.append("\": "); |
| 164 | + buf.append(": "); |
| 165 | + String methodScope = fv.methodScope; |
| 166 | + if (methodScope == null && scope == null) { |
| 167 | + buf.append(name); |
| 168 | + } else if (methodScope == null || scope == null) { |
| 169 | + buf.append("this.$finals." + name); |
| 170 | + } else if (methodScope.equals(scope)) { |
| 171 | + buf.append(name); |
| 172 | + } else { |
| 173 | + buf.append("this.$finals." + name); |
| 174 | + } |
| 175 | + if (iter.hasNext()) { |
| 176 | + buf.append(seperator); |
| 177 | + } |
| 178 | + } |
| 179 | + buf.append("}"); |
| 180 | + return buf.toString(); |
| 181 | + } |
| 182 | + |
| 183 | + public List<FinalVariable> getVariableList(char fvn) { |
| 184 | + switch (fvn) { |
| 185 | + case 'f': |
| 186 | + return finalVars; |
| 187 | + case 'v': |
| 188 | + return visitedVars; |
| 189 | + default: |
| 190 | + return normalVars; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments