Merge "Add missing files to fix libphonenumber for layoutlib." into klp-dev

This commit is contained in:
Deepanshu Gupta
2014-02-06 02:05:21 +00:00
committed by Android (Google) Code Review
9 changed files with 143 additions and 31 deletions

View File

@ -33,6 +33,8 @@ built_core_classes := $(call java-lib-files,core)
built_ext_dep := $(call java-lib-deps,ext) built_ext_dep := $(call java-lib-deps,ext)
built_ext_classes := $(call java-lib-files,ext) built_ext_classes := $(call java-lib-files,ext)
built_ext_data := $(call intermediates-dir-for, \
JAVA_LIBRARIES,ext,,COMMON)/javalib.jar
built_layoutlib_create_jar := $(call intermediates-dir-for, \ built_layoutlib_create_jar := $(call intermediates-dir-for, \
JAVA_LIBRARIES,layoutlib_create,HOST)/javalib.jar JAVA_LIBRARIES,layoutlib_create,HOST)/javalib.jar
@ -60,7 +62,8 @@ $(LOCAL_BUILT_MODULE): $(built_core_dep) \
$@ \ $@ \
$(built_core_classes) \ $(built_core_classes) \
$(built_framework_classes) \ $(built_framework_classes) \
$(built_ext_classes) $(built_ext_classes) \
$(built_ext_data)
$(hide) ls -l $(built_framework_classes) $(hide) ls -l $(built_framework_classes)

View File

