Skip to content

Commit

Permalink
Support ClassFile loading from jar files.
Browse files Browse the repository at this point in the history
	Change on 2017/07/24 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162999485
  • Loading branch information
tomball authored and kstanger committed Aug 16, 2017
1 parent e5216f1 commit 5573958
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Expand Up @@ -109,7 +109,7 @@ private ClassFileConverter(JavacEnvironment parserEnv, TranslationEnvironment tr
this.parserEnv = parserEnv; this.parserEnv = parserEnv;
this.translationEnv = translationEnv; this.translationEnv = translationEnv;
this.file = file; this.file = file;
this.classFile = ClassFile.create(file.getAbsolutePath(), translationEnv.typeUtil()); this.classFile = ClassFile.create(file, translationEnv.typeUtil());
this.typeName = classFile.getFullName(); this.typeName = classFile.getFullName();
} }


Expand Down
Expand Up @@ -14,13 +14,17 @@


package com.google.devtools.j2objc.util; package com.google.devtools.j2objc.util;


import com.google.devtools.j2objc.file.InputFile;
import com.strobel.assembler.InputTypeLoader; import com.strobel.assembler.InputTypeLoader;
import com.strobel.assembler.metadata.FieldDefinition; import com.strobel.assembler.metadata.FieldDefinition;
import com.strobel.assembler.metadata.ITypeLoader;
import com.strobel.assembler.metadata.JarTypeLoader;
import com.strobel.assembler.metadata.MetadataSystem; import com.strobel.assembler.metadata.MetadataSystem;
import com.strobel.assembler.metadata.MethodDefinition; import com.strobel.assembler.metadata.MethodDefinition;
import com.strobel.assembler.metadata.TypeDefinition; import com.strobel.assembler.metadata.TypeDefinition;
import com.strobel.assembler.metadata.TypeReference; import com.strobel.assembler.metadata.TypeReference;
import java.io.IOException; import java.io.IOException;
import java.util.jar.JarFile;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement; import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ExecutableType; import javax.lang.model.type.ExecutableType;
Expand All @@ -32,8 +36,21 @@ public class ClassFile {
private final TypeDefinition typeDef; private final TypeDefinition typeDef;
private final TypeUtil typeUtil; private final TypeUtil typeUtil;


public static ClassFile create(String path, TypeUtil typeUtil) throws IOException { public static ClassFile create(InputFile file, TypeUtil typeUtil) throws IOException {
final MetadataSystem metadataSystem = new MetadataSystem(new InputTypeLoader()); ITypeLoader loader;
String path = file.getAbsolutePath();
if (path.endsWith(".jar")) {
loader = new JarTypeLoader(new JarFile(path));
path = file.getUnitName();
if (!path.endsWith(".class")) {
return null;
}
// Remove .class suffix, as JarTypeLoader adds it.
path = path.substring(0, path.length() - 6);
} else {
loader = new InputTypeLoader();
}
MetadataSystem metadataSystem = new MetadataSystem(loader);
TypeReference typeRef = metadataSystem.lookupType(path); TypeReference typeRef = metadataSystem.lookupType(path);
TypeDefinition typeDef = metadataSystem.resolve(typeRef); TypeDefinition typeDef = metadataSystem.resolve(typeRef);
return new ClassFile(typeDef, typeUtil); return new ClassFile(typeDef, typeUtil);
Expand Down
Expand Up @@ -77,6 +77,7 @@
import com.google.devtools.j2objc.types.CompoundTypeTest; import com.google.devtools.j2objc.types.CompoundTypeTest;
import com.google.devtools.j2objc.types.HeaderImportCollectorTest; import com.google.devtools.j2objc.types.HeaderImportCollectorTest;
import com.google.devtools.j2objc.types.ImplementationImportCollectorTest; import com.google.devtools.j2objc.types.ImplementationImportCollectorTest;
import com.google.devtools.j2objc.util.ClassFileTest;
import com.google.devtools.j2objc.util.CodeReferenceMapTest; import com.google.devtools.j2objc.util.CodeReferenceMapTest;
import com.google.devtools.j2objc.util.ElementUtilTest; import com.google.devtools.j2objc.util.ElementUtilTest;
import com.google.devtools.j2objc.util.ErrorUtilTest; import com.google.devtools.j2objc.util.ErrorUtilTest;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class SmallTests {
AutoboxerTest.class, AutoboxerTest.class,
CastResolverTest.class, CastResolverTest.class,
ClassFileConverterTest.class, ClassFileConverterTest.class,
ClassFileTest.class,
CodeReferenceMapTest.class, CodeReferenceMapTest.class,
ComplexExpressionExtractorTest.class, ComplexExpressionExtractorTest.class,
CompoundTypeTest.class, CompoundTypeTest.class,
Expand Down
@@ -0,0 +1,37 @@
/*
* 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.util;

import com.google.devtools.j2objc.GenerationTest;
import com.google.devtools.j2objc.file.InputFile;
import com.google.devtools.j2objc.file.JarredInputFile;
import java.io.IOException;

/**
* Tests for {@link ClassFile}.
*
* Note: classfile conversion is experimental and not supported.
*/
public class ClassFileTest extends GenerationTest {

public void testJarFileLoading() throws IOException {
String jarFilePath = getResourceAsFile("packageInfoLookupTest.jar");
InputFile input = new JarredInputFile(jarFilePath,
"com/google/test/packageInfoLookupTest/package-info.class");
ClassFile cf = ClassFile.create(input, null);
assertNotNull(cf);
assertEquals("com.google.test.packageInfoLookupTest.package-info", cf.getFullName());
}
}

0 comments on commit 5573958

Please sign in to comment.