~ubuntu-branches/ubuntu/maverick/proguard/maverick

« back to all changes in this revision

Viewing changes to src/proguard/ConfigurationWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-11-13 09:42:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051113094259-432zf4yyw4890mmn
Tags: 3.4-1
* New upstream release (Closes: #338355)
* debian/control: bump standards version
* debian/copyright: update FSF address
* increase java stack size for proguard and proguardgui

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ConfigurationWriter.java,v 1.13 2004/11/20 15:41:24 eric Exp $
 
1
/* $Id: ConfigurationWriter.java,v 1.18 2005/08/21 19:28:48 eric Exp $
2
2
 *
3
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
4
4
 *
5
 
 * Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu)
 
5
 * Copyright (c) 2002-2005 Eric Lafortune (eric@graphics.cornell.edu)
6
6
 *
7
7
 * This program is free software; you can redistribute it and/or modify it
8
8
 * under the terms of the GNU General Public License as published by the Free
49
49
        ConfigurationConstants.KEEP_CLASSES_WITH_MEMBERS_OPTION
50
50
    };
51
51
 
 
52
    private static final String[] WHY_ARE_YOU_KEEPING_OPTIONS = new String[]
 
53
    {
 
54
        ConfigurationConstants.WHY_ARE_YOU_KEEPING_OPTION,
 
55
        ConfigurationConstants.WHY_ARE_YOU_KEEPING_OPTION,
 
56
        ConfigurationConstants.WHY_ARE_YOU_KEEPING_OPTION
 
57
    };
 
58
 
52
59
    private static final String[] ASSUME_NO_SIDE_EFFECT_OPTIONS = new String[]
53
60
    {
54
61
        ConfigurationConstants.ASSUME_NO_SIDE_EFFECTS_OPTION,
58
65
 
59
66
 
60
67
    private PrintWriter writer;
 
68
    private File        baseDir;
61
69
 
62
70
 
63
71
    /**
64
72
     * Creates a new ConfigurationWriter for the given file name.
65
73
     */
66
 
    public ConfigurationWriter(String configurationFile) throws IOException
 
74
    public ConfigurationWriter(File configurationFile) throws IOException
67
75
    {
68
76
        this(new PrintWriter(new FileWriter(configurationFile)));
 
77
 
 
78
        baseDir = configurationFile.getParentFile();
69
79
    }
70
80
 
71
81
 
143
153
        writeOption(ConfigurationConstants.PRINT_SEEDS_OPTION,                                configuration.printSeeds);
144
154
        writer.println();
145
155
 
 
156
        // Write the "why are you keeping" options.
 
157
        writeOptions(WHY_ARE_YOU_KEEPING_OPTIONS, configuration.whyAreYouKeeping);
 
158
 
146
159
        // Write the keep options.
147
160
        writeOptions(KEEP_OPTIONS, configuration.keep);
148
161
 
167
180
                     outputEntryOptionName :
168
181
                     inputEntryOptionName;
169
182
 
170
 
                writer.print(optionName + " " + quotedString(entry.getName()));
 
183
                writer.print(optionName + " " + relativeFileName(entry.getFile()));
171
184
 
172
185
                // Append the filters, if any.
173
186
                boolean filtered = false;
230
243
    }
231
244
 
232
245
 
 
246
    private void writeOption(String optionName, File file)
 
247
    {
 
248
        if (file != null)
 
249
        {
 
250
            if (file.getPath().length() > 0)
 
251
            {
 
252
                writer.println(optionName + " " + relativeFileName(file));
 
253
            }
 
254
            else
 
255
            {
 
256
                writer.println(optionName);
 
257
            }
 
258
        }
 
259
    }
 
260
 
 
261
 
233
262
    private void writeOptions(String[] optionNames,
234
263
                              List     classSpecifications)
235
264
    {
237
266
        {
238
267
            for (int index = 0; index < classSpecifications.size(); index++)
239
268
            {
240
 
                writeOption(optionNames,
241
 
                                              (ClassSpecification)classSpecifications.get(index));
 
269
                writeOption(optionNames, (ClassSpecification)classSpecifications.get(index));
242
270
            }
243
271
        }
244
272
    }
279
307
        // Write out the class name.
280
308
        writer.print(classSpecification.className != null ?
281
309
            ClassUtil.externalClassName(classSpecification.className) :
282
 
            "*");
 
310
            ConfigurationConstants.ANY_CLASS_KEYWORD);
283
311
 
284
312
        // Write out the extends template, if any.
285
313
        if (classSpecification.extendsClassName != null)
294
322
            writer.println(" {");
295
323
 
296
324
            writeFieldSpecification( classSpecification.fieldSpecifications);
297
 
            writeMethodSpecification(classSpecification.methodSpecifications,
298
 
                                   classSpecification.className);
 
