Java Code Examples for org.eclipse.jdt.core.dom.SimpleName#getLength()

The following examples show how to use org.eclipse.jdt.core.dom.SimpleName#getLength() . 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
@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
	if (!(getEditor() instanceof JavaEditor))
		return null;

	ITypeRoot je= getEditorInputJavaElement();
	if (je == null)
		return null;

	// Never wait for an AST in UI thread.
	CompilationUnit ast= SharedASTProvider.getAST(je, SharedASTProvider.WAIT_NO, null);
	if (ast == null)
		return null;

	ASTNode node= NodeFinder.perform(ast, offset, 1);
	if (node instanceof StringLiteral) {
		StringLiteral stringLiteral= (StringLiteral)node;
		return new Region(stringLiteral.getStartPosition(), stringLiteral.getLength());
	} else if (node instanceof SimpleName) {
		SimpleName simpleName= (SimpleName)node;
		return new Region(simpleName.getStartPosition(), simpleName.getLength());
	}

	return null;
}
 
Example 2
@SuppressWarnings("rawtypes")
@Override
public boolean visit(MethodInvocation node) {
    SimpleName methodName = node.getName();

    List args = node.arguments();
    Expression expression = node.getExpression();
    Map<String, Integer> scopeBindings = getNodeScopes().get(node);
    String target = getTarget(expression);

    String targetType = translateTargetToType(target, scopeBindings);
    // Add only if you could guess the type of target, else ignore.
    // TODO: In case of a method in super type, this will still infer it as in "this".
    if (!targetType.isEmpty()) {
        List<String> argTypes = translateArgsToTypes(args, scopeBindings);
        if (!methodStack.empty()) {
            MethodDeclaration currentMethod = methodStack.peek();
            MethodDecl methodDecl = getMethodDecl(currentMethod);
            List<MethodInvokRef> invoks = methodInvoks.get(methodDecl);
            if (invoks == null) {
                invoks = new ArrayList<>();
                methodInvoks.put(methodDecl, invoks);
            }
            MethodInvokRef methodInvokRef = new MethodInvokRef(methodName.toString(), targetType, target, args
                    .size(), node.getName().getStartPosition(), argTypes, methodName.getLength(), false,
                    getReturnType(node));
            invoks.add(methodInvokRef);
        }
    }
    return true;
}
 
Example 3
private boolean isInRemoved(SimpleName ref, List<int[]> removedStartsEnds) {
	int start= ref.getStartPosition();
	int end= start + ref.getLength();
	for (int[] removedStartsEnd : removedStartsEnds) {
		if (start >= removedStartsEnd[0] && end <= removedStartsEnd[1]) {
			return true;
		}
	}
	return false;
}
 
Example 4
private static int getNameNodeProblemKind(IProblem[] problems, SimpleName nameNode) {
	int nameOffset= nameNode.getStartPosition();
	int nameInclEnd= nameOffset + nameNode.getLength() - 1;

	for (int i= 0; i < problems.length; i++) {
		IProblem curr= problems[i];
		if (curr.getSourceStart() == nameOffset && curr.getSourceEnd() == nameInclEnd) {
			int kind= getProblemKind(curr);
			if (kind != 0) {
				return kind;
			}
		}
	}
	return 0;
}
 
Example 5
Source Project: JDeodorant   File: PreconditionExaminer.java    License: MIT License 5 votes vote down vote up
private boolean isInsideDifference(SimpleName simpleName, Expression difference) {
	int startOffset = simpleName.getStartPosition();
	int endOffset = simpleName.getStartPosition() + simpleName.getLength();
	int differenceStartOffset = difference.getStartPosition();
	int differenceEndOffset = difference.getStartPosition() + difference.getLength();
	if(startOffset >= differenceStartOffset && endOffset <= differenceEndOffset)
		return true;
	return false;
}
 
Example 6
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
	if (this.createdNode == null) {
		this.source = removeIndentAndNewLines(this.source, cu);
		ASTParser parser = ASTParser.newParser(AST.JLS8);
		parser.setSource(this.source.toCharArray());
		parser.setProject(getCompilationUnit().getJavaProject());
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
		ASTNode node = parser.createAST(this.progressMonitor);
		String createdNodeSource;
		if (node.getNodeType() != ASTNode.TYPE_DECLARATION) {
			createdNodeSource = generateSyntaxIncorrectAST();
			if (this.createdNode == null)
				throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
		} else {
			TypeDeclaration typeDeclaration = (TypeDeclaration) node;
			if ((typeDeclaration.getFlags() & ASTNode.MALFORMED) != 0) {
				createdNodeSource = generateSyntaxIncorrectAST();
				if (this.createdNode == null)
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
			} else {
				List bodyDeclarations = typeDeclaration.bodyDeclarations();
				if (bodyDeclarations.size() == 0) {
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
				}
				this.createdNode = (ASTNode) bodyDeclarations.iterator().next();
				createdNodeSource = this.source;
			}
		}
		if (this.alteredName != null) {
			SimpleName newName = this.createdNode.getAST().newSimpleName(this.alteredName);
			SimpleName oldName = rename(this.createdNode, newName);
			int nameStart = oldName.getStartPosition();
			int nameEnd = nameStart + oldName.getLength();
			StringBuffer newSource = new StringBuffer();
			if (this.source.equals(createdNodeSource)) {
				newSource.append(createdNodeSource.substring(0, nameStart));
				newSource.append(this.alteredName);
				newSource.append(createdNodeSource.substring(nameEnd));
			} else {
				// syntactically incorrect source
				int createdNodeStart = this.createdNode.getStartPosition();
				int createdNodeEnd = createdNodeStart + this.createdNode.getLength();
				newSource.append(createdNodeSource.substring(createdNodeStart, nameStart));
				newSource.append(this.alteredName);
				newSource.append(createdNodeSource.substring(nameEnd, createdNodeEnd));

			}
			this.source = newSource.toString();
		}
	}
	if (rewriter == null) return this.createdNode;
	// return a string place holder (instead of the created node) so has to not lose comments and formatting
	return rewriter.createStringPlaceholder(this.source, this.createdNode.getNodeType());
}