rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1979 0f100539ce6c
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
jaroslav@1599
     1
/**
jaroslav@1599
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1599
     4
 *
jaroslav@1599
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1599
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1599
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1599
     8
 *
jaroslav@1599
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1599
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1599
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1599
    12
 * GNU General Public License for more details.
jaroslav@1599
    13
 *
jaroslav@1599
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1599
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1599
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1599
    17
 */
jaroslav@1599
    18
package org.apidesign.bck2brwsr.aot;
jaroslav@1599
    19
jaroslav@1599
    20
import java.io.BufferedReader;
jaroslav@1684
    21
import java.io.ByteArrayInputStream;
jaroslav@1599
    22
import java.io.File;
jaroslav@1679
    23
import java.io.FileInputStream;
jaroslav@1599
    24
import java.io.IOException;
jaroslav@1599
    25
import java.io.InputStream;
jaroslav@1599
    26
import java.io.InputStreamReader;
jaroslav@1599
    27
import java.net.URL;
jaroslav@1599
    28
import java.util.ArrayList;
jaroslav@1599
    29
import java.util.Enumeration;
jaroslav@1678
    30
import java.util.HashMap;
jaroslav@1599
    31
import java.util.HashSet;
jaroslav@1599
    32
import java.util.List;
jaroslav@1678
    33
import java.util.Map;
jaroslav@1599
    34
import java.util.Set;
jaroslav@1724
    35
import java.util.jar.Attributes;
jaroslav@1599
    36
import java.util.jar.JarEntry;
jaroslav@1599
    37
import java.util.jar.JarFile;
jaroslav@1979
    38
import java.util.jar.Manifest;
jaroslav@1599
    39
import java.util.logging.Level;
jaroslav@1599
    40
import java.util.logging.Logger;
jaroslav@1599
    41
import java.util.zip.ZipEntry;
jaroslav@1599
    42
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1599
    43
jaroslav@1599
    44
/** Utilities to process JAR files and set a compiler
jaroslav@1603
    45
 * up.
jaroslav@1599
    46
 *
jaroslav@1603
    47
 * @since 0.9
jaroslav@1599
    48
 * @author Jaroslav Tulach
jaroslav@1599
    49
 */
jaroslav@1599
    50
public final class Bck2BrwsrJars {
jaroslav@1599
    51
    private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
jaroslav@1679
    52
jaroslav@1599
    53
    private Bck2BrwsrJars() {
jaroslav@1599
    54
    }
jaroslav@1599
    55
    
jaroslav@1599
    56
    /** Creates new compiler pre-configured from the content of 
jaroslav@1599
    57
     * provided JAR file. The compiler will compile all classes.
jaroslav@1729
    58
     * The system understands OSGi manifest entries and NetBeans
jaroslav@1729
    59
     * module system manifest entries and will export
jaroslav@1599
    60
     * all packages that are exported in the JAR file. The system
jaroslav@1628
    61
     * also recognizes META-INF/services and makes sure the class names
jaroslav@1599
    62
     * are not mangled.
jaroslav@1599
    63
     * 
jaroslav@1599
    64
     * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
jaroslav@1599
    65
     *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
jaroslav@1599
    66
     *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
jaroslav@1599
    67
     *    Can be <code>null</code> - in such case an 
jaroslav@1599
    68
     *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
jaroslav@1599
    69
     * @param jar the file to process
jaroslav@1599
    70
     * @return newly configured compiler
jaroslav@1599
    71
     * @throws IOException if something goes wrong
jaroslav@1599
    72
     */
jaroslav@1599
    73
    public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
jaroslav@1710
    74
        return configureFrom(c, jar, null);
jaroslav@1710
    75
    }
jaroslav@1710
    76
    
