Java Code Examples for com.puppycrawl.tools.checkstyle.api.TokenTypes#IDENT

The following examples show how to use com.puppycrawl.tools.checkstyle.api.TokenTypes#IDENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReferenceCounter.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void handleNode( SymTabAST node ) {
  if (node.getType() == TokenTypes.IDENT && node.isMeaningful()) {
    _references.add( node );
    if (node.getDefinition() != null && !(node.getDefinition() instanceof UnknownClass)) {
          _resolvedReferences.add( node );
    }
    else {
          _unresolvedReferences.add( node );
    }
  }
  walkChildren( node );
}
 
Example 2
Source File: MonitoringRecordFactoryConventionCheck.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * This method checks whether the given trees are more or less equal. This is
 * necessary as the available methods do not work as expected for this special
 * case.
 *
 * @param actual
 *            The actual tree.
 * @param expected
 *            The expected tree.
 *
 * @return true if and only if the trees are (more or less) equal.
 */
private static boolean treeCompare(final DetailAST actual, final DetailAST expected) {
	if ((actual != null) && (expected != null) && (actual.getType() == expected.getType())) {
		// If they are identifiers, we check the texts
		if ((actual.getType() == TokenTypes.IDENT) && (!actual.getText().equals(expected.getText()))) {
			return false;
		}

		// Everything fine so far. Check the equality of the children
		DetailAST actChild = actual.getFirstChild();
		DetailAST expChild = expected.getFirstChild();

		while (actChild != null) {
			// The treeCompare method checks whether one of the children is null (we
			// recognize therefore if the parents have different number of children).
			if (!MonitoringRecordFactoryConventionCheck.treeCompare(actChild, expChild)) {
				return false;
			}

			actChild = actChild.getNextSibling();
			expChild = expChild.getNextSibling();
		}

		// Everything seems to be fine - just make sure that expChild is null as well or
		// otherwise the parents had a different number of children
		return expChild == null;
	}

	return false;
}
 
Example 3
Source File: ReferenceCounter.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void handleNode( SymTabAST node ) {
  if (node.getType() == TokenTypes.IDENT && node.isMeaningful()) {
    _references.add( node );
    if (node.getDefinition() != null && !(node.getDefinition() instanceof UnknownClass)) {
          _resolvedReferences.add( node );
    }
    else {
          _unresolvedReferences.add( node );
    }
  }
  walkChildren( node );
}
 
Example 4
Source File: SymTabAST.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * sets meaningfulness for this node and its children
 * @return <code>void</code>
 * @see #setMeaningfulness(boolean)
 */
public void ignoreChildren() {
    if (getType() == TokenTypes.IDENT) {
        setMeaningfulness(false);
    }
    SymTabAST child = (SymTabAST) getFirstChild();
    while (child != null) {
        child.ignoreChildren();
        child = (SymTabAST) child.getNextSibling();
    }
}
 
Example 5
Source File: QueryEngine.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
private SymTabAST getWordNodeAtOccurrence(Occurrence location) {
  SymTabAST result = null;

  SymTabAST fileNode = getFileNode(location.getFile());
  if ( fileNode != null ) {
    SymTabAST node = fileNode.getEnclosingNode(location.getLine(),
                                               location.getColumn());

    if ( (node != null) && (node.getType() == TokenTypes.IDENT) ) {
      result = node;
    }
  }

  return result;
}
 
Example 6
Source File: SymTabAST.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * sets meaningfulness for this node and its children
 * @return <code>void</code>
 * @see #setMeaningfulness(boolean)
 */
public void ignoreChildren() {
    if (getType() == TokenTypes.IDENT) {
        setMeaningfulness(false);
    }
    SymTabAST child = (SymTabAST) getFirstChild();
    while (child != null) {
        child.ignoreChildren();
        child = (SymTabAST) child.getNextSibling();
    }
}
 
