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

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

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

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();// w  w  w .  j  a va  2 s. c o m

    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;/*from www .  ja va 2s .  c o m*/

    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.j a  v a 2  s.c  om*/
 * @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  .  j  a  va 2 s.c om*/
        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();//from  w  ww . j  av a  2  s .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 ww.j  a  va2  s . c o  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.// ww  w .j a  v a  2  s.  co 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.google.gdt.eclipse.designer.uibinder.model.util.EventHandlerProperty.java

License:Open Source License

/**
 * Opens source of given Java {@link IFile} at position that corresponds {@link MethodDeclaration}
 * .//  ww  w  . ja  v a2  s  . c om
 */
private void openMethod_inEditor(MethodDeclaration method) throws Exception {
    IEditorPart javaEditor = IDE.openEditor(DesignerPlugin.getActivePage(), m_javaFile);
    if (javaEditor instanceof ITextEditor) {
        ((ITextEditor) javaEditor).selectAndReveal(method.getStartPosition(), 0);
    }
}

From source file:com.google.gwt.eclipse.core.validators.java.JsniParser.java

License:Open Source License

public static JavaValidationResult parse(MethodDeclaration method) {
    final JavaValidationResult result = new JavaValidationResult();

    try {//from   w w  w  .j  a  v a2 s .co  m
        try {
            // Find all Java references
            result.addAllJavaRefs(findJavaRefs(method));

            // Validate the Java references
            for (JsniJavaRef ref : result.getJavaRefs()) {
                GWTJavaProblem problem = validateJavaRef(method, ref);
                if (problem != null) {
                    result.addProblem(problem);
                }
            }
        } catch (JavaScriptParseException e) {
            // Add the offset of the method declaration to get a document offset
            int offset = e.getOffset() + method.getStartPosition();

            // Add the problem as a 1 character wide error, since we don't get the
            // length of the error "region" from JsParser
            result.addProblem(
                    GWTJavaProblem.create(method, offset, 1, GWTProblemType.JSNI_PARSE_ERROR, e.getMessage()));
        } catch (IOException e) {
            GWTPluginLog.logError(e, "IO error while parsing JSNI method " + method.getName());
        } catch (InternalCompilerException e) {
            String errorMsg = "Unexpected error parsing JSNI method " + method.getName() + ": "
                    + e.getMessage();
            Throwable cause = (e.getCause() != null ? e.getCause() : e);
            GWTPluginLog.logError(cause, errorMsg);
        }
    } catch (BadLocationException e) {
        GWTPluginLog.logError(e, "Error translating JS parse error location in " + method.getName());
    }

    return result;
}

From source file:com.google.gwt.eclipse.core.validators.java.JsniParser.java

License:Open Source License

private static List<JsniJavaRef> findJavaRefs(final MethodDeclaration jsniMethod)
        throws IOException, JavaScriptParseException, BadLocationException {
    final String jsniSource = JavaASTUtils.getSource(jsniMethod);
    ICompilationUnit cu = JavaASTUtils.getCompilationUnit(jsniMethod);
    final IPath cuPath = cu.getResource().getFullPath();
    final List<JsniJavaRef> javaRefs = new ArrayList<JsniJavaRef>();

    JsBlock js = JsniParser.parse(jsniSource);
    if (js != null) {
        // Visit the JavaScript AST to find all Java references
        new JsVisitor() {
            @Override/*  w ww . j  a  v  a2  s .c  om*/
            public void endVisit(JsNameRef x, @SuppressWarnings("rawtypes") JsContext ctx) {
                String ident = x.getIdent();
                if (ident.indexOf("@") != -1) {
                    JsniJavaRef javaRef = JsniJavaRef.parse(ident);
                    if (javaRef != null) {
                        // Set the reference's Java source file
                        javaRef.setSource(cuPath);

                        // To get the Java reference offset, we have to do an indexOf on
                        // its identifier. To make sure we catch multiple references to
                        // the same Java element, we need to start at the index one past
                        // the start of the last Java reference we found (if any)
                        int fromIndex = 0;
                        if (javaRefs.size() > 0) {
                            fromIndex = javaRefs.get(javaRefs.size() - 1).getOffset()
                                    - jsniMethod.getStartPosition() + 1;
                        }
                        int offset = jsniSource.indexOf(ident, fromIndex) + jsniMethod.getStartPosition();

                        // Set the reference's offset within the Java source file
                        javaRef.setOffset(offset);

                        javaRefs.add(javaRef);
                    }
                }
            }
        }.accept(js);
    }

    return javaRefs;
}