Example usage for org.eclipse.jdt.core IJavaElement METHOD

List of usage examples for org.eclipse.jdt.core IJavaElement METHOD

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement METHOD.

Prototype

int METHOD

To view the source code for org.eclipse.jdt.core IJavaElement METHOD.

Click Source Link

Document

Constant representing a method or constructor.

Usage

From source file:rabbit.tracking.internal.trackers.JavaTracker.java

License:Apache License

/**
 * Gets the actual element that we want before saving. One of the following
 * types is returned:/* w  ww.  ja v a 2  s  .c  o m*/
 * 
 * <ul>
 * <li>A type that is not anonymous.</li>
 * <li>A method that is not enclosed in an anonymous type.</li>
 * <li>An initializer.</li>
 * <li>A compilation unit.</li>
 * <li>A class file.</li>
 * <li>Null</li>
 * </ul>
 * 
 * @param element The element to filter.
 * @return A filtered element, or null if not found.
 * @throws JavaModelException If this element does not exist or if an
 *           exception occurs while accessing its corresponding resource.
 */
private IJavaElement filterElement(@Nullable IJavaElement element) throws JavaModelException {

    if (element == null) {
        return null;
    }

    switch (element.getElementType()) {
    case IJavaElement.TYPE:
        if (((IType) element).isAnonymous()) {
            return filterElement(element.getParent());
        }
        return element;

    case IJavaElement.METHOD:
        if (((IType) element.getParent()).isAnonymous()) {
            return filterElement(element.getParent());
        }
        return element;

    case IJavaElement.INITIALIZER:
    case IJavaElement.COMPILATION_UNIT:
    case IJavaElement.CLASS_FILE:
        return element;

    default:
        return filterElement(element.getParent());
    }
}

From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java

License:Apache License

/**
 * Test an event on an anonymous. This event should be filtered on save, so
 * that instead of showing a user spent x amount of time on the anonymous
 * class, we show that a user spent x amount of time on the anonymous's parent
 * type element (a method or a type that is not anonymous).
 *///from   w  ww . j a v  a2  s  .  c om
@Test
public void testFilter_anonymousClass() throws Exception {
    /*
     * Here we test that: a method containing an anonymous Runnable which also
     * contains another anonymous Runnable, and the most inner Runnable is
     * selected (to emulate that the user is working on that), then when filter
     * on save the data should indicate that the user has spent x amount of time
     * working on the method, not any of the Runnable's.
     */

    JavaEditor editor = closeAndOpenEditor();
    IDocument document = getDocument(editor);

    StringBuilder builder = new StringBuilder();
    builder.append("void aMethod() {");
    builder.append("  new Runnable() { ");
    builder.append("    public void run(){");
    builder.append("      new Runnable() {");
    builder.append("        public void run() {}");
    builder.append("      };");
    builder.append("    } ");
    builder.append("  };");
    builder.append("}");

    String content = document.get();
    int offset = content.indexOf("{") + 1;
    int len = 0;
    document.replace(offset, len, builder.toString());

    content = document.get();
    offset = content.indexOf("Runnable", content.indexOf("Runnable") + 1);
    len = "Runnable".length();
    editor.getSelectionProvider().setSelection(new TextSelection(offset, len));

    long preStart = System.currentTimeMillis();
    tracker.setEnabled(true);
    long postStart = System.currentTimeMillis();

    Thread.sleep(30);

    long preEnd = System.currentTimeMillis();
    tracker.setEnabled(false);
    long postEnd = System.currentTimeMillis();

    // Ask the tracker to save the data, the data should be appropriately
    // filtered
    tracker.saveData();

    assertEquals(1, tracker.getData().size());

    JavaEvent event = tracker.getData().iterator().next();
    long start = event.getInterval().getStartMillis();
    long end = event.getInterval().getEndMillis();
    checkTime(preStart, start, postStart, preEnd, end, postEnd);

    IJavaElement element = getElementAtOffset(editor);
    // This two are to check we've set the selection right in this test:
    assertEquals(IJavaElement.TYPE, element.getElementType());
    assertTrue(((IType) element).isAnonymous());
    // getParent().getParent().getParent() will give us the method:
    assertEquals(IJavaElement.METHOD, event.getElement().getElementType());
    assertEquals("aMethod", event.getElement().getElementName());
}

