Skip to content

Commit

Permalink
Updated PackageInfoLookup to use Procyon instead of ASM5,
Browse files Browse the repository at this point in the history
combined createClassFile methods.

	Change on 2017/07/28 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163526498
  • Loading branch information
tomball authored and kstanger committed Aug 16, 2017
1 parent 1835f9e commit d148638
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
Expand Up @@ -120,7 +120,8 @@ private ClassFileConverter(JavacEnvironment parserEnv, TranslationEnvironment tr
*/ */
private void setClassPath() throws IOException { private void setClassPath() throws IOException {
String fullPath = file.getAbsolutePath(); String fullPath = file.getAbsolutePath();
String rootPath = fullPath.substring(0, fullPath.lastIndexOf(classFile.getName() + ".class")); String relativePath = classFile.getFullName().replace('.', '/') + ".class";
String rootPath = fullPath.substring(0, fullPath.lastIndexOf(relativePath));
List<File> classPath = new ArrayList<>(); List<File> classPath = new ArrayList<>();
classPath.add(new File(rootPath)); classPath.add(new File(rootPath));
parserEnv.fileManager().setLocation(StandardLocation.CLASS_PATH, classPath); parserEnv.fileManager().setLocation(StandardLocation.CLASS_PATH, classPath);
Expand Down
Expand Up @@ -160,6 +160,10 @@ public ConstructorDeclaration getConstructor(String signature) {
return null; return null;
} }


public TypeDeclaration getType() {
return type;
}

private String signature(MethodDeclaration method) { private String signature(MethodDeclaration method) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
signature(method.getParameters(), sb); signature(method.getParameters(), sb);
Expand Down
Expand Up @@ -16,15 +16,16 @@


import com.google.devtools.j2objc.file.InputFile; import com.google.devtools.j2objc.file.InputFile;
import com.google.j2objc.annotations.ReflectionSupport; import com.google.j2objc.annotations.ReflectionSupport;
import com.strobel.decompiler.languages.java.ast.Annotation;
import com.strobel.decompiler.languages.java.ast.Expression;
import com.strobel.decompiler.languages.java.ast.MemberReferenceExpression;
import com.strobel.decompiler.languages.java.ast.PrimitiveExpression;
import com.strobel.decompiler.languages.java.ast.TypeDeclaration;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;


/** /**
* Looks up package specific attributes by looking up and parsing package-info java or class files. * Looks up package specific attributes by looking up and parsing package-info java or class files.
Expand Down Expand Up @@ -107,14 +108,14 @@ private PackageData getPackageData(String packageName) {


private PackageData findPackageData(String packageName) { private PackageData findPackageData(String packageName) {
try { try {
String fileName = packageName + ".package-info"; String typeName = packageName + ".package-info";
// First look on the sourcepath. // First look on the sourcepath.
InputFile sourceFile = fileUtil.findOnSourcePath(fileName); InputFile sourceFile = fileUtil.findOnSourcePath(typeName);
if (sourceFile != null) { if (sourceFile != null) {
return parseDataFromSourceFile(sourceFile); return parseDataFromSourceFile(sourceFile);
} }
// Then look on the classpath. // Then look on the classpath.
InputFile classFile = fileUtil.findOnClassPath(fileName); InputFile classFile = fileUtil.findOnClassPath(typeName);
if (classFile != null) { if (classFile != null) {
return parseDataFromClassFile(classFile); return parseDataFromClassFile(classFile);
} }
Expand Down Expand Up @@ -194,35 +195,31 @@ private PackageData parseDataFromSourceFile(InputFile file) throws IOException {


private PackageData parseDataFromClassFile(InputFile file) throws IOException { private PackageData parseDataFromClassFile(InputFile file) throws IOException {
PackageDataBuilder builder = new PackageDataBuilder(); PackageDataBuilder builder = new PackageDataBuilder();
ClassReader classReader = new ClassReader(file.getInputStream()); ClassFile classFile = ClassFile.create(file);
classReader.accept(new ClassVisitor(Opcodes.ASM5) { TypeDeclaration typeDecl = classFile.getType();
@Override for (Annotation annotation : typeDecl.getAnnotations()) {
public AnnotationVisitor visitAnnotation(String desc, boolean visible) { String signature = annotation.getType().toTypeReference().getErasedSignature();
if (desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) { if (signature.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
return new AnnotationVisitor(Opcodes.ASM5) { for (Expression expr : annotation.getArguments()) {
@Override if (expr instanceof MemberReferenceExpression) {
public void visit(String name, Object value) { String value = ((MemberReferenceExpression) expr).getMemberName();
if (name.equals("value")) { builder.setObjectiveCName(value);
builder.setObjectiveCName(value.toString()); } else if (expr instanceof PrimitiveExpression) {
} Object value = ((PrimitiveExpression) expr).getValue();
} builder.setObjectiveCName((String) value);
}; }
} else if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) { }
builder.setParametersAreNonnullByDefault(); } else if (signature.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
} else if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) { builder.setParametersAreNonnullByDefault();
return new AnnotationVisitor(Opcodes.ASM5) { } else if (signature.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) {
@Override for (Expression expr : annotation.getArguments()) {
public void visitEnum(String name, String desc, String value) { if (expr instanceof MemberReferenceExpression) {
if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport$Level;") String value = ((MemberReferenceExpression) expr).getMemberName();
&& name.equals("value")) { builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value));
builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value)); }
}
}
};
} }
return null;
} }
}, 0); }
return builder.build(); return builder.build();
} }
} }
Expand Up @@ -107,10 +107,11 @@ protected void tearDown() throws Exception {


protected void loadOptions() throws IOException { protected void loadOptions() throws IOException {
options = new Options(); options = new Options();

String tempPath = tempDir.getAbsolutePath();
options.load(new String[]{ options.load(new String[]{
"-d", tempDir.getAbsolutePath(), "-d", tempPath,
"-sourcepath", tempDir.getAbsolutePath(), "-sourcepath", tempPath,
"-classpath", tempPath,
"-q", // Suppress console output. "-q", // Suppress console output.
"-encoding", "UTF-8" // Translate strings correctly when encodings are nonstandard. "-encoding", "UTF-8" // Translate strings correctly when encodings are nonstandard.
}); });
Expand Down Expand Up @@ -206,7 +207,7 @@ protected CompilationUnit compileType(String name, String source) {
* @return the parsed compilation unit * @return the parsed compilation unit
*/ */
protected CompilationUnit compileAsClassFile(String name, String source) throws IOException { protected CompilationUnit compileAsClassFile(String name, String source) throws IOException {
return compileAsClassFile(name, source, "-parameters", "-cp", tempDir.getAbsolutePath()); return compileAsClassFile(name, source, "-parameters");
} }


/** /**
Expand Down Expand Up @@ -236,20 +237,6 @@ protected CompilationUnit compileAsClassFile(String name, String source,
return unit; return unit;
} }


/**
* Compiles Java source to a JVM class file.
*
* @param typeName the name of the type being declared
* @param source the source code
* @return the InputFile defining the class file
*/
protected InputFile createClassFile(String typeName, String source) throws IOException {
String tempPath = tempDir.getAbsolutePath();
List<String> classpath = getComGoogleDevtoolsJ2objcPath();
classpath.add(0, tempPath);
return createClassFile(typeName, source, "-d", tempPath, "-cp", String.join(":", classpath));
}

/** /**
* Compiles Java source to a JVM class file. * Compiles Java source to a JVM class file.
* *
Expand All @@ -267,13 +254,23 @@ protected InputFile createClassFile(String typeName, String source, String... fl
fw.write(source); fw.write(source);
} }


String[] args = new String[flags.length + 1]; List<String> args = new ArrayList<>(Arrays.asList(flags));
System.arraycopy(flags, 0, args, 0, flags.length); String tempPath = tempDir.getAbsolutePath();
args[flags.length] = srcFile.getPath(); if (!args.contains("-d")) {
args.add("-d");
args.add(tempPath);
}
if (!args.contains("-classpath") && !args.contains("-cp")) {
args.add("-classpath");
List<String> classpath = getComGoogleDevtoolsJ2objcPath();
classpath.add(0, tempPath);
args.add(String.join(":", classpath));
}
args.add(srcFile.getPath());


JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ByteArrayOutputStream errOut = new ByteArrayOutputStream(); ByteArrayOutputStream errOut = new ByteArrayOutputStream();
int numErrors = compiler.run(null, null, errOut, args); int numErrors = compiler.run(null, null, errOut, args.toArray(new String[0]));
if (numErrors > 0) { if (numErrors > 0) {
String errMsg = errOut.toString(); String errMsg = errOut.toString();
ErrorUtil.error(errMsg); ErrorUtil.error(errMsg);
Expand Down Expand Up @@ -560,6 +557,15 @@ protected String addSourceFile(String source, String fileName) throws IOExceptio
return file.getPath(); return file.getPath();
} }


/**
* Removes a file from the tmp directory,
*/
protected void removeFile(String relativePath) {
if (!new File(tempDir, relativePath).delete()) {

}
}

/** /**
* Return the contents of a previously translated file, made by a call to * Return the contents of a previously translated file, made by a call to
* {@link #translateMethod} above. * {@link #translateMethod} above.
Expand Down
Expand Up @@ -67,6 +67,7 @@ public void testFullReflectionSupportSetValueCompiled() throws IOException {
createClassFile("foo.package-info", createClassFile("foo.package-info",
"@ReflectionSupport(value = ReflectionSupport.Level.FULL) package foo;" "@ReflectionSupport(value = ReflectionSupport.Level.FULL) package foo;"
+ "import com.google.j2objc.annotations.ReflectionSupport;"); + "import com.google.j2objc.annotations.ReflectionSupport;");
removeFile("foo/package-info.java");
CompilationUnit unit = translateType("foo.A", "package foo; public class A {}"); CompilationUnit unit = translateType("foo.A", "package foo; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("foo")); assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("foo"));
Expand All @@ -76,6 +77,7 @@ public void testFullReflectionSupportCompiled() throws IOException {
createClassFile("bar.package-info", createClassFile("bar.package-info",
"@ReflectionSupport(ReflectionSupport.Level.FULL) package bar;" "@ReflectionSupport(ReflectionSupport.Level.FULL) package bar;"
+ "import com.google.j2objc.annotations.ReflectionSupport;"); + "import com.google.j2objc.annotations.ReflectionSupport;");
removeFile("bar/package-info.java");
CompilationUnit unit = translateType("bar.A", "package bar; public class A {}"); CompilationUnit unit = translateType("bar.A", "package bar; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("bar")); assertSame(ReflectionSupport.Level.FULL, packageInfoLookup.getReflectionSupportLevel("bar"));
Expand All @@ -85,6 +87,7 @@ public void testNativeOnlyReflectionSupportCompiled() throws IOException {
createClassFile("baz.package-info", createClassFile("baz.package-info",
"@com.google.j2objc.annotations.ReflectionSupport" "@com.google.j2objc.annotations.ReflectionSupport"
+ "(com.google.j2objc.annotations.ReflectionSupport.Level.NATIVE_ONLY) package baz;"); + "(com.google.j2objc.annotations.ReflectionSupport.Level.NATIVE_ONLY) package baz;");
removeFile("baz/package-info.java");
CompilationUnit unit = translateType("baz.A", "package baz; public class A {}"); CompilationUnit unit = translateType("baz.A", "package baz; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertSame(ReflectionSupport.Level.NATIVE_ONLY, assertSame(ReflectionSupport.Level.NATIVE_ONLY,
Expand All @@ -95,6 +98,7 @@ public void testPackageRenameCompiled() throws IOException {
createClassFile("foo.package-info", createClassFile("foo.package-info",
"@ObjectiveCName(\"XYZ\") package foo; " "@ObjectiveCName(\"XYZ\") package foo; "
+ "import com.google.j2objc.annotations.ObjectiveCName;"); + "import com.google.j2objc.annotations.ObjectiveCName;");
removeFile("foo/package-info.java");
String translation = translateSourceFile("package foo; public class A {}", "foo.A", "foo/A.h"); String translation = translateSourceFile("package foo; public class A {}", "foo.A", "foo/A.h");
assertTranslation(translation, "@interface XYZA"); assertTranslation(translation, "@interface XYZA");
} }
Expand All @@ -110,6 +114,7 @@ public void testParametersAreNonnullByDefault() throws IOException {
createClassFile("bar.package-info", createClassFile("bar.package-info",
"@ParametersAreNonnullByDefault package bar;" "@ParametersAreNonnullByDefault package bar;"
+ "import javax.annotation.ParametersAreNonnullByDefault;"); + "import javax.annotation.ParametersAreNonnullByDefault;");
removeFile("bar/package-info.java");
CompilationUnit unit = translateType("bar.A", "package bar; public class A {}"); CompilationUnit unit = translateType("bar.A", "package bar; public class A {}");
PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup(); PackageInfoLookup packageInfoLookup = unit.getEnv().options().getPackageInfoLookup();
assertTrue(packageInfoLookup.hasParametersAreNonnullByDefault("bar")); assertTrue(packageInfoLookup.hasParametersAreNonnullByDefault("bar"));
Expand Down

0 comments on commit d148638

Please sign in to comment.