Skip to content

Commit

Permalink
Convert DefaultConstructorAdder to new type system.
Browse files Browse the repository at this point in the history
	Change on 2016/10/11 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135817659
  • Loading branch information
kstanger authored and tomball committed Oct 11, 2016
1 parent 691a37d commit a36dadb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
Expand Up @@ -21,11 +21,11 @@
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.TreeVisitor;
import com.google.devtools.j2objc.ast.TypeDeclaration;

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

import com.google.devtools.j2objc.util.ElementUtil;
import java.util.HashSet;
import java.util.Set;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;

/**
* Adds the implicie default constructors for classes that have no declared
Expand All @@ -44,24 +44,16 @@ public void endVisit(EnumDeclaration node) {
}

private void visitType(AbstractTypeDeclaration node) {
Set<IMethodBinding> declaredInAst = new HashSet<>();
Set<ExecutableElement> declaredInAst = new HashSet<>();
for (MethodDeclaration methodDecl : TreeUtil.getMethodDeclarations(node)) {
declaredInAst.add(methodDecl.getMethodBinding());
declaredInAst.add(methodDecl.getExecutableElement());
}
for (IMethodBinding methodBinding : node.getTypeBinding().getDeclaredMethods()) {
if (!declaredInAst.contains(methodBinding) && isDefaultConstructor(methodBinding)) {
addDefaultConstructor(node, methodBinding);
Iterable<ExecutableElement> constructors = ElementUtil.filterEnclosedElements(
node.getTypeElement(), ExecutableElement.class, ElementKind.CONSTRUCTOR);
for (ExecutableElement constructor : constructors) {
if (constructor.getParameters().isEmpty() && !declaredInAst.contains(constructor)) {
node.addBodyDeclaration(new MethodDeclaration(constructor).setBody(new Block()));
}
}
}

private boolean isDefaultConstructor(IMethodBinding method) {
return method.isConstructor() && method.getParameterTypes().length == 0;
}

private void addDefaultConstructor(AbstractTypeDeclaration node, IMethodBinding methodBinding) {
MethodDeclaration methodDecl = new MethodDeclaration(methodBinding);
methodDecl.setBody(new Block());
node.addBodyDeclaration(methodDecl);
}
}
Expand Up @@ -14,10 +14,13 @@

package com.google.devtools.j2objc.util;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.types.GeneratedVariableElement;
import com.google.devtools.j2objc.types.LambdaTypeElement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -292,24 +295,20 @@ private static void collectInheritedTypesInclusive(
}
}

public static <T extends Element> Iterable<T> filterEnclosedElements(
Element elem, Class<T> resultClass, ElementKind... kinds) {
List<ElementKind> kindsList = Arrays.asList(kinds);
return Iterables.transform(Iterables.filter(
elem.getEnclosedElements(), e -> kindsList.contains(e.getKind())), resultClass::cast);
}

public static List<ExecutableElement> getDeclaredMethods(Element e) {
List<ExecutableElement> methods = new ArrayList<>();
for (Element i : e.getEnclosedElements()) {
if (i.getKind() == ElementKind.METHOD || i.getKind() == ElementKind.CONSTRUCTOR) {
methods.add((ExecutableElement) i);
}
}
return methods;
return Lists.newArrayList(filterEnclosedElements(
e, ExecutableElement.class, ElementKind.CONSTRUCTOR, ElementKind.METHOD));
}

public static List<VariableElement> getDeclaredFields(Element e) {
List<VariableElement> fields = new ArrayList<>();
for (Element i : e.getEnclosedElements()) {
if (i.getKind() == ElementKind.FIELD) {
fields.add((VariableElement) i);
}
}
return fields;
return Lists.newArrayList(filterEnclosedElements(e, VariableElement.class, ElementKind.FIELD));
}

private static boolean paramsMatch(ExecutableElement method, String[] paramTypes) {
Expand All @@ -327,15 +326,9 @@ private static boolean paramsMatch(ExecutableElement method, String[] paramTypes
}

public static ExecutableElement findMethod(TypeElement type, String name, String... paramTypes) {
for (Element e : type.getEnclosedElements()) {
if (e.getKind() == ElementKind.METHOD && e.getSimpleName().toString().equals(name)) {
ExecutableElement method = (ExecutableElement) e;
if (paramsMatch(method, paramTypes)) {
return method;
}
}
}
return null;
return Iterables.getFirst(Iterables.filter(
filterEnclosedElements(type, ExecutableElement.class, ElementKind.METHOD),
method -> getName(method).equals(name) && paramsMatch(method, paramTypes)), null);
}

public static Set<Modifier> toModifierSet(int modifiers) {
Expand Down

0 comments on commit a36dadb

Please sign in to comment.