From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java

License:Apache License

/**
 * Test an event on a method, that is a member of a non-anonymous class. This
 * event should not be filtered on save.
 *///from w w w .jav  a2s. com
@Test
public void testFilter_existingElement_methodParentNotAnonymous() throws Exception {
    JavaEditor editor = closeAndOpenEditor();
    IDocument document = getDocument(editor);
    String methodName = "aMethodName";
    String methodText = format("void %s() {}", methodName);
    int offset = document.get().indexOf("{") + 1;
    int length = 0;
    document.replace(offset, length, methodText);

    offset = document.get().indexOf(methodName);
    length = methodName.length();
    ITextSelection selection = new TextSelection(offset, length);
    editor.getSelectionProvider().setSelection(selection);

    IJavaElement element = getElementAtOffset(editor);
    // Make sure we got the selection right:
    assertEquals(IJavaElement.METHOD, element.getElementType());

    long preStart = System.currentTimeMillis();
    tracker.setEnabled(true);
    long postStart = System.currentTimeMillis();

    Thread.sleep(30);

    long preEnd = System.currentTimeMillis();
    tracker.setEnabled(false);
    long postEnd = System.currentTimeMillis();

    // Ask the tracker to save the data, the data should be appropriately
    // filtered
    tracker.saveData();

    assertEquals(1, tracker.getData().size());

    JavaEvent event = tracker.getData().iterator().next();
    long start = event.getInterval().getStartMillis();
    long end = event.getInterval().getEndMillis();
    checkTime(preStart, start, postStart, preEnd, end, postEnd);
    assertEquals(element, event.getElement());
}

From source file:rabbit.tracking.internal.trackers.JavaTrackerTest.java

License:Apache License

/**
 * Test an event on an anonymous type. This event should be filtered so that
 * we show the user has spent x amount of time on the type's first
 * non-anonymous parent.//from   ww  w.  j  a v  a 2  s  .co m
 */
@Test
public void testFilter_existingElement_typeAnonymous() throws Exception {
    JavaEditor editor = closeAndOpenEditor();
    IDocument document = getDocument(editor);
    StringBuilder anonymous = new StringBuilder();
    anonymous.append("void aMethod() {");
    anonymous.append("  new Runnable() { ");
    anonymous.append("    public void run(){");
    anonymous.append("    } ");
    anonymous.append("  };");
    anonymous.append("}");

    int offset = document.get().indexOf("{") + 1;
    int len = 0;
    document.replace(offset, len, anonymous.toString());

    offset = document.get().indexOf("Runnable");
    len = "Runnable".length();
    ITextSelection selection = new TextSelection(offset, len);
    editor.getSelectionProvider().setSelection(selection);

    IJavaElement element = getElementAtOffset(editor);
    // Check that we got the selection right:
    assertEquals(IJavaElement.TYPE, element.getElementType());

    long preStart = System.currentTimeMillis();
    tracker.setEnabled(true);
    long postStart = System.currentTimeMillis();

    Thread.sleep(35);

    long preEnd = System.currentTimeMillis();
    tracker.setEnabled(false);
    long postEnd = System.currentTimeMillis();

    // Ask the tracker to save the data, the data should be appropriately
    // filtered
    tracker.saveData();

    assertEquals(1, tracker.getData().size());
    JavaEvent event = tracker.getData().iterator().next();
    long start = event.getInterval().getStartMillis();
    long end = event.getInterval().getEndMillis();
    checkTime(preStart, start, postStart, preEnd, end, postEnd);
    assertEquals("aMethod", event.getElement().getElementName());
    assertEquals(IJavaElement.METHOD, event.getElement().getElementType());
}

