rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Jan 2017 04:04:22 +0100
changeset 1979 0f100539ce6c
parent 1886 c4d37f95adf2
child 1980 97ea0f369e18
permissions -rw-r--r--
More defensive checks shielding against JARs without manifest
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@1678
   157
        
jaroslav@1678
   158
        String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
jaroslav@1710
   159
        String[] parts = cp == null ? new String[0] : cp.split(" ");
jaroslav@1678
   160
jaroslav@1622
   161
        if (c == null) {
jaroslav@1622
   162
            c = Bck2Brwsr.newCompiler();
jaroslav@1622
   163
        }
jaroslav@1622
   164
        
jaroslav@1622
   165
        return c
jaroslav@1710
   166
            .library(parts)
jaroslav@1601
   167
            .addClasses(classes.toArray(new String[classes.size()]))
jaroslav@1601
   168
            .addExported(exported.toArray(new String[exported.size()]))
jaroslav@1601
   169
            .addResources(resources.toArray(new String[resources.size()]))
jaroslav@1678
   170
            .resources(jarRes);
jaroslav@1599
   171
    }
jaroslav@1599
   172
    
jaroslav@1599
   173
    private static void listJAR(
jaroslav@1678
   174
        JarFile j, EmulationResources classes,
jaroslav@1599
   175
        List<String> resources, Set<String> keep
jaroslav@1599
   176
    ) throws IOException {
jaroslav@1599
   177
        Enumeration<JarEntry> en = j.entries();
jaroslav@1599
   178
        while (en.hasMoreElements()) {
jaroslav@1599
   179
            JarEntry e = en.nextElement();
jaroslav@1599
   180
            final String n = e.getName();
jaroslav@1599
   181
            if (n.endsWith("/")) {
jaroslav@1599
   182
                continue;
jaroslav@1599
   183
            }
jaroslav@1602
   184
            if (n.startsWith("META-INF/maven/")) {
jaroslav@1602
   185
                continue;
jaroslav@1602
   186
            }
jaroslav@1599
   187
            int last = n.lastIndexOf('/');
jaroslav@1599
   188
            String pkg = n.substring(0, last + 1);
jaroslav@1599
   189
            if (pkg.startsWith("java/")) {
jaroslav@1599
   190
                keep.add(pkg);
jaroslav@1599
   191
            }
jaroslav@1599
   192
            if (n.endsWith(".class")) {
jaroslav@1678
   193
                classes.addClassResource(n);
jaroslav@1599
   194
            } else {
jaroslav@1599
   195
                resources.add(n);
jaroslav@1599
   196
                if (n.startsWith("META-INF/services/") && keep != null) {
jaroslav@1599
   197
                    BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
jaroslav@1599
   198
                    for (;;) {
jaroslav@1599
   199
                        String l = r.readLine();
jaroslav@1599
   200
                        if (l == null) {
jaroslav@1599
   201
                            break;
jaroslav@1599
   202
                        }
jaroslav@1599
   203
                        if (l.startsWith("#")) {
jaroslav@1599
   204
                            continue;
jaroslav@1599
   205
                        }
jaroslav@1599
   206
                        keep.add(l.replace('.', '/'));
jaroslav@1599
   207
                    }
jaroslav@1764
   208
                    r.close();
jaroslav@1599
   209
                }
jaroslav@1599
   210
            }
jaroslav@1599
   211
        }
jaroslav@1724
   212
        if (keep != null) {
jaroslav@1979
   213
            final Manifest manifest = j.getManifest();
jaroslav@1979
   214
            if (manifest != null) {
jaroslav@1979
   215
                final Attributes mainAttr = manifest.getMainAttributes();
jaroslav@1979
   216
                if (mainAttr != null) {
jaroslav@1979
   217
                    exportPublicPackages(mainAttr, keep);
jaroslav@1979
   218
                }
jaroslav@1979
   219
            }
jaroslav@1724
   220
        }
jaroslav@1724
   221
    }
jaroslav@1724
   222
jaroslav@1724
   223
    static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
jaroslav@1724
   224
        String exp = mainAttr.getValue("Export-Package"); // NOI18N
jaroslav@1724
   225
        if (exp != null) {
jaroslav@1599
   226
            for (String def : exp.split(",")) {
jaroslav@1599
   227
                for (String sep : def.split(";")) {
jaroslav@1599
   228
                    keep.add(sep.replace('.', '/') + "/");
jaroslav@1599
   229
                    break;
jaroslav@1599
   230
                }
jaroslav@1599
   231
            }
jaroslav@1728
   232
            return;
jaroslav@1728
   233
        }
jaroslav@1728
   234
        exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
jaroslav@1728
   235
        if (exp != null) {
jaroslav@1728
   236
            for (String def : exp.split(",")) {
jaroslav@1728
   237
                def = def.trim();
jaroslav@1728
   238
                if (def.endsWith(".*")) {
jaroslav@1728
   239
                    keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
jaroslav@1728
   240
                }
jaroslav@1728
   241
            }
jaroslav@1599
   242
        }
