/
AbstractMethodRewriter.java
147 lines (128 loc) · 5.4 KB
/
AbstractMethodRewriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2objc.translate;
import com.google.devtools.j2objc.ast.AbstractTypeDeclaration;
import com.google.devtools.j2objc.ast.AnnotationTypeDeclaration;
import com.google.devtools.j2objc.ast.Block;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.EnumDeclaration;
import com.google.devtools.j2objc.ast.MethodDeclaration;
import com.google.devtools.j2objc.ast.NativeStatement;
import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.TypeDeclaration;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.TranslationUtil;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
/**
* Checks for missing methods that would cause an ObjC compilation error. Adds stubs for existing
* abstract methods. Adds the ABSTRACT bit to a MethodDeclaration node if the method is a
* non-default one from an interface.
*
* @author Tom Ball, Keith Stanger
*/
public class AbstractMethodRewriter extends TreeVisitor {
private final CompilationUnit unit;
public AbstractMethodRewriter(CompilationUnit unit) {
this.unit = unit;
}
@Override
public void endVisit(MethodDeclaration node) {
IMethodBinding methodBinding = node.getMethodBinding();
if (BindingUtil.isAbstract(methodBinding)) {
// JDT only adds the abstract bit to a MethodDeclaration node's modifiers if the abstract
// method is from a class. Since we want our code generator to go over an interface's
// method nodes for default method support and skip abstract methods, we add the bit if the
// method is from an interface.
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
boolean isInterface = declaringClass.isInterface();
if (isInterface) {
node.addModifiers(Modifier.ABSTRACT);
}
// There's no need to stub out an abstract method for an interface's companion class.
// Similarly, if this is an abstract method in a class and there's no need for reflection,
// we skip the stubbing out.
if (isInterface || !TranslationUtil.needsReflection(declaringClass)) {
unit.setHasIncompleteProtocol();
unit.setHasIncompleteImplementation();
return;
}
Block body = new Block();
// Generate a body which throws a NSInvalidArgumentException.
String bodyCode = "// can't call an abstract method\n"
+ "[self doesNotRecognizeSelector:_cmd];";
if (!BindingUtil.isVoid(node.getReturnType().getTypeBinding())) {
bodyCode += "\nreturn 0;"; // Never executes, but avoids a gcc warning.
}
body.getStatements().add(new NativeStatement(bodyCode));
node.setBody(body);
node.removeModifiers(Modifier.ABSTRACT);
}
}
@Override
public void endVisit(TypeDeclaration node) {
visitType(node);
}
@Override
public void endVisit(EnumDeclaration node) {
visitType(node);
}
@Override
public void endVisit(AnnotationTypeDeclaration node) {
visitType(node);
}
private void visitType(AbstractTypeDeclaration node) {
ITypeBinding typeBinding = node.getTypeBinding();
if (!Modifier.isAbstract(node.getModifiers()) && !typeBinding.isEnum()) {
return;
}
// Find any interface methods that aren't defined by this abstract type so
// we can silence incomplete protocol errors.
// Collect needed methods from this interface and all super-interfaces.
Queue<ITypeBinding> interfaceQueue = new LinkedList<ITypeBinding>();
Set<IMethodBinding> interfaceMethods = new LinkedHashSet<IMethodBinding>();
interfaceQueue.addAll(Arrays.asList(typeBinding.getInterfaces()));
ITypeBinding intrface;
while ((intrface = interfaceQueue.poll()) != null) {
interfaceMethods.addAll(Arrays.asList(intrface.getDeclaredMethods()));
interfaceQueue.addAll(Arrays.asList(intrface.getInterfaces()));
}
// Check if any interface methods are missing from the implementation
for (IMethodBinding interfaceMethod : interfaceMethods) {
if (!isMethodImplemented(typeBinding, interfaceMethod)) {
unit.setHasIncompleteProtocol();
}
}
}
private boolean isMethodImplemented(ITypeBinding type, IMethodBinding method) {
if (type == null) {
return false;
}
for (IMethodBinding m : type.getDeclaredMethods()) {
if (method.isSubsignature(m)
|| (method.getName().equals(m.getName())
&& method.getReturnType().getErasure().isEqualTo(m.getReturnType().getErasure())
&& Arrays.equals(method.getParameterTypes(), m.getParameterTypes()))) {
return true;
}
}
return isMethodImplemented(type.getSuperclass(), method);
}
}