From source file:rabbit.ui.internal.treebuilders.JavaDataTreeBuilder.java

License:Apache License

/**
 * Checks whether the given Java element is an anonymous type.
 * @param type the element to check.//from   www  . j av  a 2s  .co m
 * @return true if the element is anonymous, false otherwise.
 */
private boolean isAnonymousType(IJavaElement type) {
    if (type.getElementType() == IJavaElement.TYPE) {
        return type.getParent().getElementType() == IJavaElement.METHOD;
    }
    return false;
}

From source file:scala.tools.eclipse.launching.JUnitPropertyTester.java

License:Open Source License

private boolean canLaunchAsJUnitTest(IJavaElement element) {
    try {//www  .j av a  2  s. c o m
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return true; // can run, let test runner detect if there are tests
        case IJavaElement.PACKAGE_FRAGMENT:
            return ((IPackageFragment) element).hasChildren();
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.CLASS_FILE:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
            return isJUnitTest(element);
        default:
            return false;
        }
    } catch (JavaModelException e) {
        return false;
    }
}

From source file:tubame.wsearch.logics.analyzer.JavaAnalyzer.java

License:Apache License

/**
 * The return of the full class name of ASTNode that you specify.<br/>
 * If the package name is not resolved, return only the class name.<br/>
 * If ASTNode specified was not related to class, return null.<br/>
 * /*from w  w  w. ja  v  a2s.c o  m*/
 * 
 * @param node
 *            ASTNode (class that represents the Java code structure name,
 *            type, such as declaration, etc.)
 * @return Full class name
 */
private String getFullyQualifiedName(final ASTNode node) {
    // Cut out the top class name portion of the code
    final String topNodeName = getTopLevelClassName(node);
    // If this is not the class name
    if (topNodeName == null) {
        return null;
    }
    // If it is package directly specified in the code
    if (topNodeName.contains(".")) {
        return topNodeName;
    }
    String result = null;
    String fullname = null;
    String name = null;
    final IJavaElement[] parts = codeSelect(node);
    // If ASTParser has automatic interpretation from information on the
    // classpath
    if (parts.length > 0) {
        IType type = null;
        for (IJavaElement part : parts) {
            switch (part.getElementType()) {
            case IJavaElement.TYPE: // Clazz obj; -> a.b.c.Clazz
                type = ((IType) part);
                break;
            case IJavaElement.METHOD: // new Clazz(); -> a.b.c.Clazz
                type = ((IMethod) part).getDeclaringType();
                break;
            case IJavaElement.FIELD: // Clazz.TYPE_TEST; -> a.b.c.Clazz
                type = ((IField) part).getDeclaringType();
                break;
            default:
                return null;
            }
            fullname = getFullyQualifiedName(type);
            // If the package name is not be obtained
            if (fullname == null || !fullname.contains(".")) {
                continue;
            }
            name = type.getTypeQualifiedName();
            // If the top class name of the code of the report are not
            // included in the class name ASTParser is interpreted
            if (!name.equals(topNodeName) && !name.startsWith(topNodeName + "$")
                    && !name.endsWith("$" + topNodeName) && !name.contains("$" + topNodeName + "$")) {
                continue;
            }
            // Delete (inner class measures) the class name of topNodeName
            // after the class name of the automatic interpretation
            final int index = fullname.indexOf(topNodeName + "$");
            if (index >= 0) {
                fullname = fullname.substring(0, index + topNodeName.length());
            }
            result = getFullyQualifiedName(fullname);
            // Search ends upon unresolved
            if (result != null) {
                break;
            }
        }
    }

    List<String> classNameList = WSearchCacheManager.getInstance().getClassNameList();
    // Interpreting class name only string codes on If not unresolved
    if (result == null) {
        result = getFullyQualifiedName(topNodeName);
    }
    // If that name is not resolved
    if (result == null) {
        // If it is interpreted as a class project under extraction excluded
        if (parts.length == 1 && classNameList.contains(fullname)) {
            return null;
        }
        result = topNodeName;
    }
    return result;
}