@ -29,6 +29,7 @@ import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor; import org.objectweb.asm.signature.SignatureVisitor;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
@ -60,6 +61,9 @@ public class AsmAnalyzer {
private final String[] mIncludeGlobs; private final String[] mIncludeGlobs;
/** The set of classes to exclude.*/ /** The set of classes to exclude.*/
private final Set<String> mExcludedClasses; private final Set<String> mExcludedClasses;
/** Glob patterns of files to keep as is. */
private final String[] mIncludeFileGlobs;
/** Copy these files into the output as is. */
/** /**
* Creates a new analyzer. * Creates a new analyzer.
@ -70,15 +74,19 @@ public class AsmAnalyzer {
* @param deriveFrom Keep all classes that derive from these one (these included). * @param deriveFrom Keep all classes that derive from these one (these included).
* @param includeGlobs Glob patterns of classes to keep, e.g. "com.foo.*" * @param includeGlobs Glob patterns of classes to keep, e.g. "com.foo.*"
* ("*" does not matches dots whilst "**" does, "." and "$" are interpreted as-is) * ("*" does not matches dots whilst "**" does, "." and "$" are interpreted as-is)
* @param includeFileGlobs Glob patterns of files which are kept as is. This is only for files
* not ending in .class.
*/ */
public AsmAnalyzer(Log log, List<String> osJarPath, AsmGenerator gen, public AsmAnalyzer(Log log, List<String> osJarPath, AsmGenerator gen,
String[] deriveFrom, String[] includeGlobs, Set<String> excludeClasses) { String[] deriveFrom, String[] includeGlobs, Set<String> excludeClasses,
String[] includeFileGlobs) {
mLog = log; mLog = log;
mGen = gen; mGen = gen;
mOsSourceJar = osJarPath != null ? osJarPath : new ArrayList<String>(); mOsSourceJar = osJarPath != null ? osJarPath : new ArrayList<String>();
mDeriveFrom = deriveFrom != null ? deriveFrom : new String[0]; mDeriveFrom = deriveFrom != null ? deriveFrom : new String[0];
mIncludeGlobs = includeGlobs != null ? includeGlobs : new String[0]; mIncludeGlobs = includeGlobs != null ? includeGlobs : new String[0];
mExcludedClasses = excludeClasses; mExcludedClasses = excludeClasses;
mIncludeFileGlobs = includeFileGlobs != null ? includeFileGlobs : new String[0];
} }
/** /**
@ -86,7 +94,11 @@ public class AsmAnalyzer {
* Fills the generator with classes & dependencies found. * Fills the generator with classes & dependencies found.
*/ */
public void analyze() throws IOException, LogAbortException { public void analyze() throws IOException, LogAbortException {
Map<String, ClassReader> zipClasses = parseZip(mOsSourceJar);
TreeMap<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
parseZip(mOsSourceJar, zipClasses, filesFound);
mLog.info("Found %d classes in input JAR%s.", zipClasses.size(), mLog.info("Found %d classes in input JAR%s.", zipClasses.size(),
mOsSourceJar.size() > 1 ? "s" : ""); mOsSourceJar.size() > 1 ? "s" : "");
@ -96,15 +108,29 @@ public class AsmAnalyzer {
if (mGen != null) { if (mGen != null) {
mGen.setKeep(found); mGen.setKeep(found);
mGen.setDeps(deps); mGen.setDeps(deps);
mGen.setCopyFiles(filesFound);
} }
} }
/** /**
* Parses a JAR file and returns a list of all classes founds using a map * Parses a JAR file and adds all the classes found to <code>classes</code>
* class name => ASM ClassReader. Class names are in the form "android.view.View". * and all other files to <code>filesFound</code>.
*
* @param classes The map of class name => ASM ClassReader. Class names are
* in the form "android.view.View".
* @param fileFound The map of file name => InputStream. The file name is
* in the form "android/data/dataFile".
*/ */
Map<String,ClassReader> parseZip(List<String> jarPathList) throws IOException { void parseZip(List<String> jarPathList, Map<String, ClassReader> classes,
TreeMap<String, ClassReader> classes = new TreeMap<String, ClassReader>(); Map<String, InputStream> filesFound) throws IOException {
if (classes == null || filesFound == null) {
return;
}
Pattern[] includeFilePatterns = new Pattern[mIncludeFileGlobs.length];
for (int i = 0; i < mIncludeFileGlobs.length; ++i) {
includeFilePatterns[i] = getPatternFromGlob(mIncludeFileGlobs[i]);
}
for (String jarPath : jarPathList) { for (String jarPath : jarPathList) {
ZipFile zip = new ZipFile(jarPath); ZipFile zip = new ZipFile(jarPath);
@ -116,11 +142,17 @@ public class AsmAnalyzer {
ClassReader cr = new ClassReader(zip.getInputStream(entry)); ClassReader cr = new ClassReader(zip.getInputStream(entry));
String className = classReaderToClassName(cr); String className = classReaderToClassName(cr);
classes.put(className, cr); classes.put(className, cr);
} else {
for (int i = 0; i < includeFilePatterns.length; ++i) {
if (includeFilePatterns[i].matcher(entry.getName()).matches()) {
filesFound.put(entry.getName(), zip.getInputStream(entry));
break;
}
}
} }
} }
} }
return classes;
} }
/** /**
@ -202,7 +234,19 @@ public class AsmAnalyzer {
*/ */
void findGlobs(String globPattern, Map<String, ClassReader> zipClasses, void findGlobs(String globPattern, Map<String, ClassReader> zipClasses,
Map<String, ClassReader> inOutFound) throws LogAbortException { Map<String, ClassReader> inOutFound) throws LogAbortException {
// transforms the glob pattern in a regexp:
Pattern regexp = getPatternFromGlob(globPattern);
for (Entry<String, ClassReader> entry : zipClasses.entrySet()) {
String class_name = entry.getKey();
if (regexp.matcher(class_name).matches()) {
findClass(class_name, zipClasses, inOutFound);
}
}
}
Pattern getPatternFromGlob(String globPattern) {
// transforms the glob pattern in a regexp:
// - escape "." with "\." // - escape "." with "\."
// - replace "*" by "[^.]*" // - replace "*" by "[^.]*"
// - escape "$" with "\$" // - escape "$" with "\$"
@ -216,14 +260,7 @@ public class AsmAnalyzer {
globPattern = globPattern.replaceAll("@", ".*"); globPattern = globPattern.replaceAll("@", ".*");
globPattern += "$"; globPattern += "$";
Pattern regexp = Pattern.compile(globPattern); return Pattern.compile(globPattern);
for (Entry<String, ClassReader> entry : zipClasses.entrySet()) {
String class_name = entry.getKey();
if (regexp.matcher(class_name).matches()) {
findClass(class_name, zipClasses, inOutFound);
}
}
} }
/** /**

View File

@ -20,6 +20,7 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -52,6 +53,8 @@ public class AsmGenerator {
private Map<String, ClassReader> mKeep; private Map<String, ClassReader> mKeep;
/** All dependencies that must be completely stubbed. */ /** All dependencies that must be completely stubbed. */
private Map<String, ClassReader> mDeps; private Map<String, ClassReader> mDeps;
/** All files that are to be copied as-is. */
private Map<String, InputStream> mCopyFiles;
/** Counter of number of classes renamed during transform. */ /** Counter of number of classes renamed during transform. */
private int mRenameCount; private int mRenameCount;
/** FQCN Names of the classes to rename: map old-FQCN => new-FQCN */ /** FQCN Names of the classes to rename: map old-FQCN => new-FQCN */
@ -195,6 +198,11 @@ public class AsmGenerator {
mDeps = deps; mDeps = deps;
} }
/** Sets the map of files to output as-is. */
public void setCopyFiles(Map<String, InputStream> copyFiles) {
mCopyFiles = copyFiles;
}
/** Gets the map of classes to output as-is, except if they have native methods */ /** Gets the map of classes to output as-is, except if they have native methods */
public Map<String, ClassReader> getKeep() { public Map<String, ClassReader> getKeep() {
return mKeep; return mKeep;
@ -205,6 +213,11 @@ public class AsmGenerator {
return mDeps; return mDeps;
} }
/** Gets the map of files to output as-is. */
public Map<String, InputStream> getCopyFiles() {
return mCopyFiles;
}
/** Generates the final JAR */ /** Generates the final JAR */
public void generate() throws FileNotFoundException, IOException { public void generate() throws FileNotFoundException, IOException {
TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); TreeMap<String, byte[]> all = new TreeMap<String, byte[]>();
@ -232,6 +245,15 @@ public class AsmGenerator {
all.put(name, b); all.put(name, b);
} }
for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) {
try {
byte[] b = inputStreamToByteArray(entry.getValue());
all.put(entry.getKey(), b);
} catch (IOException e) {
// Ignore.
}
}
mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# deps classes: %d", mDeps.size());
mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# keep classes: %d", mKeep.size());
mLog.info("# renamed : %d", mRenameCount); mLog.info("# renamed : %d", mRenameCount);
@ -381,4 +403,13 @@ public class AsmGenerator {
return cv.hasNativeMethods(); return cv.hasNativeMethods();
} }
private byte[] inputStreamToByteArray(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[8192]; // 8KB
int n;
while ((n = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, n);
}
return buffer.toByteArray();
}
} }

