@Override public int getColumnNumber() { return cu.getColumnNumber(getStartPosition()); } }
@Override public int getColumnNumber() { return cu.getColumnNumber(getStartPosition()); } }
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; } }
public LocationInfo(CompilationUnit cu, String filePath, ASTNode node) { this.filePath = filePath; 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; } }
@Override public int getColumn() { int position = problem.getSourceStart(); if (position >= 0 && parent != null && (parent.getInternal() instanceof CompilationUnit)) { return ((CompilationUnit) parent.getInternal()).getColumnNumber(position); } return -1; }
@Override public String toString() { final CompilationUnit astRoot = sourceCode.astRoot; return "[(" + astRoot.getLineNumber(getStartPosition()) + "," + astRoot.getColumnNumber(getStartPosition()) + ")" + " => (" + astRoot.getLineNumber(getEndPosition()) + "," + astRoot.getColumnNumber(getEndPosition()) + ")]"; } }
final ASTParser p = ASTParser.newParser(AST.JLS3); p.setSource(source); final CompilationUnit root = (CompilationUnit) p.createAST(null); // stuff happens final ASTNode node = //get a node final int line = root.getLineNumber(node.getStartPosition()); final int column = root.getColumnNumber(node.getStartPosition()); System.out.println("Node started at (" + line + ", " + column + ")";
private void processType(Type type, TypeReferenceLocation referenceLocation) { if (type == null) return; String sourceString = type.toString(); sourceString = resolveClassname(sourceString); if (TypeInterestFactory.matchesAny(sourceString, referenceLocation)) { int lineNumber = cu.getLineNumber(type.getStartPosition()); int columnNumber = cu.getColumnNumber(type.getStartPosition()); int length = type.getLength(); JavaTypeReferenceModel typeRef = typeRefService.createTypeReference(fileModel, referenceLocation, lineNumber, columnNumber, length, sourceString); LOG.finer("Prefix: " + referenceLocation); if (type instanceof SimpleType) { SimpleType sType = (SimpleType) type; LOG.finer("The type name is: " + sType.getName().getFullyQualifiedName() + " and " + sourceString); } LOG.finer("Candidate: " + typeRef); } }
@Override public boolean visit(MarkerAnnotation node) { processName(node.getTypeName(), TypeReferenceLocation.ANNOTATION, cu.getLineNumber(node.getStartPosition()), cu.getColumnNumber(cu.getStartPosition()), cu.getLength()); return super.visit(node); }
@Override public boolean visit(SingleMemberAnnotation node) { processName(node.getTypeName(), TypeReferenceLocation.ANNOTATION, cu.getLineNumber(node.getStartPosition()), cu.getColumnNumber(node.getStartPosition()), node.getLength()); return super.visit(node); }
@Override public boolean visit(NormalAnnotation node) { JavaTypeReferenceModel typeRef = processName(node.getTypeName(), TypeReferenceLocation.ANNOTATION, cu.getLineNumber(node.getStartPosition()), cu.getColumnNumber(node.getStartPosition()), node.getLength()); if (typeRef != null) addAnnotationValues((JavaAnnotationTypeReferenceModel) typeRef, node); return super.visit(node); }
/** * Declaration of the variable within a block */ @Override public boolean visit(VariableDeclarationStatement node) { for (int i = 0; i < node.fragments().size(); ++i) { String nodeType = node.getType().toString(); VariableDeclarationFragment frag = (VariableDeclarationFragment) node.fragments().get(i); state.getNames().add(frag.getName().getIdentifier()); state.getNameInstance().put(frag.getName().toString(), nodeType.toString()); } processType(node.getType(), TypeReferenceLocation.VARIABLE_DECLARATION, compilationUnit.getLineNumber(node.getStartPosition()), compilationUnit.getColumnNumber(node.getStartPosition()), node.getLength(), node.toString()); return super.visit(node); }
@Override public boolean visit(InstanceofExpression node) { Type type = node.getRightOperand(); processType(type, TypeReferenceLocation.INSTANCE_OF, compilationUnit.getLineNumber(node.getStartPosition()), compilationUnit.getColumnNumber(type.getStartPosition()), type.getLength(), node.toString()); return super.visit(node); }
/** * Returns a string suitable for identifying a location in the source. * * @param node the node from which to retrieve the source location * @return a string suitable for identifying a location in the source */ public static String getSourceLocation(ASTNode node) { final ASTNode root = node != null ? node.getRoot() : null; if (root instanceof CompilationUnit) { final CompilationUnit cu = (CompilationUnit) root; final int position = node.getStartPosition(); final int line = cu.getLineNumber(position); final int column = cu.getColumnNumber(position) + 1; if (cu.getTypeRoot() != null) { return cu.getTypeRoot().getElementName() + ":" + line + ":" + column; } // it was not created from a file return line + ":" + column; } return ""; }
protected ISourceLocation getSourceLocation(ASTNode node) { try { int nodeLength = compilUnit.getExtendedLength(node); if (nodeLength > 0) { int start = compilUnit.getExtendedStartPosition(node); int end = start + nodeLength -1; if (end < start && ((node.getFlags() & 9) > 0)) { insert(messages, values.constructor(DATATYPE_RASCAL_MESSAGE_ERROR_NODE_TYPE, values.string("Recovered/Malformed node, guessing the length"), values.sourceLocation(loc, 0, 0))); nodeLength = node.toString().length(); end = start + nodeLength - 1; } return values.sourceLocation(loc, start, nodeLength, compilUnit.getLineNumber(start), compilUnit.getLineNumber(end), // TODO: only adding 1 at the end seems to work, need to test. compilUnit.getColumnNumber(start), compilUnit.getColumnNumber(end)+1); } } catch (IllegalArgumentException e) { insert(messages, values.constructor(DATATYPE_RASCAL_MESSAGE_ERROR_NODE_TYPE, values.string("Most probably missing dependency"), values.sourceLocation(loc, 0, 0))); } return values.sourceLocation(loc, 0, 0, 0, 0, 0, 0); }
public boolean visit(org.eclipse.jdt.core.dom.CatchClause node) { Type catchType = node.getException().getType(); processType(catchType, TypeReferenceLocation.CATCH_EXCEPTION_STATEMENT, compilationUnit.getLineNumber(node.getStartPosition()), compilationUnit.getColumnNumber(catchType.getStartPosition()), catchType.getLength(), node.toString()); return super.visit(node); }
@Override public boolean visit(ImportDeclaration node) { String name = node.getName().toString(); if (node.isOnDemand()) { wildcardImports.add(name); Iterable<JavaClassModel> classModels = javaClassService.findByJavaPackage(name); for (JavaClassModel classModel : classModels) { processImport(classModel.getQualifiedName(), cu.getLineNumber(node.getName().getStartPosition()), cu.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength()); } } else { String clzName = StringUtils.substringAfterLast(name, "."); classNameLookedUp.add(clzName); classNameToFQCN.put(clzName, name); processImport(node.getName().toString(), cu.getLineNumber(node.getName().getStartPosition()), cu.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength()); } return super.visit(node); }
/*** * Takes the MethodInvocation, and attempts to resolve the types of objects passed into the method invocation. */ public boolean visit(MethodInvocation node) { if (!StringUtils.contains(node.toString(), ".")) { // it must be a local method. ignore. return true; } String nodeName = StringUtils.removeStart(node.toString(), "this."); List<?> arguments = node.arguments(); List<String> resolvedParams = methodParameterGuesser(arguments); String objRef = StringUtils.substringBefore(nodeName, "." + node.getName().toString()); if (nameInstance.containsKey(objRef)) { objRef = nameInstance.get(objRef); } objRef = resolveClassname(objRef); MethodType methodCall = new MethodType(objRef, node.getName().toString(), resolvedParams); processMethod(methodCall, cu.getLineNumber(node.getName().getStartPosition()), cu.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength()); return super.visit(node); }
public boolean visit(org.eclipse.jdt.core.dom.ThrowStatement node) { if (node.getExpression() instanceof ClassInstanceCreation) { ClassInstanceCreation cic = (ClassInstanceCreation) node.getExpression(); Type type = cic.getType(); processType(type, TypeReferenceLocation.THROW_STATEMENT, compilationUnit.getLineNumber(node.getStartPosition()), compilationUnit.getColumnNumber(cic.getStartPosition()), cic.getLength(), node.toString()); } return super.visit(node); }
@Override public boolean visit(ClassInstanceCreation node) { String nodeType = node.getType().toString(); nodeType = resolveClassname(nodeType); List<String> resolvedParams = this.methodParameterGuesser(node.arguments()); ConstructorType resolvedConstructor = new ConstructorType(nodeType, resolvedParams); processConstructor(resolvedConstructor, cu.getLineNumber(node.getType().getStartPosition()), cu.getColumnNumber(node.getType().getStartPosition()), node.getType().getLength()); return super.visit(node); }