jaroslav@1710
    77
    /** Creates new compiler pre-configured from the content of 
jaroslav@1710
    78
     * provided JAR file. The compiler will compile all classes.
jaroslav@1729
    79
     * The system understands OSGi manifest entries and NetBeans
jaroslav@1729
    80
     * module system manifest entries and will export
jaroslav@1710
    81
     * all packages that are exported in the JAR file. The system
jaroslav@1710
    82
     * also recognizes META-INF/services and makes sure the class names
jaroslav@1710
    83
     * are not mangled.
jaroslav@1710
    84
     * 
jaroslav@1710
    85
     * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
jaroslav@1710
    86
     *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
jaroslav@1710
    87
     *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
jaroslav@1710
    88
     *    Can be <code>null</code> - in such case an 
jaroslav@1710
    89
     *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
jaroslav@1710
    90
     * @param jar the file to process
jaroslav@1710
    91
     * @param classpath additional resources to make available during
jaroslav@1710
    92
     *   compilation, but not include them in the generated JavaScript
jaroslav@1710
    93
     * @return newly configured compiler
jaroslav@1710
    94
     * @throws IOException if something goes wrong
jaroslav@1710
    95
     * @since 0.11
jaroslav@1710
    96
     */
jaroslav@1710
    97
    public static Bck2Brwsr configureFrom(
jaroslav@1710
    98
        Bck2Brwsr c, File jar, final ClassLoader classpath
jaroslav@1710
    99
    ) throws IOException {
jaroslav@1769
   100
        return configureFrom(c, jar, classpath, true);
jaroslav@1769
   101
    }
jaroslav@1769
   102
    
jaroslav@1769
   103
    /** Creates new compiler pre-configured from the content of 
jaroslav@1769
   104
     * provided JAR file. The compiler will compile all classes.
jaroslav@1769
   105
     * The system understands OSGi manifest entries and NetBeans
jaroslav@1769
   106
     * module system manifest entries and will export
jaroslav@1769
   107
     * all packages that are exported in the JAR file. The system
jaroslav@1769
   108
     * also recognizes META-INF/services and makes sure the class names
jaroslav@1769
   109
     * are not mangled.
jaroslav@1769
   110
     * 
jaroslav@1769
   111
     * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
jaroslav@1769
   112
     *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
jaroslav@1769
   113
     *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
jaroslav@1769
   114
     *    Can be <code>null</code> - in such case an 
jaroslav@1769
   115
     *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
jaroslav@1769
   116
     * @param jar the file to process
jaroslav@1769
   117
     * @param classpath additional resources to make available during
jaroslav@1769
   118
     *   compilation, but not include them in the generated JavaScript
jaroslav@1769
   119
     * @param ignoreBootClassPath should we ignore classes on bootclasspath?
jaroslav@1769
   120
     * @return newly configured compiler
jaroslav@1769
   121
     * @throws IOException if something goes wrong
jaroslav@1769
   122
     * @since 0.14
jaroslav@1769
   123
     */
jaroslav@1769
   124
    public static Bck2Brwsr configureFrom(
jaroslav@1769
   125
        Bck2Brwsr c, File jar, final ClassLoader classpath, final boolean ignoreBootClassPath
jaroslav@1769
   126
    ) throws IOException {
jaroslav@1679
   127
        if (jar.isDirectory()) {
jaroslav@1769
   128
            return configureDir(ignoreBootClassPath, c, jar, classpath);
jaroslav@1679
   129
        }
jaroslav@1601
   130
        final JarFile jf = new JarFile(jar);
jaroslav@1678
   131
        final List<String> classes = new ArrayList<>();
jaroslav@1601
   132
        List<String> resources = new ArrayList<>();
jaroslav@1601
   133
        Set<String> exported = new HashSet<>();
jaroslav@1601
   134
        class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1678
   135
            JarRes() {
jaroslav@1769
   136
                super(ignoreBootClassPath, classpath, classes);
jaroslav@1678
   137
            }
jaroslav@1601
   138
            @Override
jaroslav@1601
   139
            public InputStream get(String resource) throws IOException {
jaroslav@1696
   140
                InputStream is = getConverted(resource);
jaroslav@1696
   141
                if (is != null) {
jaroslav@1696
   142
                    return is;
jaroslav@1696
   143
                }
jaroslav@1821
   144
                if (resource.startsWith("/")) {
jaroslav@1821
   145
                    resource = resource.substring(1);
jaroslav@1821
   146
                }
jaroslav@1828
   147
                ZipEntry ze = jf.getEntry(resource);
jaroslav@1828
   148
                if (ze != null) {
jaroslav@1828
   149
                    is = jf.getInputStream(ze);
jaroslav@1828
   150
                }
jaroslav@1601
   151
                return is == null ? super.get(resource) : is;
jaroslav@1599
   152
            }
jaroslav@1599
   153
        }
