FileDocCategorySizeDatePackage
DexWrapper.javaAPI DocAndroid 1.5 API6970Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.build

DexWrapper

public final class DexWrapper extends Object
Wrapper to access dex.jar through reflection.

Since there is no proper api to call the method in the dex library, this wrapper is going to access it through reflection.

Fields Summary
private static final String
DEX_MAIN
private static final String
DEX_CONSOLE
private static final String
DEX_ARGS
private static final String
MAIN_RUN
private Method
mRunMethod
private Constructor
mArgConstructor
private Field
mArgOutName
private Field
mArgVerbose
private Field
mArgJarOutput
private Field
mArgFileNames
private Field
mConsoleOut
private Field
mConsoleErr
Constructors Summary
Methods Summary
private static org.eclipse.core.runtime.IStatuscreateErrorStatus(java.lang.String message, java.lang.Exception e)

        AdtPlugin.log(e, message);
        AdtPlugin.printErrorToConsole(Messages.DexWrapper_Dex_Loader, message);
        
        return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, message, e);
    
public synchronized org.eclipse.core.runtime.IStatusloadDex(java.lang.String osFilepath)
Loads the dex library from a file path. The loaded library can be used via {@link DexWrapper#run(String, String[], boolean, PrintStream, PrintStream)}.

param
osFilepath the location of the dex.jar file.
return
an IStatus indicating the result of the load.

    
                                                 
         
        try {
            File f = new File(osFilepath);
            if (f.isFile() == false) {
                return new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID, String.format(
                        Messages.DexWrapper_s_does_not_exists, osFilepath));
            }
            URL url = f.toURL();
    
            URLClassLoader loader = new URLClassLoader(new URL[] { url },
                    DexWrapper.class.getClassLoader());
            
            // get the classes.
            Class<?> mainClass = loader.loadClass(DEX_MAIN);
            Class<?> consoleClass = loader.loadClass(DEX_CONSOLE);
            Class<?> argClass = loader.loadClass(DEX_ARGS);
            
            try {
                // now get the fields/methods we need
                mRunMethod = mainClass.getMethod(MAIN_RUN, argClass);
                
                mArgConstructor = argClass.getConstructor();
                mArgOutName = argClass.getField("outName"); //$NON-NLS-1$
                mArgJarOutput = argClass.getField("jarOutput"); //$NON-NLS-1$
                mArgFileNames = argClass.getField("fileNames"); //$NON-NLS-1$
                mArgVerbose = argClass.getField("verbose"); //$NON-NLS-1$
                
                mConsoleOut = consoleClass.getField("out"); //$NON-NLS-1$
                mConsoleErr = consoleClass.getField("err"); //$NON-NLS-1$
                
            } catch (SecurityException e) {
                return createErrorStatus(Messages.DexWrapper_SecuryEx_Unable_To_Find_API, e);
            } catch (NoSuchMethodException e) {
                return createErrorStatus(Messages.DexWrapper_SecuryEx_Unable_To_Find_Method, e);
            } catch (NoSuchFieldException e) {
                return createErrorStatus(Messages.DexWrapper_SecuryEx_Unable_To_Find_Field, e);
            }

            return Status.OK_STATUS;
        } catch (MalformedURLException e) {
            // really this should not happen.
            return createErrorStatus(
                    String.format(Messages.DexWrapper_Failed_to_load_s, osFilepath), e);
        } catch (ClassNotFoundException e) {
            return createErrorStatus(
                    String.format(Messages.DexWrapper_Failed_to_load_s, osFilepath), e);
        }
    
public synchronized intrun(java.lang.String osOutFilePath, java.lang.String[] osFilenames, boolean verbose, java.io.PrintStream outStream, java.io.PrintStream errStream)
Runs the dex command.

param
osOutFilePath the OS path to the outputfile (classes.dex
param
osFilenames list of input source files (.class and .jar files)
param
verbose verbose mode.
param
outStream the stdout console
param
errStream the stderr console
return
the integer return code of com.android.dx.command.dexer.Main.run()
throws
CoreException

        
        try {
            // set the stream
            mConsoleErr.set(null /* obj: static field */, errStream);
            mConsoleOut.set(null /* obj: static field */, outStream);
            
            // create the Arguments object.
            Object args = mArgConstructor.newInstance();
            mArgOutName.set(args, osOutFilePath);
            mArgFileNames.set(args, osFilenames);
            mArgJarOutput.set(args, false);
            mArgVerbose.set(args, verbose);
            
            // call the run method
            Object res = mRunMethod.invoke(null /* obj: static method */, args);
            
            if (res instanceof Integer) {
                return ((Integer)res).intValue();
            }
        
            return -1;
        } catch (IllegalAccessException e) {
            throw new CoreException(createErrorStatus(
                    String.format(Messages.DexWrapper_Unable_To_Execute_Dex_s, e.getMessage()), e));
        } catch (InstantiationException e) {
            throw new CoreException(createErrorStatus(
                    String.format(Messages.DexWrapper_Unable_To_Execute_Dex_s, e.getMessage()), e));
        } catch (InvocationTargetException e) {
            throw new CoreException(createErrorStatus(
                    String.format(Messages.DexWrapper_Unable_To_Execute_Dex_s, e.getMessage()), e));
        }