use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class TypeDeclarationGeneratorTest method testSortMethods.
public void testSortMethods() throws IOException {
String source = "class A {" + "void zebra() {}" + "void gnu(String s, int i, Runnable r) {}" + "A(int i) {}" + "void gnu() {}" + "void gnu(int i, Runnable r) {}" + "void yak() {}" + "A(String s) {}" + "A() {}" + "A(int i, Runnable r) {}" + "void gnu(String s, int i) {}}";
CompilationUnit unit = translateType("A", source);
final ArrayList<MethodDeclaration> methods = new ArrayList<>();
unit.accept(new TreeVisitor() {
@Override
public void endVisit(MethodDeclaration node) {
if (!ElementUtil.isSynthetic(node.getExecutableElement())) {
methods.add(node);
}
}
});
Collections.sort(methods, TypeDeclarationGenerator.METHOD_DECL_ORDER);
assertTrue(methods.get(0).toString().startsWith("<init>()"));
assertTrue(methods.get(1).toString().startsWith("<init>(int i)"));
assertTrue(methods.get(2).toString().startsWith("<init>(int i,java.lang.Runnable r)"));
assertTrue(methods.get(3).toString().startsWith("<init>(java.lang.String s)"));
assertTrue(methods.get(4).toString().startsWith("void gnu()"));
assertTrue(methods.get(5).toString().startsWith("void gnu(int i,java.lang.Runnable r)"));
assertTrue(methods.get(6).toString().startsWith("void gnu(java.lang.String s,int i)"));
assertTrue(methods.get(7).toString().startsWith("void gnu(java.lang.String s,int i,java.lang.Runnable r)"));
assertTrue(methods.get(8).toString().startsWith("void yak()"));
assertTrue(methods.get(9).toString().startsWith("void zebra()"));
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class PackageInfoRewriter method createPrefixMethod.
private MethodDeclaration createPrefixMethod(String prefix, TypeElement type) {
ExecutableElement element = GeneratedExecutableElement.newMethodWithSelector("__prefix", typeUtil.getJavaString().asType(), type).addModifiers(Modifier.STATIC);
MethodDeclaration method = new MethodDeclaration(element);
method.setHasDeclaration(false);
Block body = new Block();
method.setBody(body);
body.addStatement(new ReturnStatement(new StringLiteral(prefix, typeUtil)));
return method;
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class Functionizer method addDisallowedConstructors.
/**
* Declare any inherited constructors that aren't allowed to be accessed in Java
* with a NS_UNAVAILABLE macro, so that clang will flag such access from native
* code as an error.
*/
private void addDisallowedConstructors(TypeDeclaration node) {
TypeElement typeElement = node.getTypeElement();
TypeElement superClass = ElementUtil.getSuperclass(typeElement);
if (ElementUtil.isPrivateInnerType(typeElement) || ElementUtil.isAbstract(typeElement) || superClass == null) {
return;
}
Set<String> constructors = new HashSet<>();
for (ExecutableElement constructor : ElementUtil.getConstructors(typeElement)) {
constructors.add(nameTable.getMethodSelector(constructor));
}
Map<String, ExecutableElement> inheritedConstructors = new HashMap<>();
// Add super constructors that have unique parameter lists.
for (ExecutableElement superC : ElementUtil.getConstructors(superClass)) {
if (ElementUtil.isPrivate(superC)) {
// Skip private super constructors since they're already unavailable.
continue;
}
String selector = nameTable.getMethodSelector(superC);
if (!constructors.contains(selector)) {
inheritedConstructors.put(selector, superC);
}
}
for (Map.Entry<String, ExecutableElement> entry : inheritedConstructors.entrySet()) {
ExecutableElement oldConstructor = entry.getValue();
GeneratedExecutableElement newConstructor = GeneratedExecutableElement.newConstructorWithSelector(entry.getKey(), typeElement, typeUtil);
MethodDeclaration decl = new MethodDeclaration(newConstructor).setUnavailable(true);
decl.addModifiers(Modifier.ABSTRACT);
int count = 0;
for (VariableElement param : oldConstructor.getParameters()) {
VariableElement newParam = GeneratedVariableElement.newParameter("arg" + count++, param.asType(), newConstructor);
newConstructor.addParameter(newParam);
decl.addParameter(new SingleVariableDeclaration(newParam));
}
addImplicitParameters(decl, ElementUtil.getDeclaringClass(oldConstructor));
node.addBodyDeclaration(decl);
}
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class MetadataWriter method visitType.
private void visitType(AbstractTypeDeclaration node) {
TypeElement type = node.getTypeElement();
if (!translationUtil.needsReflection(type)) {
return;
}
ExecutableElement metadataElement = GeneratedExecutableElement.newMethodWithSelector("__metadata", CLASS_INFO_TYPE, type).addModifiers(Modifier.STATIC, Modifier.PRIVATE);
MethodDeclaration metadataDecl = new MethodDeclaration(metadataElement);
metadataDecl.setHasDeclaration(false);
Block body = new Block();
metadataDecl.setBody(body);
new MetadataGenerator(node, body.getStatements()).generateClassMetadata();
node.addBodyDeclaration(metadataDecl);
}
use of com.google.devtools.j2objc.ast.MethodDeclaration in project j2objc by google.
the class DeadCodeEliminator method removeDeadMethods.
/**
* Remove dead methods from a type's body declarations.
*/
private void removeDeadMethods(String clazz, List<BodyDeclaration> declarations) {
Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
while (declarationsIter.hasNext()) {
BodyDeclaration declaration = declarationsIter.next();
if (declaration instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) declaration;
// TODO(kstanger): Remove the method and its OCNI comment.
if (Modifier.isNative(method.getModifiers())) {
continue;
}
ExecutableElement elem = method.getExecutableElement();
String name = typeUtil.getReferenceName(elem);
String signature = typeUtil.getReferenceSignature(elem);
if (deadCodeMap.containsMethod(clazz, name, signature)) {
if (method.isConstructor()) {
deadCodeMap.addConstructorRemovedClass(clazz);
}
declarationsIter.remove();
}
}
}
}
Aggregations