jaroslav@1599
   243
    }
jaroslav@1678
   244
    
jaroslav@1678
   245
    static byte[] readFrom(InputStream is) throws IOException {
jaroslav@1678
   246
        int expLen = is.available();
jaroslav@1678
   247
        if (expLen < 1) {
jaroslav@1678
   248
            expLen = 1;
jaroslav@1678
   249
        }
jaroslav@1678
   250
        byte[] arr = new byte[expLen];
jaroslav@1678
   251
        int pos = 0;
jaroslav@1678
   252
        for (;;) {
jaroslav@1678
   253
            int read = is.read(arr, pos, arr.length - pos);
jaroslav@1678
   254
            if (read == -1) {
jaroslav@1678
   255
                break;
jaroslav@1678
   256
            }
jaroslav@1678
   257
            pos += read;
jaroslav@1678
   258
            if (pos == arr.length) {
jaroslav@1678
   259
                byte[] tmp = new byte[arr.length * 2];
jaroslav@1678
   260
                System.arraycopy(arr, 0, tmp, 0, arr.length);
jaroslav@1678
   261
                arr = tmp;
jaroslav@1678
   262
            }
jaroslav@1678
   263
        }
jaroslav@1678
   264
        if (pos != arr.length) {
jaroslav@1678
   265
            byte[] tmp = new byte[pos];
jaroslav@1678
   266
            System.arraycopy(arr, 0, tmp, 0, pos);
jaroslav@1678
   267
            arr = tmp;
jaroslav@1678
   268
        }
jaroslav@1678
   269
        return arr;
jaroslav@1678
   270
    }
jaroslav@1678
   271
    
jaroslav@1599
   272
jaroslav@1599
   273
    static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1678
   274
        private final List<String> classes;
jaroslav@1678
   275
        private final Map<String,byte[]> converted = new HashMap<>();
jaroslav@1678
   276
        private final BytecodeProcessor proc;
jaroslav@1710
   277
        private final ClassLoader cp;
jaroslav@1769
   278
        private final boolean ignoreBootClassPath;
jaroslav@1678
   279
jaroslav@1769
   280
        protected EmulationResources(boolean ignoreBootClassPath, ClassLoader cp, List<String> classes) {
jaroslav@1769
   281
            this.ignoreBootClassPath = ignoreBootClassPath;
jaroslav@1678
   282
            this.classes = classes;
jaroslav@1710
   283
            this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
jaroslav@1678
   284
            BytecodeProcessor p;
jaroslav@1678
   285
            try {
jaroslav@1678
   286
                Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
jaroslav@1678
   287
                p = (BytecodeProcessor) bpClass.newInstance();
jaroslav@1678
   288
            } catch (Throwable t) {
jaroslav@1678
   289
                p = null;
jaroslav@1678
   290
            }
jaroslav@1678
   291
            this.proc = p;
jaroslav@1678
   292
        }
jaroslav@1599
   293
jaroslav@1696
   294
        protected final InputStream getConverted(String name) throws IOException {
jaroslav@1684
   295
            byte[] arr = converted.get(name);
jaroslav@1684
   296
            if (arr != null) {
jaroslav@1684
   297
                return new ByteArrayInputStream(arr);
jaroslav@1684
   298
            }
jaroslav@1696
   299
            return null;
jaroslav@1696
   300
        }
jaroslav@1696
   301
        
jaroslav@1696
   302
        @Override
jaroslav@1696
   303
        public InputStream get(String name) throws IOException {
jaroslav@1696
   304
            InputStream is = getConverted(name);
jaroslav@1696
   305
            if (is != null) {
jaroslav@1696
   306
                return is;
jaroslav@1696
   307
            }
jaroslav@1886
   308
            return getFromCp(name);
jaroslav@1886
   309
        }
jaroslav@1886
   310
jaroslav@1886
   311
        private InputStream getFromCp(String name) throws IOException {
jaroslav@1710
   312
            Enumeration<URL> en = cp.getResources(name);
jaroslav@1599
   313
            URL u = null;
jaroslav@1599
   314
            while (en.hasMoreElements()) {
jaroslav@1599
   315
                u = en.nextElement();
jaroslav@1599
   316
            }
jaroslav@1599
   317
            if (u == null) {
jaroslav@1763
   318
                LOG.log(Level.FINE, "Cannot find {0}", name);
jaroslav@1599
   319
                return null;
jaroslav@1599
   320
            }
jaroslav@1769
   321
            if (ignoreBootClassPath && u.toExternalForm().contains("/rt.jar!")) {
jaroslav@1763
   322
                LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
jaroslav@1599
   323
                return null;
jaroslav@1599
   324
            }
jaroslav@1599
   325
            return u.openStream();
jaroslav@1599
   326
        }
jaroslav@1678
   327