jaroslav@1678
   154
        JarRes jarRes = new JarRes();
jaroslav@1678
   155
jaroslav@1678
   156
        listJAR(jf, jarRes, resources, exported);
jaroslav@1980
   157
        final Manifest manifest = jf.getManifest();
jaroslav@1980
   158
        final Attributes mainAttributes = manifest == null ? null : manifest.getMainAttributes();
jaroslav@1980
   159
        String cp = mainAttributes == null ? null : mainAttributes.getValue("Class-Path"); // NOI18N
jaroslav@1710
   160
        String[] parts = cp == null ? new String[0] : cp.split(" ");
jaroslav@1678
   161
jaroslav@1622
   162
        if (c == null) {
jaroslav@1622
   163
            c = Bck2Brwsr.newCompiler();
jaroslav@1622
   164
        }
jaroslav@1622
   165
        
jaroslav@1622
   166
        return c
jaroslav@1710
   167
            .library(parts)
jaroslav@1601
   168
            .addClasses(classes.toArray(new String[classes.size()]))
jaroslav@1601
   169
            .addExported(exported.toArray(new String[exported.size()]))
jaroslav@1601
   170
            .addResources(resources.toArray(new String[resources.size()]))
jaroslav@1678
   171
            .resources(jarRes);
jaroslav@1599
   172
    }
jaroslav@1599
   173
    
jaroslav@1599
   174
    private static void listJAR(
jaroslav@1678
   175
        JarFile j, EmulationResources classes,
jaroslav@1599
   176
        List<String> resources, Set<String> keep
jaroslav@1599
   177
    ) throws IOException {
jaroslav@1599
   178
        Enumeration<JarEntry> en = j.entries();
jaroslav@1599
   179
        while (en.hasMoreElements()) {
jaroslav@1599
   180
            JarEntry e = en.nextElement();
jaroslav@1599
   181
            final String n = e.getName();
jaroslav@1599
   182
            if (n.endsWith("/")) {
jaroslav@1599
   183
                continue;
jaroslav@1599
   184
            }
jaroslav@1602
   185
            if (n.startsWith("META-INF/maven/")) {
jaroslav@1602
   186
                continue;
jaroslav@1602
   187
            }
jaroslav@1599
   188
            int last = n.lastIndexOf('/');
jaroslav@1599
   189
            String pkg = n.substring(0, last + 1);
jaroslav@1599
   190
            if (pkg.startsWith("java/")) {
jaroslav@1599
   191
                keep.add(pkg);
jaroslav@1599
   192
            }
jaroslav@1599
   193
            if (n.endsWith(".class")) {
jaroslav@1678
   194
                classes.addClassResource(n);
jaroslav@1599
   195
            } else {
jaroslav@1599
   196
                resources.add(n);
jaroslav@1599
   197
                if (n.startsWith("META-INF/services/") && keep != null) {
jaroslav@1599
   198
                    BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
jaroslav@1599
   199
                    for (;;) {
jaroslav@1599
   200
                        String l = r.readLine();
jaroslav@1599
   201
                        if (l == null) {
jaroslav@1599
   202
                            break;
jaroslav@1599
   203
                        }
jaroslav@1599
   204
                        if (l.startsWith("#")) {
jaroslav@1599
   205
                            continue;
jaroslav@1599
   206
                        }
jaroslav@1599
   207
                        keep.add(l.replace('.', '/'));
jaroslav@1599
   208
                    }
jaroslav@1764
   209
                    r.close();
jaroslav@1599
   210
                }
jaroslav@1599
   211
            }
jaroslav@1599
   212
        }
jaroslav@1724
   213
        if (keep != null) {
jaroslav@1979
   214
            final Manifest manifest = j.getManifest();
jaroslav@1979
   215
            if (manifest != null) {
jaroslav@1979
   216
                final Attributes mainAttr = manifest.getMainAttributes();
jaroslav@1979
   217
                if (mainAttr != null) {
jaroslav@1979
   218
                    exportPublicPackages(mainAttr, keep);
jaroslav@1979
   219
                }
jaroslav@1979
   220
            }
jaroslav@1724
   221
        }
jaroslav@1724
   222
    }