Example 7
Source File: QueryEngine.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private SymTabAST getWordNodeAtOccurrence(Occurrence location) {
  SymTabAST result = null;

  SymTabAST fileNode = getFileNode(location.getFile());
  if ( fileNode != null ) {
    SymTabAST node = fileNode.getEnclosingNode(location.getLine(),
                                               location.getColumn());

    if ( (node != null) && (node.getType() == TokenTypes.IDENT) ) {
      result = node;
    }
  }

  return result;
}
 
Example 8
Source File: IdentCheckFilter.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public void visitToken(DetailAST ast) {
	if (ast.getType() == TokenTypes.IDENT && isFiltered(ast)) {
		return;
	}
	super.visitToken(ast);
}
 
Example 9
Source File: Resolver.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolves a constructor call.
 *
 * @param tree the root node of the constructor call
 * @return the <code>ClassDef</code> for the class instantiated by the
 *         constructor
 */
private IClass resolveConstructor(
    SymTabAST constructor,
    Scope location,
    IClass context,
    boolean referencePhase) {

    IClass classConstructed = null;

    SymTabAST nameNode = (SymTabAST) (constructor.getFirstChild());
    //SymTabAST parametersNode = (SymTabAST) (nameNode.getNextSibling());
    SymTabAST parametersNode =
        constructor.findFirstToken(TokenTypes.ELIST);
    SymTabAST nameIdent = null;
    if (nameNode.getType() == TokenTypes.IDENT) {
        nameIdent = nameNode;
    }
    else {
        nameIdent = (SymTabAST) nameNode.getFirstChild().getNextSibling();
    }

    classConstructed = resolveClass(nameNode, location, context, false);
    if (classConstructed != null) {
        MethodSignature signature =
            resolveParameters(
                parametersNode,
                location,
                context,
                referencePhase);

        IMethod constructorDef =
            classConstructed.getMethodDefinition(
                nameIdent.getText(),
                signature);

        if (constructorDef != null && referencePhase) {
            nameIdent.setDefinition(
                constructorDef,
                location,
                referencePhase);
        }
    }

    return classConstructed;
}
 
Example 10
Source File: Resolver.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolves a method call.
 *
 * @param methodNode the <code>SymTabAST</code> for the METHOD_CALL node
 * @param location the <code>Scope</code> where the expression occurs
 * @param context the <code>Scope</code> in which the expression occurs
 *                (where the search for a defintion begins)
 * @param referencePhase whether or not this is the reference phase of
 *                       table construction
 *
 * @return the <code>ClassDef</code> for the type returned by the method
 */
private IClass resolveMethod(
    SymTabAST methodNode,
    Scope location,
    IClass context,
    boolean referencePhase) {
    IClass result = new UnknownClass(methodNode.getText(), methodNode);
    IClass newContext = null;

    if (context == null) {
        newContext = location.getEnclosingClass();
    }
    else {
        newContext = context;
    }

    String name = null;
    boolean createReference = true;

    SymTabAST nameNode = (SymTabAST) (methodNode.getFirstChild());
    SymTabAST parametersNode = (SymTabAST) (nameNode.getNextSibling());

    ISignature signature =
        resolveParameters(
            parametersNode,
            location,
            context,
            referencePhase);

    if (nameNode.getType() == TokenTypes.IDENT) {
        name = nameNode.getText();
    }
    else if (
        nameNode.getType() == TokenTypes.LITERAL_SUPER
            || (nameNode.getType() == TokenTypes.SUPER_CTOR_CALL)) {
        IClass superclass = location.getEnclosingClass().getSuperclass();
        newContext = superclass;
        name = superclass.getName();
        createReference = false;
    }
    else if (nameNode.getType() == TokenTypes.LITERAL_THIS) {
        newContext = location.getEnclosingClass();
        name = newContext.getName();
        createReference = false;
    }
    else {
        // REDTAG -- doing dotted name resolution on its own
        SymTabAST contextNode = (SymTabAST) (nameNode.getFirstChild());
        //TODO: handle Checkstyle grammar
        nameNode = (SymTabAST) contextNode.getNextSibling();
        //skip to IDENT
        while (nameNode.getType() != TokenTypes.IDENT) {
            nameNode = (SymTabAST) nameNode.getNextSibling();
        }
        
        name = nameNode.getText();
        newContext =
            resolveExpression(
                contextNode,
                location,
                context,
                referencePhase);
    }

    if (newContext != null) {
        IMethod method = newContext.getMethodDefinition(name, signature);

        if (method != null) {
            if (createReference && referencePhase) {
                nameNode.setDefinition(method, location, referencePhase);
            }
            result = method.getType();
        }
    }

    if (result == null) {
        result = new UnknownClass(methodNode.getText(), methodNode);
    }

    return result;
}
 
