org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: CogniCrypt   文件: ProblemMarkerBuilder.java
/**
 * 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;
}
 
源代码2 项目: gwt-eclipse-plugin   文件: GdtJavaProblem.java
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;
}
 
源代码3 项目: RefactoringMiner   文件: LocationInfo.java
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;
	}
}
 
源代码4 项目: RefactoringMiner   文件: VariableScope.java
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;
	}
}
 
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;
}
 
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);
	}
}
 
源代码7 项目: lapse-plus   文件: LapseView.java
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));
}
 
源代码8 项目: spotbugs   文件: ASTUtil.java
@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;
}
 
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);
		}
	};
}
 
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;
}