Skip to content

Commit

Permalink
Converts TranslationUtil.needsReflection to new type system.
Browse files Browse the repository at this point in the history
	Change on 2016/10/18 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136483898
  • Loading branch information
kstanger authored and Keith Stanger committed Oct 19, 2016
1 parent c849cb6 commit d278b9a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
Expand Up @@ -28,19 +28,21 @@
import com.google.devtools.j2objc.types.IOSMethodBinding;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.DeadCodeMap;
import com.google.devtools.j2objc.util.ElementUtil;
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 com.google.devtools.j2objc.util.TypeUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;

/**
* Checks for missing methods that would cause an ObjC compilation error. Adds stubs for existing
Expand All @@ -60,18 +62,17 @@ public AbstractMethodRewriter(CompilationUnit unit, DeadCodeMap deadCodeMap) {

@Override
public void endVisit(MethodDeclaration node) {
IMethodBinding methodBinding = node.getMethodBinding();
if (!BindingUtil.isAbstract(methodBinding)) {
ExecutableElement methodElement = node.getExecutableElement();
if (!ElementUtil.isAbstract(methodElement)) {
return;
}

// 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) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
if (declaringClass.getKind().isInterface()) {
node.addModifiers(Modifier.ABSTRACT);
return;
}
Expand All @@ -89,7 +90,7 @@ public void endVisit(MethodDeclaration node) {
// 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())) {
if (!TypeUtil.isVoid(node.getReturnType().getTypeMirror())) {
bodyCode += "\nreturn 0;"; // Never executes, but avoids a gcc warning.
}
body.addStatement(new NativeStatement(bodyCode));
Expand Down
Expand Up @@ -45,6 +45,7 @@
import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.UnitTreeVisitor;
import com.google.devtools.j2objc.gen.SignatureGenerator;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.types.FunctionBinding;
import com.google.devtools.j2objc.types.GeneratedVariableBinding;
import com.google.devtools.j2objc.util.BindingUtil;
Expand Down Expand Up @@ -278,7 +279,8 @@ public void endVisit(MethodDeclaration node) {
// Public methods must be kept for the public API.
|| !(BindingUtil.isPrivateInnerType(declaringClass) || BindingUtil.isPrivate(binding))
// Methods must be kept for reflection if enabled.
|| (TranslationUtil.needsReflection(declaringClass) && !isEnumConstructor);
|| (TranslationUtil.needsReflection(BindingConverter.getTypeElement(declaringClass))
&& !isEnumConstructor);
if (keepMethod) {
setFunctionCaller(node, binding);
} else {
Expand Down
Expand Up @@ -83,7 +83,7 @@ public void endVisit(AnnotationTypeDeclaration node) {

private void visitType(AbstractTypeDeclaration node) {
ITypeBinding type = node.getTypeBinding();
if (!TranslationUtil.needsReflection(type)) {
if (!TranslationUtil.needsReflection(BindingConverter.getTypeElement(type))) {
return;
}

Expand Down
Expand Up @@ -36,24 +36,19 @@
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.Type;
import com.google.devtools.j2objc.ast.TypeDeclaration;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.types.IOSMethodBinding;
import com.google.devtools.j2objc.types.Types;
import com.google.j2objc.annotations.ReflectionSupport;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;

/**
* General collection of utility methods.
Expand Down Expand Up @@ -94,26 +89,25 @@ public static List<ITypeBinding> getInterfaceTypes(AbstractTypeDeclaration node)
}

public static boolean needsReflection(AbstractTypeDeclaration node) {
return needsReflection(node.getTypeBinding());
return needsReflection(node.getTypeElement());
}

public static boolean needsReflection(PackageDeclaration node) {
return needsReflection(getReflectionSupportLevel(
getAnnotation(node.getPackageElement(), ReflectionSupport.class)));
ElementUtil.getAnnotation(node.getPackageElement(), ReflectionSupport.class)));
}

public static boolean needsReflection(ITypeBinding type) {
if (BindingUtil.isLambda(type)) {
public static boolean needsReflection(TypeElement type) {
if (ElementUtil.isLambda(type)) {
return false;
}
while (type != null) {
TypeElement element = (TypeElement) BindingConverter.getElement(type);
ReflectionSupport.Level level = getReflectionSupportLevel(
getAnnotation(element, ReflectionSupport.class));
ElementUtil.getAnnotation(type, ReflectionSupport.class));
if (level != null) {
return level == ReflectionSupport.Level.FULL;
}
type = type.getDeclaringClass();
type = ElementUtil.getDeclaringClass(type);
}
return !Options.stripReflection();
}
Expand All @@ -137,17 +131,6 @@ public static ReflectionSupport.Level getReflectionSupportLevel(
? ReflectionSupport.Level.valueOf(level.getSimpleName().toString()) : null;
}

private static AnnotationMirror getAnnotation(Element element, Class<?> annotationClass) {
String className = annotationClass.getName();
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
TypeElement type = (TypeElement) mirror.getAnnotationType().asElement();
if (type.getQualifiedName().toString().equals(className)) {
return mirror;
}
}
return null;
}

/**
* If possible give this expression an unbalanced extra retain. If a non-null
* result is returned, then the returned expression has an unbalanced extra
Expand Down

0 comments on commit d278b9a

Please sign in to comment.