Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.ast.EnumConstantDeclaration;
Expand Down Expand Up @@ -560,23 +561,29 @@ public String getMethodNameFromAnnotation(ExecutableElement method) {
}

public @Nullable String getSwiftMethodNameFromAnnotation(MethodDeclaration method) {
ExecutableElement element = method.getExecutableElement();
// Check if the method has the annotation
String annotationName = swiftNameFromAnnotation(method.getExecutableElement());
String annotationName = swiftNameFromAnnotation(element);
if (annotationName != null) {
return annotationName;
}

if (method.getParameters().isEmpty()) {
return null;
}

TypeElement clazz = ElementUtil.getDeclaringClass(element);
// Check if the class or package has the annotation
TypeElement clazz = ElementUtil.getDeclaringClass(method.getExecutableElement());
if (!packageHasSwiftNameAnnotation(clazz) && !elementHasSwiftNameAnnotation(clazz)) {
return null;
}

ExecutableElement element = method.getExecutableElement();
String selector = getMethodSelector(element);
if (ElementUtil.isConstructor(element)) {
if (hasSuperConstructorWithSameSelector(selector, clazz)) {
return null;
}
} else {
if (hasSuperMethodWithSameSelector(selector, clazz)) {
return null;
}
}

// If the annotation is still null after checking the names then it doesn't exist
String methodName = element.getSimpleName().toString();
Expand All @@ -591,6 +598,42 @@ public String getMethodNameFromAnnotation(ExecutableElement method) {
return addSwiftParamNames(method.getParameters(), methodName, ':');
}

private boolean hasSuperConstructorWithSameSelector(String selector, TypeElement clazz) {
TypeElement superClass = ElementUtil.getSuperclass(clazz);
if (superClass == null) {
return false;
}
if (Iterables.any(
ElementUtil.getConstructors(superClass), c -> getMethodSelector(c).equals(selector))) {
return true;
}
return hasSuperConstructorWithSameSelector(selector, superClass);
}

private boolean hasSuperMethodWithSameSelector(String selector, TypeElement clazz) {
TypeElement superClass = ElementUtil.getSuperclass(clazz);
if (superClass != null) {
if (hasMethodWithSelector(superClass, selector)
|| hasSuperMethodWithSameSelector(selector, superClass)) {
return true;
}
}
for (TypeMirror interfaceMirror : clazz.getInterfaces()) {
TypeElement interfaceElement = TypeUtil.asTypeElement(interfaceMirror);
if (interfaceElement != null) {
if (hasMethodWithSelector(interfaceElement, selector)
|| hasSuperMethodWithSameSelector(selector, interfaceElement)) {
return true;
}
}
}
return false;
}

private boolean hasMethodWithSelector(TypeElement clazz, String selector) {
return Iterables.any(ElementUtil.getMethods(clazz), m -> getMethodSelector(m).equals(selector));
}

public @Nullable String getSwiftClassNameFromAnnotation(TypeElement clazz, boolean getParents) {
String annotationName = swiftNameFromAnnotation(clazz);
if (annotationName != null) {
Expand All @@ -605,8 +648,8 @@ public String getMethodNameFromAnnotation(ExecutableElement method) {
NestingKind nesting = clazz.getNestingKind();
if (nesting == NestingKind.MEMBER) {
Element parent = clazz.getEnclosingElement();
if (parent instanceof TypeElement && !parent.getKind().isInterface()) {
String parentName = getSwiftClassNameFromAnnotation((TypeElement) parent, false);
if (parent instanceof TypeElement parentElement && !parent.getKind().isInterface()) {
String parentName = getSwiftClassNameFromAnnotation(parentElement, false);
if (parentName != null) {
return (parentName + "." + clazz.getSimpleName()).replaceAll("$", "");
}
Expand Down
Loading