Skip to content

Commit

Permalink
Simple classes are parsed into correct ASTs.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160198078
  • Loading branch information
manvithn authored and tomball committed Jul 14, 2017
1 parent d78b35f commit 081c067
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 31 deletions.
1 change: 1 addition & 0 deletions translator/Makefile
Expand Up @@ -132,6 +132,7 @@ JAVA_SOURCES = \
ast/QualifiedName.java \
ast/QualifiedType.java \
ast/ReturnStatement.java \
ast/SignatureASTPrinter.java \
ast/SimpleName.java \
ast/SimpleType.java \
ast/SingleMemberAnnotation.java \
Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.google.devtools.j2objc.util.TypeUtil;
import com.google.devtools.j2objc.util.UnicodeUtils;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -34,7 +35,7 @@
* @author Tom Ball
*/
public class DebugASTPrinter extends TreeVisitor {
private SourceBuilder sb = new SourceBuilder(false);
protected SourceBuilder sb = new SourceBuilder(false);
private boolean inIfStatement = false;

public static String toString(TreeNode node) {
Expand Down Expand Up @@ -629,6 +630,14 @@ public boolean visit(MemberValuePair node) {
return false;
}

protected void printMethodBody(MethodDeclaration node) {
if (node.getBody() == null) {
sb.println(';');
} else {
node.getBody().accept(this);
}
}

@Override
public boolean visit(MethodDeclaration node) {
sb.printIndent();
Expand Down Expand Up @@ -660,11 +669,7 @@ public boolean visit(MethodDeclaration node) {
}
sb.print(' ');
}
if (node.getBody() == null) {
sb.println(';');
} else {
node.getBody().accept(this);
}
printMethodBody(node);
return false;
}

Expand Down Expand Up @@ -1031,6 +1036,8 @@ public boolean visit(TryStatement node) {
return false;
}

protected void sort(List<BodyDeclaration> lst) {}

@Override
public boolean visit(TypeDeclaration node) {
if (node.getJavadoc() != null) {
Expand Down Expand Up @@ -1062,8 +1069,10 @@ public boolean visit(TypeDeclaration node) {
}
sb.println('{');
sb.indent();
for (Iterator<BodyDeclaration> it = node.getBodyDeclarations().iterator(); it.hasNext(); ) {
it.next().accept(this);
List<BodyDeclaration> bodyDeclarations = new ArrayList<>(node.getBodyDeclarations());
sort(bodyDeclarations);
for (BodyDeclaration bodyDecl : bodyDeclarations) {
bodyDecl.accept(this);
}
printStaticBlock(node);
sb.unindent();
Expand Down Expand Up @@ -1165,7 +1174,7 @@ public boolean visit(WhileStatement node) {
return false;
}

private void printAnnotations(List<Annotation> annotations) {
protected void printAnnotations(List<Annotation> annotations) {
Iterator<Annotation> iterator = annotations.iterator();
while (iterator.hasNext()) {
iterator.next().accept(this);
Expand All @@ -1179,7 +1188,7 @@ public static void printModifiers(int modifiers, StringBuilder builder) {
builder.append(temp.sb.toString());
}

private void printModifiers(int modifiers) {
protected void printModifiers(int modifiers) {
if (Modifier.isPublic(modifiers)) {
sb.print("public ");
}
Expand Down Expand Up @@ -1218,7 +1227,7 @@ private void printModifiers(int modifiers) {
}
}

private void printTypeParameters(List<? extends TypeParameterElement> typeParams) {
protected void printTypeParameters(List<? extends TypeParameterElement> typeParams) {
if (!typeParams.isEmpty()) {
sb.print('<');
for (int i = 0; i < typeParams.size(); ) {
Expand All @@ -1231,7 +1240,7 @@ private void printTypeParameters(List<? extends TypeParameterElement> typeParams
}
}

private void printStaticBlock(AbstractTypeDeclaration node) {
protected void printStaticBlock(AbstractTypeDeclaration node) {
if (!node.getClassInitStatements().isEmpty()) {
sb.printIndent();
sb.println("static {");
Expand Down
@@ -0,0 +1,43 @@
/*
* 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.ast;

import java.util.Comparator;
import java.util.List;

/**
* An AST printer which is identical to DebugASTPrinter except MethodDeclaration bodies are not
* printed.
*
* @author Manvith Narahari
*/
public class SignatureASTPrinter extends DebugASTPrinter {

public static String toString(TreeNode node) {
SignatureASTPrinter printer = new SignatureASTPrinter();
node.accept(printer);
return printer.sb.toString();
}

@Override
protected void sort(List<BodyDeclaration> lst) {
lst.sort(Comparator.comparing(BodyDeclaration::toString));
}

@Override
protected void printMethodBody(MethodDeclaration node) {
sb.println(';');
}
}
Expand Up @@ -18,6 +18,8 @@
import com.google.devtools.j2objc.ast.AbstractTypeDeclaration;
import com.google.devtools.j2objc.ast.BodyDeclaration;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.Expression;
import com.google.devtools.j2objc.ast.FieldDeclaration;
import com.google.devtools.j2objc.ast.MethodDeclaration;
import com.google.devtools.j2objc.ast.Name;
import com.google.devtools.j2objc.ast.PackageDeclaration;
Expand All @@ -26,6 +28,7 @@
import com.google.devtools.j2objc.ast.SingleVariableDeclaration;
import com.google.devtools.j2objc.ast.SourcePosition;
import com.google.devtools.j2objc.ast.TreeNode;
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.file.InputFile;
Expand Down Expand Up @@ -134,8 +137,9 @@ private TreeNode convert(Element element) {
// break;
// case EXCEPTION_PARAMETER:
// break;
// case FIELD:
// break;
case FIELD:
node = convertFieldDeclaration((VariableElement) element);
break;
// case INSTANCE_INIT:
// break;
// case LOCAL_VARIABLE:
Expand Down Expand Up @@ -222,4 +226,16 @@ private TreeNode convertParameter(VariableElement element) {
/* TODO(user): annotations; finish when supported
* .setAnnotations(convertAnnotations(node.getModifiers())); */
}

/* TODO(user): fields are linked to the static initializer and constructors;
* consider storing static final compile-time constants;
* static final primitive types or Strings */
private TreeNode convertFieldDeclaration(VariableElement element) {
Object constantValue = element.getConstantValue();
Expression initializer = constantValue != null
? TreeUtil.newLiteral(constantValue, translationEnv.typeUtil()) : null;
FieldDeclaration fieldDecl = new FieldDeclaration(element, initializer);
convertBodyDeclaration(fieldDecl);
return fieldDecl;
}
}
Expand Up @@ -22,6 +22,7 @@
import com.google.common.io.Resources;
import com.google.devtools.j2objc.ast.CompilationUnit;
import com.google.devtools.j2objc.ast.MethodDeclaration;
import com.google.devtools.j2objc.ast.SignatureASTPrinter;
import com.google.devtools.j2objc.ast.Statement;
import com.google.devtools.j2objc.ast.TreeNode;
import com.google.devtools.j2objc.ast.TreeVisitor;
Expand Down Expand Up @@ -381,7 +382,19 @@ protected void assertOccurrences(String translation, String expected, int times)
*/
protected void assertEqualASTs(TreeNode first, TreeNode second) {
if (!first.toString().equals(second.toString())) {
fail("unmatched:\"" + first + "\" vs:\n" + second);
fail("unmatched:\n" + first + "vs:\n" + second);
}
}

/**
* Verify that two AST nodes are equal excepting MethodDeclaration bodies, by comparing their
* signatures.
*/
protected void assertEqualASTSignatures(TreeNode first, TreeNode second) {
String firstStr = SignatureASTPrinter.toString(first);
String secondStr = SignatureASTPrinter.toString(second);
if (!firstStr.equals(secondStr)) {
fail("unmatched:\n" + firstStr + "vs:\n" + secondStr);
}
}

Expand All @@ -395,7 +408,7 @@ protected void assertEqualASTs(TreeNode first, TreeNode second) {
protected void assertEqualSrcClassfile(String type, String source) throws IOException {
CompilationUnit srcUnit = compileType(type, source);
CompilationUnit classfileUnit = compileAsClassFile(type, source);
assertEqualASTs(srcUnit, classfileUnit);
assertEqualASTSignatures(srcUnit, classfileUnit);
}

/**
Expand Down

0 comments on commit 081c067

Please sign in to comment.