FileDocCategorySizeDatePackage
Main.javaAPI DocAndroid 1.5 API7473Wed May 06 22:41:10 BST 2009com.android.mkstubs

Main

public class Main extends Object
Main entry point of the MkStubs app.

For workflow details, see {@link #process(Params)}.

Fields Summary
Constructors Summary
Methods Summary
private voidaddString(com.android.mkstubs.Main$Params p, java.lang.String s)
Adds one pattern string to the current filter. The syntax must be:
  • +full_include or +prefix_include*
  • -full_exclude or -prefix_exclude*
  • @filename
The input string is trimmed so any space around the first letter (-/+/@) or at the end is removed. Empty strings are ignored.

param
p The params which filters to edit.
param
s The string to examine.
throws
IOException

        if (s == null) {
            return;
        }

        s = s.trim();

        if (s.length() < 2) {
            return;
        }
        
        char mode = s.charAt(0);
        s = s.substring(1).trim();

        if (mode == '@") {
            addStringsFromFile(p, s);
            
        } else if (mode == '-") {
            s = s.replace('.", '/");  // transform FQCN into ASM internal name
            if (s.endsWith("*")) {
                p.getFilter().getExcludePrefix().add(s.substring(0, s.length() - 1));
            } else {
                p.getFilter().getExcludeFull().add(s);
            }

        } else if (mode == '+") {
            s = s.replace('.", '/");  // transform FQCN into ASM internal name
            if (s.endsWith("*")) {
                p.getFilter().getIncludePrefix().add(s.substring(0, s.length() - 1));
            } else {
                p.getFilter().getIncludeFull().add(s);
            }
        }
    
private voidaddStringsFromFile(com.android.mkstubs.Main$Params p, java.lang.String osFilePath)
Adds all the filter strings from the given file.

param
p The params which filter to edit.
param
osFilePath The OS path to the file containing the patterns.
throws
IOException
see
#addString(Params, String)

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(osFilePath));
            String line;
            while ((line = br.readLine()) != null) {
                addString(p, line);
            }
        } finally {
            br.close();
        }
    
public static voidmain(java.lang.String[] args)
Main entry point. Processes arguments then performs the "real" work.


        Main m = new Main();
        try {
            Params p = m.processArgs(args);
            m.process(p);
        } catch (IOException e) {
            e.printStackTrace();
        }
    
private voidprocess(com.android.mkstubs.Main$Params p)
Performs the main workflow of this app:
  • Read the input Jar to get all its classes.
  • Filter out all classes that should not be included or that should be excluded.
  • Goes thru the classes, filters methods/fields and generate their source in a directory called "<outpath_jar_path>_sources"
  • Does the same filtering on the classes but this time generates the real stubbed output jar.

        AsmAnalyzer aa = new AsmAnalyzer();
        Map<String, ClassReader> classes = aa.parseInputJar(p.getInputJarPath());

        System.out.println(String.format("Classes loaded: %d", classes.size()));
        
        aa.filter(classes, p.getFilter());

        System.out.println(String.format("Classes filtered: %d", classes.size()));

        // dump as Java source files, mostly for debugging
        SourceGenerator src_gen = new SourceGenerator();
        File dst_src_dir = new File(p.getOutputJarPath() + "_sources");
        dst_src_dir.mkdir();
        src_gen.generateSource(dst_src_dir, classes, p.getFilter());
        
        // dump the stubbed jar
        StubGenerator stub_gen = new StubGenerator();
        File dst_jar = new File(p.getOutputJarPath());
        stub_gen.generateStubbedJar(dst_jar, classes, p.getFilter());
    
private com.android.mkstubs.Main$ParamsprocessArgs(java.lang.String[] args)
Grabs command-line arguments. The expected arguments are:
  • The filename of the input Jar.
  • The filename of the output Jar.
  • One or more include/exclude patterns or files containing these patterns. See {@link #addString(Params, String)} for syntax.

throws
IOException on failure to read a pattern file.

        
        if (args.length < 2) {
            usage();
        }

        Params p = new Params(args[0], args[1]);
        
        for (int i = 2; i < args.length; i++) {
            addString(p, args[i]);
        }
        
        return p;
    
private voidusage()
Prints some help to stdout.

        System.out.println("Usage: mkstub input.jar output.jar [excluded-class @excluded-classes-file ...]");

        System.out.println("Include syntax:\n" +
                "+com.package.* : whole package, with glob\n" +
                "+com.package.Class[$Inner] or ...Class*: whole classes with optional glob\n" +
                "Inclusion is not supported at method/field level.\n\n");

        System.out.println("Exclude syntax:\n" +
        		"-com.package.* : whole package, with glob\n" +
        		"-com.package.Class[$Inner] or ...Class*: whole classes with optional glob\n" +
        		"-com.package.Class#method: whole method or field\n" +
                "-com.package.Class#method(IILjava/lang/String;)V: specific method with signature.\n\n");
        System.exit(1);