Example usage for java.lang Character isJavaIdentifierStart

List of usage examples for java.lang Character isJavaIdentifierStart

Introduction

In this page you can find the example usage for java.lang Character isJavaIdentifierStart.

Prototype

public static boolean isJavaIdentifierStart(int codePoint) 

Source Link

Document

Determines if the character (Unicode code point) is permissible as the first character in a Java identifier.

Usage

From source file:org.voltdb.compiler.DDLCompiler.java

/**
 * Checks whether or not the start of the given identifier is java (and
 * thus DDL) compliant. An identifier may start with: _ [a-zA-Z] $
 * @param identifier the identifier to check
 * @param statement the statement where the identifier is
 * @return the given identifier unmodified
 * @throws VoltCompilerException when it is not compliant
 *//*  ww w . j a  v  a 2  s.c  o m*/
private String checkIdentifierStart(final String identifier, final String statement)
        throws VoltCompilerException {

    assert identifier != null && !identifier.trim().isEmpty();
    assert statement != null && !statement.trim().isEmpty();

    int loc = 0;
    do {
        if (!Character.isJavaIdentifierStart(identifier.charAt(loc))) {
            String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                    + "\" contains invalid identifier \"" + identifier + "\"";
            throw m_compiler.new VoltCompilerException(msg);
        }
        loc = identifier.indexOf('.', loc) + 1;
    } while (loc > 0 && loc < identifier.length());

    return identifier;
}

From source file:com.attribyte.essem.util.Util.java

/**
 * Creates a string that is a valid "Java identifier" by
 * replacing invalid characters with underscore ('_').
 * @param str The input string./* w w w. j a  va2 s.  co m*/
 * @return The valid java identifier.
 */
public static String toJavaIdentifier(final String str) {

    StringBuilder buf = new StringBuilder();
    char[] chars = str.toCharArray();
    if (Character.isJavaIdentifierStart(chars[0])) {
        buf.append(chars[0]);
    } else if (Character.isJavaIdentifierPart(chars[0])) {
        buf.append("_").append(chars[0]);
    } else {
        buf.append("_");
    }

    for (int pos = 1; pos < chars.length; pos++) {
        if (Character.isJavaIdentifierPart(chars[pos])) {
            buf.append(chars[pos]);
        } else {
            buf.append("_");
        }
    }

    return buf.toString();
}

From source file:org.opencms.module.CmsModuleXmlHandler.java

/**
 * Generates a (hopefully) valid Java class name from an invalid class name.<p>
 * /*  w  w w .  j  a  v a 2 s. c o  m*/
 * All invalid characters are replaced by an underscore "_".
 * This is for example used to make sure old (5.0) modules can still be imported,
 * by converting the name to a valid class name.<p>
 * 
 * @param className the class name to make valid
 * 
 * @return a valid Java class name from an invalid class name
 */
public static String makeValidJavaClassName(String className) {

    StringBuffer result = new StringBuffer(className.length());
    int length = className.length();
    boolean nodot = true;
    for (int i = 0; i < length; i++) {
        char ch = className.charAt(i);
        if (nodot) {
            if (ch == '.') {
                // ignore, remove
            } else if (Character.isJavaIdentifierStart(ch)) {
                nodot = false;
                result.append(ch);
            } else {
                result.append('_');
            }
        } else {
            if (ch == '.') {
                nodot = true;
                result.append(ch);
            } else if (Character.isJavaIdentifierPart(ch)) {
                nodot = false;
                result.append(ch);
            } else {
                result.append('_');
            }
        }
    }
    return result.toString();
}

From source file:org.voltdb.compiler.DDLCompiler.java

/**
 * Checks whether or not the start of the given identifier is java (and
 * thus DDL) compliant. An identifier may start with: _ [a-zA-Z] $ *
 * and contain subsequent characters including: _ [0-9a-zA-Z] $ *
 * @param identifier the identifier to check
 * @param statement the statement where the identifier is
 * @return the given identifier unmodified
 * @throws VoltCompilerException when it is not compliant
 *///w ww.  j  a v a 2 s  .c  o  m
private String checkIdentifierWithWildcard(final String identifier, final String statement)
        throws VoltCompilerException {

    assert identifier != null && !identifier.trim().isEmpty();
    assert statement != null && !statement.trim().isEmpty();

    int loc = 0;
    do {
        if (!Character.isJavaIdentifierStart(identifier.charAt(loc)) && identifier.charAt(loc) != '*') {
            String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                    + "\" contains invalid identifier \"" + identifier + "\"";
            throw m_compiler.new VoltCompilerException(msg);
        }
        loc++;
        while (loc < identifier.length() && identifier.charAt(loc) != '.') {
            if (!Character.isJavaIdentifierPart(identifier.charAt(loc)) && identifier.charAt(loc) != '*') {
                String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                        + "\" contains invalid identifier \"" + identifier + "\"";
                throw m_compiler.new VoltCompilerException(msg);
            }
            loc++;
        }
        if (loc < identifier.length() && identifier.charAt(loc) == '.') {
            loc++;
            if (loc >= identifier.length()) {
                String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                        + "\" contains invalid identifier \"" + identifier + "\"";
                throw m_compiler.new VoltCompilerException(msg);
            }
        }
    } while (loc > 0 && loc < identifier.length());

    return identifier;
}

From source file:edu.tum.cs.conqat.quamoco.qiesl.QIESLEngine.java

/**
 * Escapes a measure name, so that a valid java identifier results
 *///from  w  w w  .j  a v a2s.  c o m
protected static String toTechnicalName(String name) {
    CCSMPre.isFalse(StringUtils.isEmpty(name), "Name cannot be empty");
    if (!Character.isJavaIdentifierStart(name.charAt(0))) {
        name = "_" + name;
    }
    return name.replaceAll("[^\\p{javaJavaIdentifierPart}]", "_");
}