jaroslav@1724
   223
jaroslav@1724
   224
    static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
jaroslav@1724
   225
        String exp = mainAttr.getValue("Export-Package"); // NOI18N
jaroslav@1724
   226
        if (exp != null) {
jaroslav@1599
   227
            for (String def : exp.split(",")) {
jaroslav@1599
   228
                for (String sep : def.split(";")) {
jaroslav@1599
   229
                    keep.add(sep.replace('.', '/') + "/");
jaroslav@1599
   230
                    break;
jaroslav@1599
   231
                }
jaroslav@1599
   232
            }
jaroslav@1728
   233
            return;
jaroslav@1728
   234
        }
jaroslav@1728
   235
        exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
jaroslav@1728
   236
        if (exp != null) {
jaroslav@1728
   237
            for (String def : exp.split(",")) {
jaroslav@1728
   238
                def = def.trim();
jaroslav@1728
   239
                if (def.endsWith(".*")) {
jaroslav@1728
   240
                    keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
jaroslav@1728
   241
                }
jaroslav@1728
   242
            }
jaroslav@1599
   243
        }
jaroslav@1599
   244
    }
jaroslav@1678
   245
    
jaroslav@1678
   246
    static byte[] readFrom(InputStream is) throws IOException {
jaroslav@1678
   247
        int expLen = is.available();
jaroslav@1678
   248
        if (expLen < 1) {
jaroslav@1678
   249
            expLen = 1;
jaroslav@1678
   250
        }
jaroslav@1678
   251
        byte[] arr = new byte[expLen];
jaroslav@1678
   252
        int pos = 0;
jaroslav@1678
   253
        for (;;) {
jaroslav@1678
   254
            int read = is.read(arr, pos, arr.length - pos);
jaroslav@1678
   255
            if (read == -1) {
jaroslav@1678
   256
                break;
jaroslav@1678
   257
            }
jaroslav@1678
   258
            pos += read;
jaroslav@1678
   259
            if (pos == arr.length) {
jaroslav@1678
   260
                byte[] tmp = new byte[arr.length * 2];
jaroslav@1678
   261
                System.arraycopy(arr, 0, tmp, 0, arr.length);
jaroslav@1678
   262
                arr = tmp;
jaroslav@1678
   263
            }
jaroslav@1678
   264
        }
jaroslav@1678
   265
        if (pos != arr.length) {
jaroslav@1678
   266
            byte[] tmp = new byte[pos];
jaroslav@1678
   267
            System.arraycopy(arr, 0, tmp, 0, pos);
jaroslav@1678
   268
            arr = tmp;
jaroslav@1678
   269
        }
jaroslav@1678
   270
        return arr;
jaroslav@1678
   271
    }
jaroslav@1678
   272
    
jaroslav@1599
   273
jaroslav@1599
   274
    static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1678
   275
        private final List<String> classes;
jaroslav@1678
   276
        private final Map<String,byte[]> converted = new HashMap<>();
jaroslav@1678
   277
        private final BytecodeProcessor proc;
