From f9da7885e323f2ae032a1558beee42d46ed93ed1 Mon Sep 17 00:00:00 2001 From: tball Date: Tue, 19 Dec 2017 13:41:37 -0800 Subject: [PATCH] Automated g4 rollback of changelist 179497585: Support classfile translation from command-line, rather than just from tests. *** Reason for rollback *** Restored original CL, as build breakage was due to different change. *** Original change description *** Automated g4 rollback of changelist 179465118. *** Reason for rollback *** iOS tests timeout with this change. *** Original change description *** Support classfile translation from command-line, rather than just from tests. *** *** Change on 2017/12/19 by tball ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=179597752 --- .../j2objc/javac/ClassFileConverter.java | 11 ++++++++--- .../devtools/j2objc/javac/MethodTranslator.java | 16 ++++++---------- .../j2objc/pipeline/GenerationBatch.java | 3 ++- .../j2objc/pipeline/InputFilePreprocessor.java | 6 ++++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/translator/src/main/java/com/google/devtools/j2objc/javac/ClassFileConverter.java b/translator/src/main/java/com/google/devtools/j2objc/javac/ClassFileConverter.java index 02475a9dcd..2ad0e2d38d 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/javac/ClassFileConverter.java +++ b/translator/src/main/java/com/google/devtools/j2objc/javac/ClassFileConverter.java @@ -156,7 +156,8 @@ private TreeNode convert(Element element, TreeNode parent) { break; case CONSTRUCTOR: case METHOD: - node = convertMethodDeclaration((ExecutableElement) element, (TypeDeclaration) parent); + node = convertMethodDeclaration( + (ExecutableElement) element, (AbstractTypeDeclaration) parent); break; case ENUM: node = convertEnumDeclaration((TypeElement) element); @@ -280,7 +281,10 @@ private TreeNode convertTypeDeclaration(TypeElement element) { TypeDeclaration typeDecl = new TypeDeclaration(element); convertBodyDeclaration(typeDecl, element); for (Element elem : element.getEnclosedElements()) { - typeDecl.addBodyDeclaration((BodyDeclaration) convert(elem, typeDecl)); + // Ignore inner types, as they are defined by other classfiles. + if (!elem.getKind().isClass() && !elem.getKind().isInterface()) { + typeDecl.addBodyDeclaration((BodyDeclaration) convert(elem, typeDecl)); + } } if (typeDecl.isInterface()) { removeInterfaceModifiers(typeDecl); @@ -292,7 +296,8 @@ private String getMethodDescriptor(ExecutableElement exec) { return translationEnv.typeUtil().getMethodDescriptor((ExecutableType) exec.asType()); } - private TreeNode convertMethodDeclaration(ExecutableElement element, TypeDeclaration node) { + private TreeNode convertMethodDeclaration(ExecutableElement element, + AbstractTypeDeclaration node) { MethodDeclaration methodDecl = new MethodDeclaration(element); convertBodyDeclaration(methodDecl, element); HashMap localVariableTable = new HashMap<>(); diff --git a/translator/src/main/java/com/google/devtools/j2objc/javac/MethodTranslator.java b/translator/src/main/java/com/google/devtools/j2objc/javac/MethodTranslator.java index a1e63d7b77..0734192cb4 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/javac/MethodTranslator.java +++ b/translator/src/main/java/com/google/devtools/j2objc/javac/MethodTranslator.java @@ -14,6 +14,7 @@ package com.google.devtools.j2objc.javac; +import com.google.devtools.j2objc.ast.AbstractTypeDeclaration; import com.google.devtools.j2objc.ast.ArrayAccess; import com.google.devtools.j2objc.ast.ArrayCreation; import com.google.devtools.j2objc.ast.ArrayInitializer; @@ -47,7 +48,6 @@ import com.google.devtools.j2objc.ast.TreeNode.Kind; 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.ast.VariableDeclarationExpression; import com.google.devtools.j2objc.ast.VariableDeclarationFragment; import com.google.devtools.j2objc.ast.VariableDeclarationStatement; @@ -68,7 +68,6 @@ import com.strobel.decompiler.languages.java.ast.IAstVisitor; import com.strobel.decompiler.languages.java.ast.Keys; import com.strobel.decompiler.patterns.Pattern; -import com.sun.tools.javac.code.Symbol; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -87,17 +86,18 @@ * Procyon AST visitor that converts method/constructor bodies. * * @author Manvith Narahari + * @author Tom Ball */ class MethodTranslator implements IAstVisitor { private final JavacEnvironment parserEnv; private final TypeUtil typeUtil; private final ExecutableElement executableElement; - private final TypeDeclaration typeDecl; + private final AbstractTypeDeclaration typeDecl; private final Map localVariableTable; private final boolean sourceDebugging; public MethodTranslator(JavacEnvironment parserEnv, TranslationEnvironment translationEnv, - ExecutableElement executableElement, TypeDeclaration typeDecl, + ExecutableElement executableElement, AbstractTypeDeclaration typeDecl, Map localVariableTable) { this.parserEnv = parserEnv; this.typeUtil = translationEnv.typeUtil(); @@ -150,11 +150,7 @@ private TypeMirror resolve(TypeReference typeRef) { } String typeName = typeRef.getFullName(); Element element = parserEnv.resolve(typeName); - if (element instanceof TypeElement && ((Symbol.ClassSymbol) element).classfile == null) { - // Should never happen, since all types for the classfile this method - // is from should have been previously loaded by javac. - throw new AssertionError("failed resolving type: " + typeName); - } + // TODO(tball): element is raw, any support for type parameters needed? return element.asType(); } @@ -192,7 +188,7 @@ public TreeNode visitInvocationExpression( } throw new AssertionError("not implemented"); } - + private ExecutableElement findConstructor(TypeElement type, MethodDefinition methodDef) { String signature = methodDef.getSignature(); String erasedSignature = methodDef.getErasedSignature(); diff --git a/translator/src/main/java/com/google/devtools/j2objc/pipeline/GenerationBatch.java b/translator/src/main/java/com/google/devtools/j2objc/pipeline/GenerationBatch.java index 14fe6125e4..ae1bed418f 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/pipeline/GenerationBatch.java +++ b/translator/src/main/java/com/google/devtools/j2objc/pipeline/GenerationBatch.java @@ -144,7 +144,8 @@ private void processJarFile(String filename) { while (enumerator.hasMoreElements()) { ZipEntry entry = enumerator.nextElement(); String internalPath = entry.getName(); - if (internalPath.endsWith(".java")) { + if (internalPath.endsWith(".java") + || (options.translateClassfiles() && internalPath.endsWith(".class"))) { // Extract JAR file to a temporary directory File outputFile = options.fileUtil().extractZipEntry(tempDir, zfile, entry); InputFile newFile = new RegularInputFile(outputFile.getAbsolutePath(), internalPath); diff --git a/translator/src/main/java/com/google/devtools/j2objc/pipeline/InputFilePreprocessor.java b/translator/src/main/java/com/google/devtools/j2objc/pipeline/InputFilePreprocessor.java index 534bce808d..97b3c67439 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/pipeline/InputFilePreprocessor.java +++ b/translator/src/main/java/com/google/devtools/j2objc/pipeline/InputFilePreprocessor.java @@ -32,7 +32,7 @@ import java.util.logging.Logger; /** - * Preprocesses each input file in the batch. + * Preprocesses each Java file in the batch. */ public class InputFilePreprocessor { @@ -49,7 +49,9 @@ public InputFilePreprocessor(Parser parser) { public void processInputs(Iterable inputs) { for (ProcessingContext input : inputs) { - processInput(input); + if (input.getFile().getUnitName().endsWith(".java")) { + processInput(input); + } } }