📄 configurationparser.java
字号:
{ // It's not a constructor. // Make sure we have a proper name. checkJavaIdentifier("class member name"); // Read the opening parenthesis or the separating // semi-colon. readNextWord("opening '" + ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD + "' or separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "'"); } // Are we looking at a field, a method, or something else? if (ConfigurationConstants.SEPARATOR_KEYWORD.equals(nextWord)) { // It's a field. checkFieldAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); // We already have a field descriptor. String descriptor = ClassUtil.internalType(type); // Add the field. classSpecification.addField( new ClassMemberSpecification(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags, name, descriptor)); } else if (ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD.equals(nextWord)) { // It's a method. checkMethodAccessFlags(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags); // Parse the method arguments. String descriptor = ClassUtil.internalMethodDescriptor(type, parseCommaSeparatedList(true, false)); if (!ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD.equals(nextWord)) { throw new ParseException("Expecting separating '" + ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD + "' or closing '" + ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD + "' before " + reader.locationDescription()); } // Read the separator after the closing parenthesis. readNextWord("separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "'"); if (!ConfigurationConstants.SEPARATOR_KEYWORD.equals(nextWord)) { throw new ParseException("Expecting separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "' before " + reader.locationDescription()); } // Add the method. classSpecification.addMethod( new ClassMemberSpecification(requiredSetMemberAccessFlags, requiredUnsetMemberAccessFlags, name, descriptor)); } else { // It doesn't look like a field or a method. throw new ParseException("Expecting opening '" + ConfigurationConstants.OPEN_ARGUMENTS_KEYWORD + "' or separator '" + ConfigurationConstants.SEPARATOR_KEYWORD + "' before " + reader.locationDescription()); } } return true; } /** * Reads a comma-separated list of java identifiers or of file names, * optionally ending after the closing parenthesis. */ private List parseCommaSeparatedList(boolean checkJavaIdentifiers, boolean replaceSystemProperties) throws ParseException, IOException { List arguments = new ArrayList(); while (true) { // Read an argument. readNextWord("argument"); if (arguments.size() == 0 && (ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD.equals(nextWord) || ConfigurationConstants.SEPARATOR_KEYWORD.equals(nextWord))) { break; } if (checkJavaIdentifiers) { checkJavaIdentifier("argument type"); } if (replaceSystemProperties) { nextWord = replaceSystemProperties(nextWord); } arguments.add(nextWord); // Read a comma (or a closing parenthesis, or a different word). readNextWord("separating '" + ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD + "' or closing '" + ConfigurationConstants.CLOSE_ARGUMENTS_KEYWORD + "'"); if (!ConfigurationConstants.ARGUMENT_SEPARATOR_KEYWORD.equals(nextWord)) { break; } } return arguments; } /** * Throws a ParseException for an unexpected keyword. */ private int unknownAccessFlag() throws ParseException { throw new ParseException("Unexpected keyword " + reader.locationDescription()); } /** * Replaces any system properties in the given word by their values * (e.g. the substring "<java.home>" is replaced by its value). */ private String replaceSystemProperties(String word) throws ParseException { int fromIndex = 0; while (true) { fromIndex = word.indexOf(ConfigurationConstants.OPEN_SYSTEM_PROPERTY, fromIndex); if (fromIndex < 0) { break; } int toIndex = word.indexOf(ConfigurationConstants.CLOSE_SYSTEM_PROPERTY, fromIndex+1); if (toIndex < 0) { throw new ParseException("Expecting closing '" + ConfigurationConstants.CLOSE_SYSTEM_PROPERTY + "' after opening '" + ConfigurationConstants.OPEN_SYSTEM_PROPERTY + "' in " + reader.locationDescription()); } String propertyName = word.substring(fromIndex+1, toIndex); String propertyValue = System.getProperty(propertyName); if (propertyValue == null) { throw new ParseException("Value of system property '" + propertyName + "' is undefined in " + reader.locationDescription()); } word = word.substring(0, fromIndex) + propertyValue + word.substring(toIndex+1); } return word; } /** * Reads the next word of the configuration in the 'nextWord' field, * throwing an exception if there is no next word. */ private void readNextWord(String expectedDescription) throws ParseException, IOException { readNextWord(); if (configurationEnd()) { throw new ParseException("Expecting " + expectedDescription + " before " + reader.locationDescription()); } } /** * Reads the next word of the configuration in the 'nextWord' field. */ private void readNextWord() throws IOException { nextWord = reader.nextWord(); } /** * Returns whether the end of the configuration has been reached. */ private boolean configurationEnd() { return nextWord == null || nextWord.startsWith(ConfigurationConstants.OPTION_PREFIX) || nextWord.equals(ConfigurationConstants.AT_DIRECTIVE); } /** * 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()); } } /** * Returns whether the given word is a valid Java identifier. * Wildcard characters are accepted. */ private boolean isJavaIdentifier(String aWord) { for (int index = 0; index < aWord.length(); index++) { char c = aWord.charAt(index); if (!(Character.isJavaIdentifierPart(c) || c == '.' || c == '[' || c == ']' || c == '<' || c == '>' || c == '*' || c == '?' || c == '%')) { return false; } } return true; } /** * Checks whether the given access flags are valid field access flags, * throwing a ParseException if they aren't. */ private void checkFieldAccessFlags(int requiredSetMemberAccessFlags, int requiredUnsetMemberAccessFlags) throws ParseException { if (((requiredSetMemberAccessFlags | requiredUnsetMemberAccessFlags) & ~ClassConstants.VALID_INTERNAL_ACC_FIELD) != 0) { throw new ParseException("Invalid method access modifier for field before " + reader.locationDescription()); } } /** * Checks whether the given access flags are valid method access flags, * throwing a ParseException if they aren't. */ private void checkMethodAccessFlags(int requiredSetMemberAccessFlags, int requiredUnsetMemberAccessFlags) throws ParseException { if (((requiredSetMemberAccessFlags | requiredUnsetMemberAccessFlags) & ~ClassConstants.VALID_INTERNAL_ACC_METHOD) != 0) { throw new ParseException("Invalid field access modifier for method before " + reader.locationDescription()); } } /** * A main method for testing configuration parsing. */ public static void main(String[] args) { try { ConfigurationParser parser = new ConfigurationParser(args); parser.parse(new Configuration()); } catch (Exception ex) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -