Java Code Examples for org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber()

The following examples show how to use org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber() . 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: GdtJavaProblem.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
protected GdtJavaProblem(ASTNode node, int offset, int length, T problemType,
    GdtProblemSeverity severity, String[] messageArguments,
    String[] problemArguments) {
  this.id = problemType.getProblemId();

  this.filename = getFileNameFromASTNode(node);
  this.startPosition = offset;
  this.endPosition = offset + length - 1;
  CompilationUnit cu = (CompilationUnit) node.getRoot();
  this.line = cu.getLineNumber(node.getStartPosition());
  this.column = cu.getColumnNumber(node.getStartPosition());

  this.problemType = problemType;
  this.severity = severity;
  this.message = MessageFormat.format(problemType.getMessage(),
      (Object[]) messageArguments);
  this.problemArguments = problemArguments;
}
 
Example 2
Source File: LocationInfo.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public LocationInfo(CompilationUnit cu, String filePath, ASTNode node, CodeElementType codeElementType) {
	this.filePath = filePath;
	this.codeElementType = codeElementType;
	this.startOffset = node.getStartPosition();
	this.length = node.getLength();
	this.endOffset = startOffset + length;
	
	//lines are 1-based
	this.startLine = cu.getLineNumber(startOffset);
	this.endLine = cu.getLineNumber(endOffset);
	//columns are 0-based
	this.startColumn = cu.getColumnNumber(startOffset);
	//convert to 1-based
	if(this.startColumn > 0) {
		this.startColumn += 1;
	}
	this.endColumn = cu.getColumnNumber(endOffset);
	//convert to 1-based
	if(this.endColumn > 0) {
		this.endColumn += 1;
	}
}
 
Example 3
Source File: VariableScope.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public VariableScope(CompilationUnit cu, String filePath, int startOffset, int endOffset) {
	//ASTNode parent = node.getParent();
	this.filePath = filePath;
	this.startOffset = startOffset;
	this.endOffset = endOffset;
	//this.startOffset = node.getStartPosition();
	//this.endOffset = parent.getStartPosition() + parent.getLength();
	
	//lines are 1-based
	this.startLine = cu.getLineNumber(startOffset);
	this.endLine = cu.getLineNumber(endOffset);
	//columns are 0-based
	this.startColumn = cu.getColumnNumber(startOffset);
	//convert to 1-based
	if(this.startColumn > 0) {
		this.startColumn += 1;
	}
	this.endColumn = cu.getColumnNumber(endOffset);
	//convert to 1-based
	if(this.endColumn > 0) {
		this.endColumn += 1;
	}
}
 
Example 4
Source File: OccurrencesSearchQuery.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private JavaElementLine getLineElement(CompilationUnit astRoot, OccurrenceLocation location, HashMap<Integer, JavaElementLine> lineToGroup) {
	int lineNumber= astRoot.getLineNumber(location.getOffset());
	if (lineNumber <= 0) {
		return null;
	}
	JavaElementLine lineElement= null;
	try {
		Integer key= new Integer(lineNumber);
		lineElement= lineToGroup.get(key);
		if (lineElement == null) {
			int lineStartOffset= astRoot.getPosition(lineNumber, 0);
			if (lineStartOffset >= 0) {
				lineElement= new JavaElementLine(astRoot.getTypeRoot(), lineNumber - 1, lineStartOffset);
				lineToGroup.put(key, lineElement);
			}
		}
	} catch (CoreException e) {
		//nothing
	}
	return lineElement;
}
 
Example 5
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IRegion evaluateReplaceRange(CompilationUnit root) {
	List imports= root.imports();
	if (!imports.isEmpty()) {
		ImportDeclaration first= (ImportDeclaration) imports.get(0);
		ImportDeclaration last= (ImportDeclaration) imports.get(imports.size() - 1);

		int startPos= first.getStartPosition(); // no extended range for first: bug 121428
		int endPos= root.getExtendedStartPosition(last) + root.getExtendedLength(last);
		int endLine= root.getLineNumber(endPos);
		if (endLine > 0) {
			int nextLinePos= root.getPosition(endLine + 1, 0);
			if (nextLinePos >= 0) {
				int firstTypePos= getFirstTypeBeginPos(root);
				if (firstTypePos != -1 && firstTypePos < nextLinePos) {
					endPos= firstTypePos;
				} else {
					endPos= nextLinePos;
				}
			}
		}
		return new Region(startPos, endPos - startPos);
	} else {
		int start= getPackageStatementEndPos(root);
		return new Region(start, 0);
	}
}
 
