Java String Sanitize sanitizeMethodName(String methodName)

Here you can find the source of sanitizeMethodName(String methodName)

Description

Sanitizes a potential method name so it is both valid and follows Java conventions (camel-cased, no underscores, etc.).

License

Open Source License

Declaration

public static String sanitizeMethodName(String methodName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.// w  w w  . j a va2s  .  c  o  m
 *******************************************************************************/

public class Main {
    /**
     * Sanitizes a potential method name so it is both valid and follows Java
     * conventions (camel-cased, no underscores, etc.).
     */
    public static String sanitizeMethodName(String methodName) {
        assert (methodName != null && methodName.length() > 0);
        StringBuilder sb = new StringBuilder();

        // Ensure first character is valid and lower-case
        char firstChar = methodName.charAt(0);
        if (Character.isJavaIdentifierStart(firstChar)) {
            if (Character.isUpperCase(firstChar)) {
                firstChar = Character.toLowerCase(firstChar);
            }
            sb.append(firstChar);
        }

        // Replace remaining invalid characters
        boolean previousCharWasDropped = false;
        for (int i = 1; i < methodName.length(); i++) {
            char ch = methodName.charAt(i);
            if (Character.isLetterOrDigit(ch)) {
                // If we interpreted the last character as a separator and dropped it,
                // we capitalize the next character so the final name is camel-cased.
                if (previousCharWasDropped) {
                    ch = Character.toUpperCase(ch);
                }
                sb.append(ch);
                previousCharWasDropped = false;
            } else {
                // Assume anything that is not alphanumeric is meant as a separator and
                // drop it. This includes characters that are invalid Java identifier
                // characters (e.g. dashes) as well as characters that are technically
                // valid, but which would look ugly in a method name (e.g. underscores).
                previousCharWasDropped = true;
            }
        }

        // If the original name was composed entirely of non-alphanumeric chars, we
        // need to return *something*, even if it's not very descriptive.
        if (sb.length() == 0) {
            sb.append("_method");
        }

        return sb.toString();
    }
}

Related

  1. sanitizeInput(String string)
  2. sanitizeJavascript(String originalString)
  3. sanitizeLine(String line)
  4. sanitizeLineSpaces(String line)
  5. sanitizeLoggerNamePart(String name)
  6. sanitizeMimeType(final String mimeType)
  7. sanitizeName(String input)
  8. SanitizeName(String Name)
  9. sanitizeName(String name)