jaroslav@1886
   328
        private final class NoConvRes implements Bck2Brwsr.Resources {
jaroslav@1886
   329
            @Override
jaroslav@1886
   330
            public InputStream get(String resource) throws IOException {
jaroslav@1886
   331
                return getFromCp(resource);
jaroslav@1886
   332
            }
jaroslav@1886
   333
        }
jaroslav@1886
   334
jaroslav@1678
   335
        private void addClassResource(String n) throws IOException {
jaroslav@1678
   336
            if (proc != null) {
jaroslav@1678
   337
                try (InputStream is = this.get(n)) {
jaroslav@1886
   338
                    Map<String, byte[]> conv = proc.process(n, readFrom(is), new NoConvRes());
jaroslav@1678
   339
                    if (conv != null) {
jaroslav@1681
   340
                        boolean found = false;
jaroslav@1681
   341
                        for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
jaroslav@1681
   342
                            String res = entrySet.getKey();
jaroslav@1681
   343
                            byte[] bytes = entrySet.getValue();
jaroslav@1681
   344
                            if (res.equals(n)) {
jaroslav@1681
   345
                                found = true;
jaroslav@1681
   346
                            }
jaroslav@1684
   347
                            assert res.endsWith(".class") : "Wrong resource: " + res;
jaroslav@1681
   348
                            converted.put(res, bytes);
jaroslav@1681
   349
                            classes.add(res.substring(0, res.length() - 6));
jaroslav@1681
   350
                        }
jaroslav@1681
   351
                        if (!found) {
jaroslav@1678
   352
                            throw new IOException("Cannot find " + n + " among " + conv);
jaroslav@1678
   353
                        }
jaroslav@1678
   354
                        return;
jaroslav@1678
   355
                    }
jaroslav@1678
   356
                }
jaroslav@1678
   357
            }
jaroslav@1678
   358
            classes.add(n.substring(0, n.length() - 6));
jaroslav@1678
   359
        }
jaroslav@1599
   360
    }
jaroslav@1599
   361
    
jaroslav@1769
   362
    private static Bck2Brwsr configureDir(final boolean ignoreBootClassPath, Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
jaroslav@1679
   363
        List<String> arr = new ArrayList<>();
jaroslav@1679
   364
        List<String> classes = new ArrayList<>();
jaroslav@1679
   365
        class DirRes extends EmulationResources {
jaroslav@1710
   366
            public DirRes(ClassLoader cp, List<String> classes) {
jaroslav@1769
   367
                super(ignoreBootClassPath, cp, classes);
jaroslav@1679
   368
            }
jaroslav@1679
   369
jaroslav@1679
   370
            @Override
jaroslav@1679
   371
            public InputStream get(String name) throws IOException {
jaroslav@1679
   372
                InputStream is = super.get(name);
jaroslav@1679
   373
                if (is != null) {
jaroslav@1679
   374
                    return is;
jaroslav@1679
   375
                }
jaroslav@1679
   376
                File r = new File(dir, name.replace('/', File.separatorChar));
jaroslav@1679
   377
                if (r.exists()) {
jaroslav@1679
   378
                    return new FileInputStream(r);
jaroslav@1679
   379
                }
jaroslav@1679
   380
                return null;
jaroslav@1679
   381
            }
jaroslav@1679
   382
        }
jaroslav@1710
   383
        DirRes dirRes = new DirRes(cp, classes);
jaroslav@1679
   384
        listDir(dir, null, dirRes, arr);
jaroslav@1679
   385
        if (c == null) {
jaroslav@1679
   386
            c = Bck2Brwsr.newCompiler();
jaroslav@1679
   387
        }
jaroslav@1679
   388
        return c
jaroslav@1679
   389
        .addRootClasses(classes.toArray(new String[0]))
jaroslav@1679
   390
        .addResources(arr.toArray(new String[0]))
jaroslav@1679
   391
        .library()
jaroslav@1679
   392
        //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1679
   393
        .resources(dirRes);
jaroslav@1679
   394
    }
jaroslav@1679
   395
jaroslav@1679
   396
    private static void listDir(
jaroslav@1679
   397
        File f, String pref, EmulationResources res, List<String> resources
jaroslav@1679
   398
    ) throws IOException {
jaroslav@1679
   399
        File[] arr = f.listFiles();
jaroslav@1679
   400
        if (arr == null) {
jaroslav@1679
   401
            if (f.getName().endsWith(".class")) {
jaroslav@1679
   402
                res.addClassResource(pref + f.getName());
jaroslav@1679
   403
            } else {
jaroslav@1679
   404
                resources.add(pref + f.getName());
jaroslav@1679
   405
            }
jaroslav@1679
   406
        } else {
jaroslav@1679
   407
            for (File ch : arr) {
jaroslav@1679
   408
                listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
jaroslav@1679
   409
            }
jaroslav@1679
   410
        }
jaroslav@1679
   411
    }
jaroslav@1678
   412
    
jaroslav@1599
   413
}