jaroslav@1710
   278
        private final ClassLoader cp;
jaroslav@1769
   279
        private final boolean ignoreBootClassPath;
jaroslav@1678
   280
jaroslav@1769
   281
        protected EmulationResources(boolean ignoreBootClassPath, ClassLoader cp, List<String> classes) {
jaroslav@1769
   282
            this.ignoreBootClassPath = ignoreBootClassPath;
jaroslav@1678
   283
            this.classes = classes;
jaroslav@1710
   284
            this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
jaroslav@1678
   285
            BytecodeProcessor p;
jaroslav@1678
   286
            try {
jaroslav@1678
   287
                Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
jaroslav@1678
   288
                p = (BytecodeProcessor) bpClass.newInstance();
jaroslav@1678
   289
            } catch (Throwable t) {
jaroslav@1678
   290
                p = null;
jaroslav@1678
   291
            }
jaroslav@1678
   292
            this.proc = p;
jaroslav@1678
   293
        }
jaroslav@1599
   294
jaroslav@1696
   295
        protected final InputStream getConverted(String name) throws IOException {
jaroslav@1684
   296
            byte[] arr = converted.get(name);
jaroslav@1684
   297
            if (arr != null) {
jaroslav@1684
   298
                return new ByteArrayInputStream(arr);
jaroslav@1684
   299
            }
jaroslav@1696
   300
            return null;
jaroslav@1696
   301
        }
jaroslav@1696
   302
        
jaroslav@1696
   303
        @Override
jaroslav@1696
   304
        public InputStream get(String name) throws IOException {
jaroslav@1696
   305
            InputStream is = getConverted(name);
jaroslav@1696
   306
            if (is != null) {
jaroslav@1696
   307
                return is;
jaroslav@1696
   308
            }
jaroslav@1886
   309
            return getFromCp(name);
jaroslav@1886
   310
        }
jaroslav@1886
   311
jaroslav@1886
   312
        private InputStream getFromCp(String name) throws IOException {
jaroslav@1710
   313
            Enumeration<URL> en = cp.getResources(name);
jaroslav@1599
   314
            URL u = null;
jaroslav@1599
   315
            while (en.hasMoreElements()) {
jaroslav@1599
   316
                u = en.nextElement();
jaroslav@1599
   317
            }
jaroslav@1599
   318
            if (u == null) {
jaroslav@1763
   319
                LOG.log(Level.FINE, "Cannot find {0}", name);
jaroslav@1599
   320
                return null;
jaroslav@1599
   321
            }
jaroslav@1769
   322
            if (ignoreBootClassPath && u.toExternalForm().contains("/rt.jar!")) {
jaroslav@1763
   323
                LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
jaroslav@1599
   324
                return null;
jaroslav@1599
   325
            }
jaroslav@1599
   326
            return u.openStream();
jaroslav@1599
   327
        }
jaroslav@1678
   328
jaroslav@1886
   329
        private final class NoConvRes implements Bck2Brwsr.Resources {
jaroslav@1886
   330
            @Override
jaroslav@1886
   331
            public InputStream get(String resource) throws IOException {
jaroslav@1886
   332
                return getFromCp(resource);
jaroslav@1886
   333
            }
jaroslav@1886
   334
        }
jaroslav@1886
   335