Example 11
Source File: SpringNoThisCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public int[] getAcceptableTokens() {
	return new int[] { TokenTypes.IDENT };
}
 
Example 12
Source File: UnusedPrivateMethodCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Checks if a given method is writeReplace() or readResolve().
 * @param aAST method def to check
 * @return true if this is a writeReplace() definition
 */
private boolean isWriteReplaceOrReadResolve(DetailAST aAST)
{
    // name is writeReplace or readResolve...
    final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
    if (!"writeReplace".equals(ident.getText())
        && !"readResolve".equals(ident.getText()))
    {
        return false;
    }

    // returns Object...
    final DetailAST typeAST =
        (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
    if (typeAST.getType() != TokenTypes.DOT
        && typeAST.getType() != TokenTypes.IDENT)
    {
        return false;
    }

    // should have no parameters...
    final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
    if (params != null && params.getChildCount() != 0) {
        return false;
    }

    // and, finally, it should throws java.io.ObjectStreamException
    final DetailAST throwsAST =
        aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
    if (throwsAST == null || throwsAST.getChildCount() != 1) {
        return false;
    }
    final DetailAST excpt = (DetailAST) throwsAST.getFirstChild();
    final String exception = FullIdent.createFullIdent(excpt).getText();
    if (!"java.io.ObjectStreamException".equals(exception)
        && !"ObjectStreamException".equals(exception))
    {
        return false;
    }

    return true;
}
 
Example 13
Source File: Resolver.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolves a method call.
 *
 * @param methodNode the <code>SymTabAST</code> for the METHOD_CALL node
 * @param location the <code>Scope</code> where the expression occurs
 * @param context the <code>Scope</code> in which the expression occurs
 *                (where the search for a defintion begins)
 * @param referencePhase whether or not this is the reference phase of
 *                       table construction
 *
 * @return the <code>ClassDef</code> for the type returned by the method
 */
private IClass resolveMethod(
    SymTabAST methodNode,
    Scope location,
    IClass context,
    boolean referencePhase) {
    IClass result = new UnknownClass(methodNode.getText(), methodNode);
    IClass newContext = null;

    if (context == null) {
        newContext = location.getEnclosingClass();
    }
    else {
        newContext = context;
    }

    String name = null;
    boolean createReference = true;

    SymTabAST nameNode = (SymTabAST) (methodNode.getFirstChild());
    SymTabAST parametersNode = (SymTabAST) (nameNode.getNextSibling());

    ISignature signature =
        resolveParameters(
            parametersNode,
            location,
            context,
            referencePhase);

    if (nameNode.getType() == TokenTypes.IDENT) {
        name = nameNode.getText();
    }
    else if (
        nameNode.getType() == TokenTypes.LITERAL_SUPER
            || (nameNode.getType() == TokenTypes.SUPER_CTOR_CALL)) {
        IClass superclass = location.getEnclosingClass().getSuperclass();
        newContext = superclass;
        name = superclass.getName();
        createReference = false;
    }
    else if (nameNode.getType() == TokenTypes.LITERAL_THIS) {
        newContext = location.getEnclosingClass();
        name = newContext.getName();
        createReference = false;
    }
    else {
        // REDTAG -- doing dotted name resolution on its own
        SymTabAST contextNode = (SymTabAST) (nameNode.getFirstChild());
        //TODO: handle Checkstyle grammar
        nameNode = (SymTabAST) contextNode.getNextSibling();
        //skip to IDENT
        while (nameNode.getType() != TokenTypes.IDENT) {
            nameNode = (SymTabAST) nameNode.getNextSibling();
        }
        
        name = nameNode.getText();
        newContext =
            resolveExpression(
                contextNode,
                location,
                context,
                referencePhase);
    }

    if (newContext != null) {
        IMethod method = newContext.getMethodDefinition(name, signature);

        if (method != null) {
            if (createReference && referencePhase) {
                nameNode.setDefinition(method, location, referencePhase);
            }
            result = method.getType();
        }
    }

    if (result == null) {
        result = new UnknownClass(methodNode.getText(), methodNode);
    }

    return result;
}
 
Example 14
Source File: Resolver.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolves a constructor call.
 *
 * @param tree the root node of the constructor call
 * @return the <code>ClassDef</code> for the class instantiated by the
 *         constructor
 */
private IClass resolveConstructor(
    SymTabAST constructor,
    Scope location,
    IClass context,
    boolean referencePhase) {

    IClass classConstructed = null;

    SymTabAST nameNode = (SymTabAST) (constructor.getFirstChild());
    //SymTabAST parametersNode = (SymTabAST) (nameNode.getNextSibling());
    SymTabAST parametersNode =
        constructor.findFirstToken(TokenTypes.ELIST);
    SymTabAST nameIdent = null;
    if (nameNode.getType() == TokenTypes.IDENT) {
        nameIdent = nameNode;
    }
    else {
        nameIdent = (SymTabAST) nameNode.getFirstChild().getNextSibling();
    }

    classConstructed = resolveClass(nameNode, location, context, false);
    if (classConstructed != null) {
        MethodSignature signature =
            resolveParameters(
                parametersNode,
                location,
                context,
                referencePhase);

        IMethod constructorDef =
            classConstructed.getMethodDefinition(
                nameIdent.getText(),
                signature);

        if (constructorDef != null && referencePhase) {
            nameIdent.setDefinition(
                constructorDef,
                location,
                referencePhase);
        }
    }

    return classConstructed;
}
 
Example 15
Source File: SpringCatchCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
private void visitParameterDef(DetailAST ast) {
	DetailAST lastChild = ast.getLastChild();
	if (lastChild != null && lastChild.getType() == TokenTypes.IDENT) {
		checkIdent(lastChild);
	}
}
 
Example 16
Source File: UnusedPrivateMethodCheck.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Checks if a given method is writeReplace() or readResolve().
 * @param aAST method def to check
 * @return true if this is a writeReplace() definition
 */
private boolean isWriteReplaceOrReadResolve(DetailAST aAST)
{
    // name is writeReplace or readResolve...
    final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
    if (!"writeReplace".equals(ident.getText())
        && !"readResolve".equals(ident.getText()))
    {
        return false;
    }

    // returns Object...
    final DetailAST typeAST =
        (DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
    if (typeAST.getType() != TokenTypes.DOT
        && typeAST.getType() != TokenTypes.IDENT)
    {
        return false;
    }

    // should have no parameters...
    final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
    if (params != null && params.getChildCount() != 0) {
        return false;
    }

    // and, finally, it should throws java.io.ObjectStreamException
    final DetailAST throwsAST =
        aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
    if (throwsAST == null || throwsAST.getChildCount() != 1) {
        return false;
    }
    final DetailAST excpt = (DetailAST) throwsAST.getFirstChild();
    final String exception = FullIdent.createFullIdent(excpt).getText();
    if (!"java.io.ObjectStreamException".equals(exception)
        && !"ObjectStreamException".equals(exception))
    {
        return false;
    }

    return true;
}
 
Example 17
Source File: SpringNoThisCheck.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
@Override
public void visitToken(DetailAST ast) {
	if (ast.getType() == TokenTypes.IDENT) {
		visitIdent(ast);
	}
}