Skip to content

Commit dba7539

Browse files
committed
Merge pull request #32 from lyle-luan/master
solve bug when @WeakOuter annotate inner class
2 parents d75d8a8 + 573779e commit dba7539

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

translator/src/main/java/com/google/devtools/j2objc/translate/OuterReferenceResolver.java

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
import com.google.devtools.j2objc.util.BindingUtil;
4545

4646
import com.google.devtools.j2objc.util.ErrorUtil;
47-
import org.eclipse.jdt.core.dom.IMethodBinding;
48-
import org.eclipse.jdt.core.dom.ITypeBinding;
49-
import org.eclipse.jdt.core.dom.IVariableBinding;
50-
import org.eclipse.jdt.core.dom.Modifier;
47+
import org.eclipse.jdt.core.dom.*;
5148

5249
import java.util.ArrayList;
5350
import java.util.List;
@@ -209,47 +206,54 @@ private IVariableBinding getOrCreateOuterField(Scope scope) {
209206
ITypeBinding type = scope.type;
210207
IVariableBinding outerField = outerVars.get(type);
211208
if (outerField == null) {
212-
GeneratedTypeBinding newType = new GeneratedTypeBinding(
213-
type.getName(), type.getPackage(),
214-
type.getComponentType(), false, type.getComponentType(),
215-
type.getDeclaringClass()
216-
);
217-
218-
if (type.isAnonymous()) {
219-
Scope preScope = preScope(scope);
220-
221-
boolean isField = false;
222-
if (type.getDeclaringMethod() == null) {
223-
isField = true;
224-
} else {
225-
isField = false;
226-
}
209+
if (type.isAnonymous() && (getOrCreateWeakOuterOuterField(scope) != null)) {
210+
outerField = getOrCreateWeakOuterOuterField(scope);
211+
} else {
212+
outerField = new GeneratedVariableBinding(
213+
getOuterFieldName(type), Modifier.PRIVATE | Modifier.FINAL, type.getDeclaringClass(),
214+
true, false, type, null);
215+
}
216+
}
217+
outerVars.put(type, outerField);
218+
return outerField;
219+
}
227220

228-
for (IVariableBinding theVariableBinding : preScope.declaredVars) {
229-
if (!(theVariableBinding.isField() ^ isField)) {
230-
if (scope.node != null) {
231-
TreeNode parent = scope.node.getParent().getParent();
232-
if (parent instanceof VariableDeclarationFragment) {
233-
VariableDeclarationFragment newParent = (VariableDeclarationFragment) parent;
234-
if (theVariableBinding.getName().equals(newParent.getVariableBinding().getName())) {
235-
newType.addAnnotation(BindingUtil.getAnnotation(theVariableBinding, com.google.j2objc.annotations.WeakOuter.class));
236-
break;
237-
}
221+
private IVariableBinding getOrCreateWeakOuterOuterField(Scope scope) {
222+
Scope preScope = preScope(scope);
223+
ITypeBinding type = scope.type;
224+
GeneratedTypeBinding newType = new GeneratedTypeBinding(
225+
type.getName(), type.getPackage(),
226+
type.getComponentType(), false, type.getComponentType(),
227+
type.getDeclaringClass()
228+
);
229+
230+
boolean isField = false;
231+
if (type.getDeclaringMethod() == null) {
232+
isField = true;
233+
} else {
234+
isField = false;
235+
}
236+
237+
for (IVariableBinding theVariableBinding : preScope.declaredVars) {
238+
if (!(theVariableBinding.isField() ^ isField)) {
239+
if (scope.node != null) {
240+
TreeNode parent = scope.node.getParent().getParent();
241+
if (parent instanceof VariableDeclarationFragment) {
242+
VariableDeclarationFragment newParent = (VariableDeclarationFragment) parent;
243+
if (theVariableBinding.getName().equals(newParent.getVariableBinding().getName())) {
244+
IAnnotationBinding weakOuterAnnotation = BindingUtil.getAnnotation(theVariableBinding, com.google.j2objc.annotations.WeakOuter.class);
245+
if (weakOuterAnnotation != null) {
246+
newType.addAnnotation(BindingUtil.getAnnotation(theVariableBinding, com.google.j2objc.annotations.WeakOuter.class));
247+
return new GeneratedVariableBinding(
248+
getOuterFieldName(newType), Modifier.PRIVATE | Modifier.FINAL, newType.getDeclaringClass(),
249+
true, false, newType, null);
238250
}
239251
}
240252
}
241253
}
242-
outerField = new GeneratedVariableBinding(
243-
getOuterFieldName(newType), Modifier.PRIVATE | Modifier.FINAL, newType.getDeclaringClass(),
244-
true, false, newType, null);
245-
} else {
246-
outerField = new GeneratedVariableBinding(
247-
getOuterFieldName(type), Modifier.PRIVATE | Modifier.FINAL, newType.getDeclaringClass(),
248-
true, false, newType, null);
249254
}
250255
}
251-
outerVars.put(type, outerField);
252-
return outerField;
256+
return null;
253257
}
254258

255259
private IVariableBinding getOrCreateInnerField(IVariableBinding var, ITypeBinding declaringType) {

0 commit comments

Comments
 (0)