public void applyConfigurationFile(@NonNull File file) throws IOException, ParseException { ConfigurationParser parser = new ConfigurationParser(file, System.getProperties()); try { parser.parse(configuration); } finally { parser.close(); } }
/** * Creates specifications to keep classes and class members, based on the * given ProGuard-style class specification. */ private ClassSpecification createClassSpecification(String classSpecificationString) throws ParseException { try { ConfigurationParser parser = new ConfigurationParser(new String[] { classSpecificationString }, null); try { return parser.parseClassSpecificationArguments(); } finally { parser.close(); } } catch (IOException e) { throw new ParseException(e.getMessage()); } }
/** * Checks whether the given word is a valid Java identifier and throws * a ParseException if it isn't. Wildcard characters are accepted. */ private void checkJavaIdentifier(String expectedDescription) throws ParseException { checkJavaIdentifier(expectedDescription, true); }
private File parseFile() throws ParseException, IOException { // Read the obligatory file name. readNextWord("file name"); // Make sure the file is properly resolved. File file = file(nextWord); readNextWord(); return file; }
private String parseOptionalArgument() throws IOException { // Read the optional argument. readNextWord(); // Didn't the user specify an argument? if (configurationEnd()) { return ""; } String fileName = nextWord; readNextWord(); return fileName; }
private File parseOptionalFile() throws ParseException, IOException { // Read the optional file name. readNextWord(true, true); // Didn't the user specify a file name? if (configurationEnd()) { return Configuration.STD_OUT; } // Make sure the file is properly resolved. File file = file(nextWord); readNextWord(); return file; }
ConfigurationConstants.INCLUDE_DIRECTIVE .startsWith(nextWord)) configuration.lastModified = parseIncludeArgument(configuration.lastModified); else if (ConfigurationConstants.BASE_DIRECTORY_DIRECTIVE .startsWith(nextWord)) parseBaseDirectoryArgument(); else if (ConfigurationConstants.INJARS_OPTION .startsWith(nextWord)) configuration.programJars = parseClassPathArgument(configuration.programJars, false); else if (ConfigurationConstants.OUTJARS_OPTION .startsWith(nextWord)) configuration.programJars = parseClassPathArgument(configuration.programJars, true); else if (ConfigurationConstants.LIBRARYJARS_OPTION .startsWith(nextWord)) configuration.libraryJars = parseClassPathArgument(configuration.libraryJars, false); else if (ConfigurationConstants.RESOURCEJARS_OPTION .startsWith(nextWord)) throw new ParseException("The '-resourcejars' option is no longer supported. Please use the '-injars' option for all input"); else if (ConfigurationConstants.DONT_SKIP_NON_PUBLIC_LIBRARY_CLASSES_OPTION .startsWith(nextWord)) configuration.skipNonPublicLibraryClasses = parseNoArgument(false); else if (ConfigurationConstants.DONT_SKIP_NON_PUBLIC_LIBRARY_CLASS_MEMBERS_OPTION.startsWith(nextWord)) configuration.skipNonPublicLibraryClassMembers = parseNoArgument(false); else if (ConfigurationConstants.TARGET_OPTION .startsWith(nextWord)) configuration.targetClassVersion = parseClassVersion(); else if (ConfigurationConstants.FORCE_PROCESSING_OPTION .startsWith(nextWord)) configuration.lastModified = parseNoArgument(Long.MAX_VALUE); else if (ConfigurationConstants.KEEP_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, true, false, false); else if (ConfigurationConstants.KEEP_CLASS_MEMBERS_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, false, false); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBERS_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, true, false); else if (ConfigurationConstants.KEEP_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, true, false, true); else if (ConfigurationConstants.KEEP_CLASS_MEMBER_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, false, true); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBER_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, true, true); else if (ConfigurationConstants.PRINT_SEEDS_OPTION .startsWith(nextWord)) configuration.printSeeds = parseOptionalFile(); else if (ConfigurationConstants.KEEP_DIRECTORIES_OPTION .startsWith(nextWord)) configuration.keepDirectories = parseCommaSeparatedList("directory name", true, true, false, false, true, false, false, configuration.keepDirectories); else if (ConfigurationConstants.DONT_SHRINK_OPTION .startsWith(nextWord)) configuration.shrink = parseNoArgument(false); else if (ConfigurationConstants.PRINT_USAGE_OPTION .startsWith(nextWord)) configuration.printUsage = parseOptionalFile(); else if (ConfigurationConstants.WHY_ARE_YOU_KEEPING_OPTION .startsWith(nextWord)) configuration.whyAreYouKeeping = parseClassSpecificationArguments(configuration.whyAreYouKeeping); else if (ConfigurationConstants.DONT_OPTIMIZE_OPTION .startsWith(nextWord)) configuration.optimize = parseNoArgument(false); else if (ConfigurationConstants.OPTIMIZATION_PASSES .startsWith(nextWord)) configuration.optimizationPasses = parseIntegerArgument(); else if (ConfigurationConstants.OPTIMIZATIONS .startsWith(nextWord)) configuration.optimizations = parseCommaSeparatedList("optimization name", true, false, false, false, false, false, false, configuration.optimizations);
while (!configurationEnd(true)) parseCommaSeparatedList("annotation type", true, false, false, false, true, false, false, false, true, null), false); continue; readNextWord("class member description"); checkFieldAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkMethodAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkFieldAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkMethodAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); readNextWord("separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "'"); checkJavaIdentifier("java type"); String type = nextWord; readNextWord("class member name"); String name = nextWord; checkJavaIdentifier("class member name");
strippedWord.equals(ClassConstants.EXTERNAL_ACC_ANNOTATION) ? ClassConstants.INTERNAL_ACC_ANNOTATTION : strippedWord.equals(ClassConstants.EXTERNAL_ACC_ENUM) ? ClassConstants.INTERNAL_ACC_ENUM : unknownAccessFlag(); readNextWord("annotation type or keyword '" + ClassConstants.EXTERNAL_ACC_INTERFACE + "'", false); parseCommaSeparatedList("annotation type", false, false, false, true, false, false, true, null)); readNextWord("keyword '" + ConfigurationConstants.CLASS_KEYWORD + "', '" + ClassConstants.EXTERNAL_ACC_INTERFACE + "', or '" + ClassConstants.EXTERNAL_ACC_ENUM + "'", true); parseCommaSeparatedList("class name or interface name", true, false, false, true, false, false, false, null)); if (!configurationEnd()) readNextWord("class name or interface name", true); parseCommaSeparatedList("annotation type", true, false, false, true, false, false, true, null)); parseCommaSeparatedList("class name or interface name", false, false, false, true, false, false, false, null)); if (!configurationEnd()) readNextWord("class member description" +
readNextWord("jar or directory name"); ClassPathEntry entry = new ClassPathEntry(file(nextWord), isOutput); readNextWord(); if (!configurationEnd() && ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD.equals(nextWord)) parseCommaSeparatedList("filter", true, false, true, false, true, false, false, null); readNextWord(); if (configurationEnd())
readNextWord(expectedDescription, isFileName, true, false); readNextWord(expectedDescription, isFileName, true, false); readNextWord("closing '" + ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD + "'"); readNextWord(isFileName, true); if (configurationEnd()) checkJavaIdentifier("java type", allowGenerics); nextWord = replaceSystemProperties(nextWord); readNextWord("separating '" + ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD + "' or closing '" + ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD + "'"); readNextWord(); readNextWord(expectedDescription, isFileName, true, false);
private List parseClassSpecificationArguments(List classSpecifications) throws ParseException, IOException { // Create a new List if necessary. if (classSpecifications == null) { classSpecifications = new ArrayList(); } // Read and add the class configuration. readNextWord("keyword '" + ConfigurationConstants.CLASS_KEYWORD + "', '" + ClassConstants.EXTERNAL_ACC_INTERFACE + "', or '" + ClassConstants.EXTERNAL_ACC_ENUM + "'", true); classSpecifications.add(parseClassSpecificationArguments()); return classSpecifications; }
private static File executeProguard(File inputFile, String name, String replacement, List<URL> additionalUrls, boolean addToClasspath) throws MalformedURLException, URISyntaxException, IOException, ParseException { String outFilename= inputFile.getAbsolutePath().replace(".jar", replacement); File outputFile= new File(outFilename); Properties properties= System.getProperties(); properties.put("in-jar-filename", inputFile.getAbsolutePath()); properties.put("out-jar-filename", outputFile.getAbsolutePath()); ConfigurationParser parser= new ConfigurationParser(DragomeCompilerLauncher.class.getResource(name).toURI().toURL(), properties); Configuration configuration= new Configuration(); parser.parse(configuration); ArrayList<URL> urls= new ArrayList<URL>(additionalUrls); for (URL url : urls) { ConfigurationParser parserForAdditionalKeepCodeConfigFile= new ConfigurationParser(url, properties); parserForAdditionalKeepCodeConfigFile.parse(configuration); } if (addToClasspath) System.setProperty("java.class.path", outFilename + ";" + System.getProperty("java.class.path")); new ProGuard(configuration).execute(); return outputFile; } }
private List parseIfCondition(List keepClassSpecifications) throws ParseException, IOException { // Read the condition. readNextWord("keyword '" + ConfigurationConstants.CLASS_KEYWORD + "', '" + JavaConstants.ACC_INTERFACE + "', or '" + JavaConstants.ACC_ENUM + "'", false, false, true); ClassSpecification condition = parseClassSpecificationArguments(); // Read the corresponding keep option. if (nextWord == null) { throw new ParseException("Expecting '-keep' option after '-if' option, before " + reader.locationDescription()); } if (ConfigurationConstants.KEEP_OPTION .startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, true, false, false, condition); else if (ConfigurationConstants.KEEP_CLASS_MEMBERS_OPTION .startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, false, false, false, condition); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBERS_OPTION .startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, false, true, false, condition); else if (ConfigurationConstants.KEEP_NAMES_OPTION .startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, true, false, true, condition); else if (ConfigurationConstants.KEEP_CLASS_MEMBER_NAMES_OPTION .startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, false, false, true, condition); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBER_NAMES_OPTION.startsWith(nextWord)) keepClassSpecifications = parseKeepClassSpecificationArguments(keepClassSpecifications, false, true, true, condition); else { throw new ParseException("Expecting '-keep' option after '-if' option, before " + reader.locationDescription()); } return keepClassSpecifications; }
@Override public void keep(@NonNull String keep) { if (configuration.keep == null) { configuration.keep = Lists.newArrayList(); } ClassSpecification classSpecification; try { ConfigurationParser parser = new ConfigurationParser(new String[]{keep}, null); classSpecification = parser.parseClassSpecificationArguments(); } catch (IOException e) { // No IO happens when parsing in-memory strings. throw new AssertionError(e); } catch (ParseException e) { throw new RuntimeException(e); } //noinspection unchecked configuration.keep.add(new KeepClassSpecification( true /*markClasses*/, false /*markConditionally*/, false /*includedescriptorclasses */, false /*allowshrinking*/, false /*allowoptimization*/, false /*allowobfuscation*/, classSpecification)); }
/** * Checks whether the given word is a valid Java identifier and throws * a ParseException if it isn't. Wildcard characters are accepted. */ private void checkJavaIdentifier(String expectedDescription, boolean allowGenerics) throws ParseException { if (!isJavaIdentifier(nextWord)) { throw new ParseException("Expecting " + expectedDescription + " before " + reader.locationDescription()); } if (!allowGenerics && containsGenerics(nextWord)) { throw new ParseException("Use of generics not allowed for " + expectedDescription + " at " + reader.locationDescription()); } }
/** * Returns whether the end of the configuration has been reached. */ private boolean configurationEnd() { return configurationEnd(false); }
/** * Checks whether the given word is a valid Java identifier and throws * a ParseException if it isn't. Wildcard characters are accepted. */ private void checkJavaIdentifier(String expectedDescription) throws ParseException { if (!isJavaIdentifier(nextWord)) { throw new ParseException("Expecting " + expectedDescription + " before " + reader.locationDescription()); } }
ConfigurationConstants.INCLUDE_DIRECTIVE .startsWith(nextWord)) configuration.lastModified = parseIncludeArgument(configuration.lastModified); else if (ConfigurationConstants.BASE_DIRECTORY_DIRECTIVE .startsWith(nextWord)) parseBaseDirectoryArgument(); else if (ConfigurationConstants.INJARS_OPTION .startsWith(nextWord)) configuration.programJars = parseClassPathArgument(configuration.programJars, false); else if (ConfigurationConstants.OUTJARS_OPTION .startsWith(nextWord)) configuration.programJars = parseClassPathArgument(configuration.programJars, true); else if (ConfigurationConstants.LIBRARYJARS_OPTION .startsWith(nextWord)) configuration.libraryJars = parseClassPathArgument(configuration.libraryJars, false); else if (ConfigurationConstants.RESOURCEJARS_OPTION .startsWith(nextWord)) throw new ParseException("The '-resourcejars' option is no longer supported. Please use the '-injars' option for all input"); else if (ConfigurationConstants.SKIP_NON_PUBLIC_LIBRARY_CLASSES_OPTION .startsWith(nextWord)) configuration.skipNonPublicLibraryClasses = parseNoArgument(true); else if (ConfigurationConstants.DONT_SKIP_NON_PUBLIC_LIBRARY_CLASSES_OPTION .startsWith(nextWord)) configuration.skipNonPublicLibraryClasses = parseNoArgument(false); else if (ConfigurationConstants.DONT_SKIP_NON_PUBLIC_LIBRARY_CLASS_MEMBERS_OPTION.startsWith(nextWord)) configuration.skipNonPublicLibraryClassMembers = parseNoArgument(false); else if (ConfigurationConstants.TARGET_OPTION .startsWith(nextWord)) configuration.targetClassVersion = parseClassVersion(); else if (ConfigurationConstants.FORCE_PROCESSING_OPTION .startsWith(nextWord)) configuration.lastModified = parseNoArgument(Long.MAX_VALUE); else if (ConfigurationConstants.IF_OPTION .startsWith(nextWord)) configuration.keep = parseIfCondition(configuration.keep); else if (ConfigurationConstants.KEEP_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, true, false, false, null); else if (ConfigurationConstants.KEEP_CLASS_MEMBERS_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, false, false, null); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBERS_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, true, false, null); else if (ConfigurationConstants.KEEP_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, true, false, true, null); else if (ConfigurationConstants.KEEP_CLASS_MEMBER_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, false, true, null); else if (ConfigurationConstants.KEEP_CLASSES_WITH_MEMBER_NAMES_OPTION .startsWith(nextWord)) configuration.keep = parseKeepClassSpecificationArguments(configuration.keep, false, true, true, null); else if (ConfigurationConstants.PRINT_SEEDS_OPTION .startsWith(nextWord)) configuration.printSeeds = parseOptionalFile(); else if (ConfigurationConstants.KEEP_DIRECTORIES_OPTION .startsWith(nextWord)) configuration.keepDirectories = parseCommaSeparatedList("directory name", true, true, false, true, false, true, true, false, false, configuration.keepDirectories); else if (ConfigurationConstants.DONT_SHRINK_OPTION .startsWith(nextWord)) configuration.shrink = parseNoArgument(false); else if (ConfigurationConstants.PRINT_USAGE_OPTION .startsWith(nextWord)) configuration.printUsage = parseOptionalFile(); else if (ConfigurationConstants.WHY_ARE_YOU_KEEPING_OPTION .startsWith(nextWord)) configuration.whyAreYouKeeping = parseClassSpecificationArguments(true, configuration.whyAreYouKeeping); else if (ConfigurationConstants.DONT_OPTIMIZE_OPTION .startsWith(nextWord)) configuration.optimize = parseNoArgument(false);
while (!configurationEnd(true)) parseCommaSeparatedList("annotation type", true, false, false, false, true, false, false, false, true, null), false); continue; readNextWord("class member description"); checkFieldAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkMethodAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkFieldAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); checkMethodAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); readNextWord("separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "'"); checkJavaIdentifier("java type"); String type = nextWord; readNextWord("class member name"); String name = nextWord; checkJavaIdentifier("class member name");