325
            writeMethodSpecification(classSpecification.methodSpecifications);
299
326
            writer.println("}");
300
327
        }
301
328
        else
346
373
                writer.print(ClassUtil.externalFieldAccessFlags(classMemberSpecification.requiredSetAccessFlags));
347
374
 
348
375
                // Write out the field name and descriptor.
349
 
                writer.print(classMemberSpecification.name       != null ||
350
 
                             classMemberSpecification.descriptor != null ?
 
376
                String name       = classMemberSpecification.name;
 
377
                String descriptor = classMemberSpecification.descriptor;
 
378
                
 
379
                if (name == null)
 
380
                {
 
381
                    name = ConfigurationConstants.ANY_CLASS_MEMBER_KEYWORD;
 
382
                }
 
383
    
 
384
                writer.print(descriptor != null ?
351
385
                    ClassUtil.externalFullFieldDescription(0,
352
 
                                                           classMemberSpecification.name,
353
 
                                                           classMemberSpecification.descriptor) :
 
386
                                                           name,
 
387
                                                           descriptor) :
354
388
                    ConfigurationConstants.ANY_FIELD_KEYWORD);
355
389
 
356
390
                writer.println(";");
359
393
    }
360
394
 
361
395
 
362
 
    private void writeMethodSpecification(List classMemberSpecifications, String className)
 
396
    private void writeMethodSpecification(List classMemberSpecifications)
363
397
    {
364
398
        if (classMemberSpecifications != null)
365
399
        {
377
411
                writer.print(ClassUtil.externalMethodAccessFlags(classMemberSpecification.requiredSetAccessFlags));
378
412
 
379
413
                // Write out the method name and descriptor.
380
 
                writer.print(classMemberSpecification.name       != null ||
381
 
                             classMemberSpecification.descriptor != null ?
382
 
                    ClassUtil.externalFullMethodDescription(className,
 
414
                String name       = classMemberSpecification.name;
 
415
                String descriptor = classMemberSpecification.descriptor;
 
416
                
 
417
                if (name == null)
 
418
                {
 
419
                    name = ConfigurationConstants.ANY_CLASS_MEMBER_KEYWORD;
 
420
                }
 
421
    
 
422
                writer.print(descriptor != null ?
 
423
                    ClassUtil.externalFullMethodDescription(ClassConstants.INTERNAL_METHOD_NAME_INIT,
383
424
                                                            0,
384
 
                                                            classMemberSpecification.name,
385
 
                                                            classMemberSpecification.descriptor) :
 
425
                                                            name,
 
426
                                                            descriptor) :
386
427
                    ConfigurationConstants.ANY_METHOD_KEYWORD);
387
428
 
388
429
                writer.println(";");
391
432
    }
392
433
 
393
434
 
 
435
    /**
 
436
     * Returns a relative file name of the given file, if possible.
 
437
     * The file name is also quoted, if necessary.
 
438
     */
 
439
    private String relativeFileName(File file)
 
440
    {
 
441
        String fileName = file.getAbsolutePath();
 
442
 
 
443
        // See if we can convert the file name into a relative file name.
 
444
        if (baseDir != null)
 
445
        {
 
446
            String baseDirName = baseDir.getAbsolutePath() + File.separator;
 
447
            if (fileName.startsWith(baseDirName))
 
448
            {
 
449
                fileName = fileName.substring(baseDirName.length());
 
450
            }
 
451
        }
 
452
 
 
453
        return quotedString(fileName);
 
454
    }
 
455
 
 
456
 
 
457
    /**
 
458
     * Returns a quoted version of the given string, if necessary.
 
459
     */
394
460
    private String quotedString(String string)
395
461
    {
396
 
        return
397
 
            string.length()     == 0 ||
398
 
            string.indexOf(' ') >= 0  ? ("'" + string + "'") :
399
 
                                        (      string      );
 
462
        return string.length()     == 0 ||
 
463
               string.indexOf(' ') >= 0 ||
 
464
               string.indexOf('@') >= 0 ||
 
465
               string.indexOf('{') >= 0 ||
 
466
               string.indexOf('}') >= 0 ||
 
467
               string.indexOf('(') >= 0 ||
 
468
               string.indexOf(')') >= 0 ||
 
469
               string.indexOf(':') >= 0 ||
 
470
               string.indexOf(';') >= 0  ? ("'" + string + "'") :
 
471
                                           (      string      );
400
472
    }
401
473
 
402
474
 
406
478
    public static void main(String[] args) {
407
479
        try
408
480
        {
409
 
            ConfigurationWriter writer = new ConfigurationWriter(args[0]);
 
481
            ConfigurationWriter writer = new ConfigurationWriter(new File(args[0]));
410
482
 
411
483
            writer.write(new Configuration());
412
484
        }