View File

@ -115,7 +115,10 @@ public class Main {
"android.database.ContentObserver", // for Digital clock "android.database.ContentObserver", // for Digital clock
"com.android.i18n.phonenumbers.*", // for TextView with autolink attribute "com.android.i18n.phonenumbers.*", // for TextView with autolink attribute
}, },
excludeClasses); excludeClasses,
new String[] {
"com/android/i18n/phonenumbers/data/*",
});
aa.analyze(); aa.analyze();
agen.generate(); agen.generate();

View File

@ -29,6 +29,7 @@ import org.junit.Test;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -55,8 +56,10 @@ public class AsmAnalyzerTest {
Set<String> excludeClasses = new HashSet<String>(1); Set<String> excludeClasses = new HashSet<String>(1);
excludeClasses.add("java.lang.JavaClass"); excludeClasses.add("java.lang.JavaClass");
mAa = new AsmAnalyzer(mLog, mOsJarPath, null /* gen */,
null /* deriveFrom */, null /* includeGlobs */, excludeClasses); String[] includeFiles = new String[]{"mock_android/data/data*"};
mAa = new AsmAnalyzer(mLog, mOsJarPath, null /* gen */, null /* deriveFrom */,
null /* includeGlobs */, excludeClasses, includeFiles);
} }
@After @After
@ -65,7 +68,11 @@ public class AsmAnalyzerTest {
@Test @Test
public void testParseZip() throws IOException { public void testParseZip() throws IOException {
Map<String, ClassReader> map = mAa.parseZip(mOsJarPath);
Map<String, ClassReader> map = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
mAa.parseZip(mOsJarPath, map, filesFound);
assertArrayEquals(new String[] { assertArrayEquals(new String[] {
"java.lang.JavaClass", "java.lang.JavaClass",
@ -86,11 +93,17 @@ public class AsmAnalyzerTest {
"mock_android.widget.TableLayout$LayoutParams" "mock_android.widget.TableLayout$LayoutParams"
}, },
map.keySet().toArray()); map.keySet().toArray());
assertArrayEquals(new String[] {"mock_android/data/dataFile"},
filesFound.keySet().toArray());
} }
@Test @Test
public void testFindClass() throws IOException, LogAbortException { public void testFindClass() throws IOException, LogAbortException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
mAa.parseZip(mOsJarPath, zipClasses, filesFound);
TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams", ClassReader cr = mAa.findClass("mock_android.view.ViewGroup$LayoutParams",
@ -105,7 +118,11 @@ public class AsmAnalyzerTest {
@Test @Test
public void testFindGlobs() throws IOException, LogAbortException { public void testFindGlobs() throws IOException, LogAbortException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
mAa.parseZip(mOsJarPath, zipClasses, filesFound);
TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
// this matches classes, a package match returns nothing // this matches classes, a package match returns nothing
@ -164,7 +181,11 @@ public class AsmAnalyzerTest {
@Test @Test
public void testFindClassesDerivingFrom() throws LogAbortException, IOException { public void testFindClassesDerivingFrom() throws LogAbortException, IOException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
mAa.parseZip(mOsJarPath, zipClasses, filesFound);
TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> found = new TreeMap<String, ClassReader>();
mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found); mAa.findClassesDerivingFrom("mock_android.view.View", zipClasses, found);
@ -186,7 +207,11 @@ public class AsmAnalyzerTest {
@Test @Test
public void testDependencyVisitor() throws IOException, LogAbortException { public void testDependencyVisitor() throws IOException, LogAbortException {
Map<String, ClassReader> zipClasses = mAa.parseZip(mOsJarPath);
Map<String, ClassReader> zipClasses = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
mAa.parseZip(mOsJarPath, zipClasses, filesFound);
TreeMap<String, ClassReader> keep = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> keep = new TreeMap<String, ClassReader>();
TreeMap<String, ClassReader> new_keep = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> new_keep = new TreeMap<String, ClassReader>();
TreeMap<String, ClassReader> in_deps = new TreeMap<String, ClassReader>(); TreeMap<String, ClassReader> in_deps = new TreeMap<String, ClassReader>();

View File

@ -33,6 +33,7 @@ import org.objectweb.asm.Type;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@ -131,7 +132,8 @@ public class AsmGeneratorTest {
new String[] { // include classes new String[] { // include classes
"**" "**"
}, },
new HashSet<String>(0) /* excluded classes */); new HashSet<String>(0) /* excluded classes */,
new String[]{} /* include files */);
aa.analyze(); aa.analyze();
agen.generate(); agen.generate();
@ -195,10 +197,15 @@ public class AsmGeneratorTest {
new String[] { // include classes new String[] { // include classes
"**" "**"
}, },
new HashSet<String>(1)); new HashSet<String>(1),
new String[] { /* include files */
"mock_android/data/data*"
});
aa.analyze(); aa.analyze();
agen.generate(); agen.generate();
Map<String, ClassReader> output = parseZip(mOsDestJar); Map<String, ClassReader> output = new TreeMap<String, ClassReader>();
Map<String, InputStream> filesFound = new TreeMap<String, InputStream>();
parseZip(mOsDestJar, output, filesFound);
boolean injectedClassFound = false; boolean injectedClassFound = false;
for (ClassReader cr: output.values()) { for (ClassReader cr: output.values()) {
TestClassVisitor cv = new TestClassVisitor(); TestClassVisitor cv = new TestClassVisitor();
@ -206,10 +213,13 @@ public class AsmGeneratorTest {
injectedClassFound |= cv.mInjectedClassFound; injectedClassFound |= cv.mInjectedClassFound;
} }
assertTrue(injectedClassFound); assertTrue(injectedClassFound);
assertArrayEquals(new String[] {"mock_android/data/dataFile"},
filesFound.keySet().toArray());
} }
private Map<String,ClassReader> parseZip(String jarPath) throws IOException { private void parseZip(String jarPath,
TreeMap<String, ClassReader> classes = new TreeMap<String, ClassReader>(); Map<String, ClassReader> classes,
Map<String, InputStream> filesFound) throws IOException {
ZipFile zip = new ZipFile(jarPath); ZipFile zip = new ZipFile(jarPath);
Enumeration<? extends ZipEntry> entries = zip.entries(); Enumeration<? extends ZipEntry> entries = zip.entries();
@ -220,10 +230,11 @@ public class AsmGeneratorTest {
ClassReader cr = new ClassReader(zip.getInputStream(entry)); ClassReader cr = new ClassReader(zip.getInputStream(entry));
String className = classReaderToClassName(cr); String className = classReaderToClassName(cr);
classes.put(className, cr); classes.put(className, cr);
} else {
filesFound.put(entry.getName(), zip.getInputStream(entry));
} }
} }
return classes;
} }
private String classReaderToClassName(ClassReader classReader) { private String classReaderToClassName(ClassReader classReader) {

View File

@ -0,0 +1 @@
A simple data file that should *not* be copied to the output jar.

View File

@ -0,0 +1 @@
A simple data file that should be copied to the output jar unchanged.