From source file:android.databinding.tool.reflection.ModelClass.java

private static String stripFieldName(String fieldName) {
    // TODO: Make this configurable through IntelliJ
    if (fieldName.length() > 2) {
        final char start = fieldName.charAt(2);
        if (fieldName.startsWith("m_") && Character.isJavaIdentifierStart(start)) {
            return Character.toLowerCase(start) + fieldName.substring(3);
        }//from w  w  w. ja  v a2  s  .  c om
    }
    if (fieldName.length() > 1) {
        final char start = fieldName.charAt(1);
        final char fieldIdentifier = fieldName.charAt(0);
        final boolean strip;
        if (fieldIdentifier == '_') {
            strip = true;
        } else if (fieldIdentifier == 'm' && Character.isJavaIdentifierStart(start)
                && !Character.isLowerCase(start)) {
            strip = true;
        } else {
            strip = false; // not mUppercase format
        }
        if (strip) {
            return Character.toLowerCase(start) + fieldName.substring(2);
        }
    }
    return fieldName;
}

From source file:com.google.dart.tools.ui.text.dart.CompletionProposalLabelProvider.java

/**
 * Creates a display label for the given method proposal. The display label consists of:
 * <ul>//from   w  w w .  j  a v  a 2  s.c om
 * <li>the method name</li>
 * <li>the parameter list (see {@link #createParameterList(CompletionProposal)})</li>
 * <li>the upper bound of the return type (see {@link SignatureUtil#getUpperBound(String)})</li>
 * <li>the raw simple name of the declaring type</li>
 * </ul>
 * <p>
 * Examples: For the <code>get(int)</code> method of a variable of type
 * <code>List<? extends Number></code>, the following display name is returned:
 * <code>get(int index)  Number - List</code>.<br>
 * For the <code>add(E)</code> method of a variable of type <code>List<? super Number></code>, the
 * following display name is returned: <code>add(Number o)  void - List</code>.<br>
 * </p>
 * 
 * @param methodProposal the method proposal to display
 * @return the display label for the given method proposal
 */
StyledString createMethodProposalLabel(CompletionProposal methodProposal) {
    StyledString buffer = new StyledString();

    // method name
    buffer.append(methodProposal.getName());

    boolean hasParameters = methodProposal.getKind() != CompletionProposal.METHOD_NAME_REFERENCE;

    // parameters
    if (hasParameters && Character.isJavaIdentifierStart(methodProposal.getName()[0])) {
        if (!methodProposal.isGetOrSet()) {
            int start = buffer.length();
            buffer.append('(');
            appendUnboundedParameterList(buffer, methodProposal);
            buffer.append(')');
            buffer.setStyle(start, buffer.length() - start, StyledString.DECORATIONS_STYLER);
        }
    }

    // return type
    if (!methodProposal.isConstructor()) {
        char[] returnType = createTypeDisplayName(methodProposal.getReturnTypeName());
        if (!Arrays.equals(Signature.ANY, returnType)) {
            if (isVoid(returnType)) {
                buffer.append(VOID_INDICATOR, StyledString.QUALIFIER_STYLER);
            } else if (isDynamic(returnType)) {
                buffer.append(DYNAMIC_INDICATOR, StyledString.QUALIFIER_STYLER);
            } else {
                buffer.append(Element.RIGHT_ARROW, StyledString.QUALIFIER_STYLER);
                buffer.append(returnType != null ? returnType : DYNAMIC, StyledString.QUALIFIER_STYLER);
            }
        }
    }
    if (methodProposal.isPotentialMatch()) {
        potentialize(buffer, methodProposal);
    }
    return buffer;
}

From source file:com.hybridbpm.core.util.HybridbpmCoreUtil.java

public static String generateClassName(String name, String version) {
    StringBuilder nameValue = new StringBuilder();
    nameValue.append(name != null ? name : "").append("_").append(version != null ? version : "_");

    StringBuilder result = new StringBuilder();
    for (Character c : nameValue.toString().toCharArray()) {
        if (Character.isJavaIdentifierPart(c)) {
            result.append(c);//from   www .j  a  v a2s .com
        } else {
            result.append("_");
        }
    }
    while (!Character.isJavaIdentifierStart(result.charAt(0))) {
        result.deleteCharAt(0);
    }
    return Character.toUpperCase(result.charAt(0)) + result.substring(1);
}

From source file:com.hybridbpm.core.util.HybridbpmCoreUtil.java

public static String checkClassName(String name) {
    name = name.replaceAll("\\s+", "").replaceAll("\\W", "");
    StringBuilder result = new StringBuilder();
    for (Character c : name.toCharArray()) {
        if (Character.isJavaIdentifierPart(c)) {
            result.append(c);// w  w w . j  av a  2s. co m
        } else {
            result.append("_");
        }
    }
    while (!Character.isJavaIdentifierStart(result.charAt(0))) {
        result.deleteCharAt(0);
    }
    return Character.toUpperCase(result.charAt(0)) + result.substring(1);
}

From source file:com.hybridbpm.core.util.HybridbpmCoreUtil.java

public static String generateFieldName(String name) {
    String nameValue = name;//from www . j  a  v a2 s  .  c o m
    StringBuilder result = new StringBuilder();
    for (Character c : nameValue.toCharArray()) {
        if (Character.isJavaIdentifierPart(c)) {
            result.append(c);
        } else {
            result.append("");
        }
    }
    while (!Character.isJavaIdentifierStart(result.charAt(0))) {
        result.deleteCharAt(0);
    }
    return Character.toLowerCase(result.charAt(0)) + result.substring(1);
}