1 11 package org.eclipse.jdt.internal.compiler.lookup; 12 13 import org.eclipse.jdt.internal.compiler.ast.ASTNode; 14 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 15 import org.eclipse.jdt.internal.compiler.ast.Annotation; 16 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; 17 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 18 import org.eclipse.jdt.internal.compiler.impl.Constant; 19 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext; 20 21 public class LocalVariableBinding extends VariableBinding { 22 23 public int resolvedPosition; 25 public static final int UNUSED = 0; 26 public static final int USED = 1; 27 public static final int FAKE_USED = 2; 28 public int useFlag; 30 public BlockScope declaringScope; public LocalDeclaration declaration; 33 public int[] initializationPCs; 34 public int initializationCount = 0; 35 36 public LocalVariableBinding(char[] name, TypeBinding type, int modifiers, boolean isArgument) { 40 super(name, type, modifiers, isArgument ? Constant.NotAConstant : null); 41 if (isArgument) this.tagBits |= TagBits.IsArgument; 42 } 43 44 public LocalVariableBinding(LocalDeclaration declaration, TypeBinding type, int modifiers, boolean isArgument) { 46 47 this(declaration.name, type, modifiers, isArgument); 48 this.declaration = declaration; 49 } 50 51 54 public final int kind() { 55 56 return LOCAL; 57 } 58 59 63 public char[] computeUniqueKey(boolean isLeaf) { 64 StringBuffer buffer = new StringBuffer (); 65 66 BlockScope scope = this.declaringScope; 68 if (scope != null) { 69 MethodScope methodScope = scope instanceof MethodScope ? (MethodScope) scope : scope.enclosingMethodScope(); 71 ReferenceContext referenceContext = methodScope.referenceContext; 72 if (referenceContext instanceof AbstractMethodDeclaration) { 73 MethodBinding methodBinding = ((AbstractMethodDeclaration) referenceContext).binding; 74 if (methodBinding != null) { 75 buffer.append(methodBinding.computeUniqueKey(false)); 76 } 77 } else if (referenceContext instanceof TypeDeclaration) { 78 TypeBinding typeBinding = ((TypeDeclaration) referenceContext).binding; 79 if (typeBinding != null) { 80 buffer.append(typeBinding.computeUniqueKey(false)); 81 } 82 } 83 84 getScopeKey(scope, buffer); 86 } 87 buffer.append('#'); 89 buffer.append(this.name); 90 91 int length = buffer.length(); 92 char[] uniqueKey = new char[length]; 93 buffer.getChars(0, length, uniqueKey, 0); 94 return uniqueKey; 95 } 96 97 public AnnotationBinding[] getAnnotations() { 98 if (this.declaringScope == null) { 99 if ((this.tagBits & TagBits.AnnotationResolved) != 0) { 100 if (this.declaration == null) { 102 return Binding.NO_ANNOTATIONS; 103 } 104 Annotation[] annotations = this.declaration.annotations; 105 if (annotations != null) { 106 int length = annotations.length; 107 AnnotationBinding[] annotationBindings = new AnnotationBinding[length]; 108 for (int i = 0; i < length; i++) { 109 AnnotationBinding compilerAnnotation = annotations[i].getCompilerAnnotation(); 110 if (compilerAnnotation == null) { 111 return Binding.NO_ANNOTATIONS; 112 } 113 annotationBindings[i] = compilerAnnotation; 114 } 115 return annotationBindings; 116 } 117 } 118 return Binding.NO_ANNOTATIONS; 119 } 120 SourceTypeBinding sourceType = this.declaringScope.enclosingSourceType(); 121 if (sourceType == null) 122 return Binding.NO_ANNOTATIONS; 123 124 AnnotationBinding[] annotations = sourceType.retrieveAnnotations(this); 125 if ((this.tagBits & TagBits.AnnotationResolved) == 0) { 126 if (((this.tagBits & TagBits.IsArgument) != 0) && this.declaration != null) { 127 Annotation[] annotationNodes = declaration.annotations; 128 if (annotationNodes != null) { 129 int length = annotationNodes.length; 130 ASTNode.resolveAnnotations(this.declaringScope, annotationNodes, this); 131 annotations = new AnnotationBinding[length]; 132 for (int i = 0; i < length; i++) 133 annotations[i] = new AnnotationBinding(annotationNodes[i]); 134 setAnnotations(annotations); 135 } 136 } 137 } 138 return annotations; 139 } 140 141 private void getScopeKey(BlockScope scope, StringBuffer buffer) { 142 int scopeIndex = scope.scopeIndex(); 143 if (scopeIndex != -1) { 144 getScopeKey((BlockScope)scope.parent, buffer); 145 buffer.append('#'); 146 buffer.append(scopeIndex); 147 } 148 } 149 150 public boolean isSecret() { 152 153 return declaration == null && (this.tagBits & TagBits.IsArgument) == 0; 154 } 155 156 public void recordInitializationEndPC(int pc) { 157 158 if (initializationPCs[((initializationCount - 1) << 1) + 1] == -1) 159 initializationPCs[((initializationCount - 1) << 1) + 1] = pc; 160 } 161 162 public void recordInitializationStartPC(int pc) { 163 164 if (initializationPCs == null) return; 165 if (initializationCount > 0) { 166 int previousEndPC = initializationPCs[ ((initializationCount - 1) << 1) + 1]; 167 if (previousEndPC == -1) { 169 return; 170 } 171 if (previousEndPC == pc) { 173 initializationPCs[ ((initializationCount - 1) << 1) + 1] = -1; return; 175 } 176 } 177 int index = initializationCount << 1; 178 if (index == initializationPCs.length) { 179 System.arraycopy(initializationPCs, 0, (initializationPCs = new int[initializationCount << 2]), 0, index); 180 } 181 initializationPCs[index] = pc; 182 initializationPCs[index + 1] = -1; 183 initializationCount++; 184 } 185 186 public void setAnnotations(AnnotationBinding[] annotations) { 187 if (this.declaringScope == null) return; 188 189 SourceTypeBinding sourceType = this.declaringScope.enclosingSourceType(); 190 if (sourceType != null) 191 sourceType.storeAnnotations(this, annotations); 192 } 193 194 public String toString() { 195 196 String s = super.toString(); 197 switch (useFlag){ 198 case USED: 199 s += "[pos: " + String.valueOf(resolvedPosition) + "]"; break; 201 case UNUSED: 202 s += "[pos: unused]"; break; 204 case FAKE_USED: 205 s += "[pos: fake_used]"; break; 207 } 208 s += "[id:" + String.valueOf(id) + "]"; if (initializationCount > 0) { 210 s += "[pc: "; for (int i = 0; i < initializationCount; i++) { 212 if (i > 0) 213 s += ", "; s += String.valueOf(initializationPCs[i << 1]) + "-" + ((initializationPCs[(i << 1) + 1] == -1) ? "?" : String.valueOf(initializationPCs[(i<< 1) + 1])); } 216 s += "]"; } 218 return s; 219 } 220 } 221 | Popular Tags |