List of usage examples for org.eclipse.jdt.core.util ICodeAttribute getLineNumberAttribute
ILineNumberAttribute getLineNumberAttribute();
From source file:de.bodden.tamiflex.resolution.MethodResolver.java
License:Open Source License
private static void disambiguateMethodByLineNumber(String containerClassName, String methodName, IProject containerProject, int lineNumber) { IJavaProject javaProject = JavaCore.create(containerProject); try {/*from w w w .j a v a 2 s . com*/ IType type = javaProject.findType(containerClassName, (IProgressMonitor) null); ICompilationUnit compilationUnit = type.getCompilationUnit(); if (compilationUnit != null) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(compilationUnit); parser.setResolveBindings(true); CompilationUnit cu = (CompilationUnit) parser.createAST(null); final int linePosition = cu.getPosition(lineNumber, 0); cu.accept(new ASTVisitor() { public boolean visit(MethodDeclaration method) { if (method.getStartPosition() <= linePosition && method.getStartPosition() + method.getLength() >= linePosition) { //method is resolved resolvedMethods.clear(); resolvedMethods.add((IMethod) method.resolveBinding().getJavaElement()); } return false; } }); } else { IClassFile classFile = type.getClassFile(); if (classFile != null) { IClassFileReader reader = ToolFactory.createDefaultClassFileReader(classFile, IClassFileReader.METHOD_INFOS | IClassFileReader.METHOD_BODIES); for (IMethodInfo method : reader.getMethodInfos()) { String currMethodName = new String(method.getName()); if (!currMethodName.equals(methodName)) continue; ICodeAttribute codeAttribute = method.getCodeAttribute(); ILineNumberAttribute lineNumberAttribute = codeAttribute.getLineNumberAttribute(); if (lineNumberAttribute != null) { int[][] lineNumberTable = lineNumberAttribute.getLineNumberTable(); if (lineNumberTable != null && lineNumberTable.length > 0) { int startLine = Integer.MAX_VALUE; int endLine = 0; for (int[] entry : lineNumberTable) { int line = entry[1]; startLine = Math.min(startLine, line); endLine = Math.max(endLine, line); } if (startLine >= lineNumber && endLine <= lineNumber) { char[][] parameterTypes = Signature.getParameterTypes(method.getDescriptor()); String[] parameterTypeNames = new String[parameterTypes.length]; int i = 0; for (char[] cs : parameterTypes) { parameterTypeNames[i] = new String(cs); i++; } IMethod iMethod = type.getMethod(currMethodName, parameterTypeNames); //method resolved resolvedMethods.clear(); resolvedMethods.add(iMethod); } } } } } } } catch (JavaModelException e) { e.printStackTrace(); } }