Example usage for org.eclipse.jdt.core.dom MethodDeclaration getLength

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration getLength

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodDeclaration getLength.

Prototype

public final int getLength() 

Source Link

Document

Returns the length in characters of the original source file indicating where the source fragment corresponding to this node ends.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(MethodDeclaration node) {

    int beginLine = beginLine(node);
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = beginColunm(node);
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Block body = node.getBody();//from ww  w.  j av a  2 s. c  om

    if (body != null) {

        int beginLineBody = cu.getLineNumber(body.getStartPosition());
        int endLineBody = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int beginColumnBody = cu.getColumnNumber(body.getStartPosition());
        int endColumnBody = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine,
                beginColumn, endColumn, beginLineBody, endLineBody, beginColumnBody, endColumnBody, null));
    } else {
        languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName() + INTERFACE, beginLine,
                endLine, beginColumn, endColumn));
    }

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(MethodDeclaration node) {

    Location location;/*  w ww  .  j  a  v  a  2  s  .c om*/

    int elementLineBegin = cu.getLineNumber(node.getStartPosition());
    int elementLineEnd = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int elementColumnBegin = cu.getColumnNumber(node.getStartPosition());
    int elementColumnEnd = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Block body = node.getBody();
    if (body == null) {
        location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);
    } else {
        int bodyLineBegin = cu.getLineNumber(body.getStartPosition());
        int bodyLineEnd = cu.getLineNumber(body.getStartPosition() + body.getLength());
        int bodyColumnBegin = cu.getColumnNumber(body.getStartPosition());
        int bodyColumnEnd = cu.getColumnNumber(body.getStartPosition() + body.getLength());

        location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd,
                bodyLineBegin, bodyLineEnd, bodyColumnBegin, bodyColumnEnd);

    }

    MyMethodDeclaration myMethodDeclaration = new MyMethodDeclaration(node, location);

    if (!classLanguageConstructsList.isEmpty()) {
        classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getMethodDeclarations()
                .add(myMethodDeclaration);
    }

    return true;
}

From source file:com.architexa.diagrams.relo.jdt.ParseUtilities.java

License:Open Source License

/**
 * @param method//w  w  w  .  ja  v a2 s  .c o  m
 * @return SourceRange
 * @throws JavaModelException
 */
public static ISourceRange getMethodRange(IMethod method) throws JavaModelException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(method.getCompilationUnit());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    TypeDeclaration td = findTypeDeclaration(cu, method);
    final MethodDeclaration md = findMethodDeclaration(td, method);
    return new ISourceRange() {
        public int getLength() {
            return md.getLength();
        }

        public int getOffset() {
            return md.getStartPosition();
        }
    };
}

From source file:com.ashigeru.eclipse.util.jdt.internal.ui.handlers.InsertAssertionHandler.java

License:Apache License

private MethodDeclaration findSelectedExecutable(CompilationUnit ast, TextSelection selection) {
    assert ast != null;
    assert selection != null;
    final MethodDeclaration[] results = new MethodDeclaration[1];
    final int carret = selection.getOffset();
    ast.accept(new ASTVisitor(false) {
        @Override//from  w w  w . ja  v  a2 s .  c o  m
        public boolean visit(MethodDeclaration node) {
            if (node.getBody() == null) {
                return false;
            }
            int start = node.getStartPosition();
            if (start < 0) {
                return true;
            }
            int end = start + node.getLength();
            if (start <= carret && carret <= end) {
                results[0] = node;
            }
            return true;
        }
    });
    return results[0];
}

From source file:com.chookapp.org.bracketeer.jdt.ClosingBracketHintVisitor.java

License:Open Source License

@Override
public boolean visit(MethodDeclaration node) {
    // starting a function empties the stack... (which should already be empty on good flow)
    _scopeStack.clear();/*w ww  .ja  va 2s . co m*/

    StringBuffer hint = new StringBuffer();
    hint.append(node.getName().getIdentifier());
    /* TODO: specific params: exclude function parameters (show only the name) */
    hint.append("( "); //$NON-NLS-1$
    for (@SuppressWarnings("rawtypes")
    Iterator iterator = node.parameters().iterator(); iterator.hasNext();) {
        SingleVariableDeclaration param = (SingleVariableDeclaration) iterator.next();
        hint.append(param.getName());
        if (iterator.hasNext())
            hint.append(", "); //$NON-NLS-1$
    }
    hint.append(" )"); //$NON-NLS-1$
    int startLoc = node.getName().getStartPosition();
    int endLoc = node.getStartPosition() + node.getLength() - 1;
    try {
        _container.add(new Hint("function", startLoc, endLoc, hint.toString())); //$NON-NLS-1$
    } catch (BadLocationException e) {
        _cancelProcessing.set(true);
    }
    return shouldContinue();
}

From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java

License:Open Source License

private String extractNativeMethodBody(MethodDeclaration m) {
    assert (m.getModifiers() & Modifier.NATIVE) > 0;
    String nativeCode = extractNativeCode(m.getStartPosition(), m.getLength());
    if (nativeCode == null) {
        J2ObjC.error(m, "no native code found");
        return "ERROR";
    }/*from  w  w w .  j a va 2  s  .co m*/
    indent();
    String code = reindent('{' + nativeCode + '}');
    unindent();
    return code;
}

From source file:com.google.devtools.j2objc.gen.SourceFileGenerator.java

License:Open Source License

/**
 * Returns true if a native method has an OCNI block, warning if JSNI
 * delimiters are found instead./*w  w w  .j  a  v a2 s  .  c  o  m*/
 */
protected boolean hasNativeCode(MethodDeclaration m, boolean reportJsniWarnings) {
    assert (m.getModifiers() & Modifier.NATIVE) > 0;
    String nativeCode = extractNativeCode(m.getStartPosition(), m.getLength(), reportJsniWarnings);
    return nativeCode != null;
}

From source file:com.halware.nakedide.eclipse.core.templates.propl.GenericTemplCompltnProcessor.java

License:Open Source License

private boolean regionOverlaps(MethodDeclaration md, IRegion region) {
    return RegionUtil.overlaps(new Region(md.getStartPosition(), md.getLength()), region);
}

From source file:com.microsoft.javapkgsrv.JavaParser.java

License:MIT License

public List<Protocol.Response.OutlineResultResponse.Outline> ProcessOutlineRequest(Integer fileId) {
    final List<OutlineResultResponse.Outline> ret = new ArrayList<OutlineResultResponse.Outline>();
    if (ActiveUnits.containsKey(fileId)) {
        CompilationUnit cu = ActiveUnits.get(fileId);
        cu.accept(new ASTVisitor() {
            @Override//w w  w . ja  v  a  2 s  .co m
            public boolean visit(TypeDeclaration type) {
                ret.add(OutlineResultResponse.Outline.newBuilder().setStartPosition(type.getStartPosition())
                        .setLength(type.getLength()).setHoverText(type.toString())
                        .setSummaryText(type.getName().toString()).build());
                return true;
            }

            @Override
            public boolean visit(MethodDeclaration method) {
                ret.add(OutlineResultResponse.Outline.newBuilder().setStartPosition(method.getStartPosition())
                        .setLength(method.getLength()).setHoverText(method.toString())
                        .setSummaryText(method.getName().toString()).build());
                return true;
            }
        });
    }
    return ret;
}

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  ww  .  j  a  va  2 s  .c  om
        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();
    }
}