jaroslav@1678
   336
        private void addClassResource(String n) throws IOException {
jaroslav@1678
   337
            if (proc != null) {
jaroslav@1678
   338
                try (InputStream is = this.get(n)) {
jaroslav@1886
   339
                    Map<String, byte[]> conv = proc.process(n, readFrom(is), new NoConvRes());
jaroslav@1678
   340
                    if (conv != null) {
jaroslav@1681
   341
                        boolean found = false;
jaroslav@1681
   342
                        for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
jaroslav@1681
   343
                            String res = entrySet.getKey();
jaroslav@1681
   344
                            byte[] bytes = entrySet.getValue();
jaroslav@1681
   345
                            if (res.equals(n)) {
jaroslav@1681
   346
                                found = true;
jaroslav@1681
   347
                            }
jaroslav@1684
   348
                            assert res.endsWith(".class") : "Wrong resource: " + res;
jaroslav@1681
   349
                            converted.put(res, bytes);
jaroslav@1681
   350
                            classes.add(res.substring(0, res.length() - 6));
jaroslav@1681
   351
                        }
jaroslav@1681
   352
                        if (!found) {
jaroslav@1678
   353
                            throw new IOException("Cannot find " + n + " among " + conv);
jaroslav@1678
   354
                        }
jaroslav@1678
   355
                        return;
jaroslav@1678
   356
                    }
jaroslav@1678
   357
                }
jaroslav@1678
   358
            }
jaroslav@1678
   359
            classes.add(n.substring(0, n.length() - 6));
jaroslav@1678
   360
        }
jaroslav@1599
   361
    }
jaroslav@1599
   362
    
jaroslav@1769
   363
    private static Bck2Brwsr configureDir(final boolean ignoreBootClassPath, Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
jaroslav@1679
   364
        List<String> arr = new ArrayList<>();
jaroslav@1679
   365
        List<String> classes = new ArrayList<>();
jaroslav@1679
   366
        class DirRes extends EmulationResources {
jaroslav@1710
   367
            public DirRes(ClassLoader cp, List<String> classes) {
jaroslav@1769
   368
                super(ignoreBootClassPath, cp, classes);
jaroslav@1679
   369
            }
jaroslav@1679
   370
jaroslav@1679
   371
            @Override
jaroslav@1679
   372
            public InputStream get(String name) throws IOException {
jaroslav@1679
   373
                InputStream is = super.get(name);
jaroslav@1679
   374
                if (is != null) {
jaroslav@1679
   375
                    return is;
jaroslav@1679
   376
                }
jaroslav@1679
   377
                File r = new File(dir, name.replace('/', File.separatorChar));
jaroslav@1679
   378
                if (r.exists()) {
jaroslav@1679
   379
                    return new FileInputStream(r);
jaroslav@1679
   380
                }
jaroslav@1679
   381
                return null;
jaroslav@1679
   382
            }
jaroslav@1679
   383
        }
jaroslav@1710
   384
        DirRes dirRes = new DirRes(cp, classes);
jaroslav@1679
   385
        listDir(dir, null, dirRes, arr);
jaroslav@1679
   386
        if (c == null) {
jaroslav@1679
   387
            c = Bck2Brwsr.newCompiler();
jaroslav@1679
   388
        }
jaroslav@1679
   389
        return c
jaroslav@1679
   390
        .addRootClasses(classes.toArray(new String[0]))
jaroslav@1679
   391
        .addResources(arr.toArray(new String[0]))
jaroslav@1679
   392
        .library()
jaroslav@1679
   393
        //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1679
   394
        .resources(dirRes);
jaroslav@1679
   395
    }
jaroslav@1679
   396
jaroslav@1679
   397
    private static void listDir(
jaroslav@1679
   398
        File f, String pref, EmulationResources res, List<String> resources
jaroslav@1679
   399
    ) throws IOException {
jaroslav@1679
   400
        File[] arr = f.listFiles();
jaroslav@1679
   401
        if (arr == null) {
jaroslav@1679
   402
            if (f.getName().endsWith(".class")) {
jaroslav@1679
   403
                res.addClassResource(pref + f.getName());
jaroslav@1679
   404
            } else {
jaroslav@1679
   405
                resources.add(pref + f.getName());
jaroslav@1679
   406
            }
jaroslav@1679
   407
        } else {
jaroslav@1679
   408
            for (File ch : arr) {
jaroslav@1679
   409
                listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
jaroslav@1679
   410
            }
jaroslav@1679
   411
        }
jaroslav@1679
   412
    }
jaroslav@1678
   413
    
jaroslav@1599
   414
}