Example 6
Source File: LapseView.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
void processNodeSelection(SimpleName sn, HistoryDefinitionLocation defl, CompilationUnit cu, IResource resource, IProgressMonitor monitor){
	//_cp.addElement("Visited node " + sn.toString());		
	if(name2decl(sn, cu, resource) == null) {
		// only deal with variables
		logError("No definition for " + sn + " is found");
		return;
	}
	
	//Name otherName = decl.getName();
	//String var = Name.getFullyQualifiedName();
	
	//fContentProvider.clearElements();
	// make the root of the slicing tree
	HistoryDefinitionLocation dl = new HistoryDefinitionLocation(
			sn.toString(),
			(IFile)resource, 
			cu.getLineNumber(sn.getStartPosition()), 
			sn, defl, HistoryDefinitionLocation.INITIAL);
	if(defl==null){
	setCurrentInput(dl);
	addHistoryEntry(dl);
	fContentProvider.addElement(dl);
	}
	processDecl(sn, cu, resource, dl, new LinkedList<MethodInvocation>(), monitor);
		
	//showMessage("Got " + _cp.getElementsCount() + " in the list");			
	//fViewer.setSelection(new StructuredSelection(covering));
}
 
Example 7
Source File: ProblemMarkerBuilder.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * createMarkerVisitor Creates the Visitor that goes through the unit and sets the Marker based on a case, which is currently hard coded as cypher.getinstance('AES').
 *
 * @param unit Unit from getUnitForParser
 * @param cu same as unit but different type
 * @return visitor for the unit
 */
private ASTVisitor createMarkerVisitor(final ICompilationUnit unit, final CompilationUnit cu) {
	final ASTVisitor visitor = new ASTVisitor() {

		@Override
		public boolean visit(final MethodInvocation node) {
			final int lineNumber = cu.getLineNumber(node.getStartPosition()) - 1;
			if ("getInstance".equals(node.getName().toString()) && "Cipher".equals(node.getExpression().toString())) {
				final List<Expression> l = node.arguments();
				if (!l.isEmpty()) {
					if ("AES".equals(l.get(0).resolveConstantExpressionValue()) && l.size() == 1) {
						addMarker(unit.getResource(), "Error found", lineNumber, node.getStartPosition(), node.getStartPosition() + node.getLength());
					}
				}
			}
			return true;
		}
	};
	return visitor;
}
 
Example 8
Source File: ASTUtil.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@CheckForNull
protected static Statement searchStatement(CompilationUnit compilationUnit, List<?> statements, int startLine, int endLine) {
    Assert.isNotNull(compilationUnit);
    Assert.isNotNull(statements);

    for (Object statementObj : statements) {
        Statement statement = (Statement) statementObj;
        int lineNumber = compilationUnit.getLineNumber(statement.getStartPosition());
        if (startLine <= lineNumber && lineNumber <= endLine) {
            return statement;
        }
    }
    return null;
}
 
Example 9
Source File: LineInformation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static LineInformation create(final CompilationUnit astRoot) {
	return new LineInformation() {
		public int getLineOfOffset(int offset) {
			return astRoot.getLineNumber(offset) - 1;
		}
		public int getLineOffset(int line) {
			return astRoot.getPosition(line + 1, 0);
		}
	};
}
 
Example 10
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getPackageStatementEndPos(CompilationUnit root) {
	PackageDeclaration packDecl= root.getPackage();
	if (packDecl != null) {
		int afterPackageStatementPos= -1;
		int lineNumber= root.getLineNumber(packDecl.getStartPosition() + packDecl.getLength());
		if (lineNumber >= 0) {
			int lineAfterPackage= lineNumber + 1;
			afterPackageStatementPos= root.getPosition(lineAfterPackage, 0);
		}
		if (afterPackageStatementPos < 0) {
			this.flags|= F_NEEDS_LEADING_DELIM;
			return packDecl.getStartPosition() + packDecl.getLength();
		}
		int firstTypePos= getFirstTypeBeginPos(root);
		if (firstTypePos != -1 && firstTypePos <= afterPackageStatementPos) {
			this.flags|= F_NEEDS_TRAILING_DELIM;
			if (firstTypePos == afterPackageStatementPos) {
				this.flags|= F_NEEDS_LEADING_DELIM;
			}
			return firstTypePos;
		}
		this.flags|= F_NEEDS_LEADING_DELIM;
		return afterPackageStatementPos; // insert a line after after package statement
	}
	this.flags |= F_NEEDS_TRAILING_DELIM;
	return 0;
}