Example usage for org.eclipse.jdt.core JavaConventions validateIdentifier

List of usage examples for org.eclipse.jdt.core JavaConventions validateIdentifier

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaConventions validateIdentifier.

Prototype

public static IStatus validateIdentifier(String id) 

Source Link

Document

Validate the given Java identifier.

Usage

From source file:org.eclipse.jst.ws.internal.common.StringUtils.java

License:Open Source License

/**
 * This returns a valid Java identifier based on the given name. 
 * (follows JAX-RPC 1.0 Public Review Draft 2 recommendations)
 *///from w  w  w.jav a 2 s.  co m
public static String javaIdentifierFor(String localName) {
    StringBuffer result = new StringBuffer();
    boolean skipped = false;
    for (int i = 0, length = localName.length(); i < length; ++i) {
        char character = localName.charAt(i);
        if (Character.isJavaIdentifierPart(character) && !isDelimiter(character)) {
            if (skipped && result.length() != 0) {
                character = Character.toUpperCase(character);
            }
            result.append(character);
            skipped = false;
        } else {
            skipped = true;
        }
    }

    if (!JavaConventions.validateIdentifier(result.toString()).isOK()) {
        result.insert(0, "_");
    }

    return result.toString();
}

From source file:org.eclipse.wb.internal.core.model.util.RenameConvertSupport.java

License:Open Source License

/**
 * Validates that all pending {@link RenameCommand}'s are valid.<br>
 * In particular we verify that variable names are valid and unique.
 * //from w w  w. ja  va 2s. c  o  m
 * @return the error message or <code>null</code>.
 */
private String validateCommands() {
    // check that variable names are valid
    for (RenameCommand command : m_commands.values()) {
        String name = command.m_name;
        if (name != null) {
            IStatus status = JavaConventions.validateIdentifier(name);
            if (status.matches(IStatus.ERROR)) {
                return status.getMessage();
            }
        }
    }
    // check for unique names
    {
        // we intentionally don't check for names uniqueness, because:
        // 1. hard to implement;
        // 2. hard for user, if we force him generate unique names by hands.
    }
    // OK
    return null;
}

From source file:org.eclipse.wb.internal.core.model.variable.AbstractNamedVariableSupport.java

License:Open Source License

/**
 * XXX Kosta.20072024. Note, that this method was implemented (partly, without toLocal/toField),
 * but then I've decided that such strong validation is not a feature, as it will make user
 * operations harder./*from ww w.java2  s  . co m*/
 * 
 * Validates that given combination of variables and their new names is valid.
 * 
 * @param variablesNames
 *          the map: variable -> new name.
 * @param toLocalVariables
 *          the set variables that are currently fields, but will be converted to locals.
 * @param toFieldVariables
 *          the set variables that are currently locales, but will be converted to fields.
 * 
 * @return the message with problem description, or <code>null</code> is variable name is valid.
 */
public static String validateVariables(Map<AbstractNamedVariableSupport, String> variablesNames,
        Set<AbstractNamedVariableSupport> toLocalVariables,
        Set<AbstractNamedVariableSupport> toFieldVariables) {
    // check that identifier are valid
    for (String name : variablesNames.values()) {
        IStatus status = JavaConventions.validateIdentifier(name);
        if (status.matches(IStatus.ERROR)) {
            return status.getMessage();
        }
    }
    // prepare map: variable declaration -> name
    Map<VariableDeclaration, String> declarationsNames = Maps.newHashMap();
    for (Map.Entry<AbstractNamedVariableSupport, String> entry : variablesNames.entrySet()) {
        AbstractNamedVariableSupport variable = entry.getKey();
        String name = entry.getValue();
        declarationsNames.put(variable.m_declaration, name);
    }
    // check each variable
    for (Map.Entry<AbstractNamedVariableSupport, String> entry : variablesNames.entrySet()) {
        AbstractNamedVariableSupport variable = entry.getKey();
        String name = entry.getValue();
        // prepare environment
        CompilationUnit astUnit = variable.m_javaInfo.getEditor().getAstUnit();
        int position = variable.m_variable.getStartPosition();
        // check variables visible at this position
        {
            List<VariableDeclaration> declarations = AstNodeUtils.getVariableDeclarationsVisibleAt(astUnit,
                    position);
            // validate visible declarations
            for (VariableDeclaration declaration : declarations) {
                if (getDeclarationName(declaration, declarationsNames).equals(name)) {
                    return MessageFormat.format("Variable \"{0}\" is already visible at this position.", name);
                }
            }
        }
        // check variables declared after this position
        {
            List<VariableDeclaration> declarations = AstNodeUtils.getVariableDeclarationsAfter(astUnit,
                    position);
            // validate shadowed declarations
            for (VariableDeclaration declaration : declarations) {
                if (getDeclarationName(declaration, declarationsNames).equals(name)) {
                    return MessageFormat
                            .format("Variable \"{0}\" conflicts with other variable declared after it.", name);
                }
            }
        }
    }
    // OK
    return null;
}

From source file:org.eclipse.wb.internal.core.model.variable.AbstractNamedVariableSupport.java

License:Open Source License

/**
 * Validates that given name is valid for this {@link VariableSupport}.
 * /*ww  w.  j  ava 2 s .  co m*/
 * @return the message with problem description, or <code>null</code> is variable name is valid.
 */
public final String validateName(String name) throws Exception {
    // check that identifier is valid
    {
        IStatus status = JavaConventions.validateIdentifier(name);
        if (status.matches(IStatus.ERROR)) {
            return status.getMessage();
        }
    }
    // check that variable is unique
    {
        CompilationUnit astUnit = m_javaInfo.getEditor().getAstUnit();
        int position = m_variable.getStartPosition();
        // check variables visible at this position
        {
            // prepare visible declarations
            List<VariableDeclaration> declarations = AstNodeUtils.getVariableDeclarationsVisibleAt(astUnit,
                    position);
            // check each declaration
            for (VariableDeclaration declaration : declarations) {
                if (declaration.getName().getIdentifier().equals(name)) {
                    return MessageFormat.format("Variable \"{0}\" is already visible at this position.", name);
                }
            }
        }
        // check variables declared after this position
        {
            // prepare shadowing declarations
            List<VariableDeclaration> declarations = AstNodeUtils.getVariableDeclarationsAfter(astUnit,
                    position);
            // check each declaration
            for (VariableDeclaration declaration : declarations) {
                if (declaration.getName().getIdentifier().equals(name)) {
                    return MessageFormat
                            .format("Variable \"{0}\" conflicts with other variable declared after it.", name);
                }
            }
        }
    }
    // OK
    return null;
}