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

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

Introduction

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

Prototype

int FIELD

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

Click Source Link

Document

Constant representing a field.

Usage

From source file:org.springframework.ide.eclipse.beans.ui.search.jdt.BeansJavaSearchParticipant.java

License:Open Source License

public void search(final ISearchRequestor requestor, QuerySpecification querySpecification,
        IProgressMonitor monitor) {/*from   w  w w .  j a  v a  2  s  .c o  m*/

    if (querySpecification.getLimitTo() != LIMIT_TO_REF && querySpecification.getLimitTo() != LIMIT_TO_ALL) {
        return;
    }

    String search = null;
    List<String> requiredTypeNames = new ArrayList<String>();
    IJavaProject project = null;

    if (querySpecification instanceof ElementQuerySpecification) {
        ElementQuerySpecification elementQuerySpecification = (ElementQuerySpecification) querySpecification;
        if (elementQuerySpecification.getElement() instanceof IType) {
            search = ((IType) elementQuerySpecification.getElement()).getFullyQualifiedName();
            project = ((IType) elementQuerySpecification.getElement()).getJavaProject();
        } else if (elementQuerySpecification.getElement() instanceof IField) {
            IField field = ((IField) elementQuerySpecification.getElement());
            search = field.getElementName();
            getTypeHierachy(monitor, requiredTypeNames, field.getDeclaringType());
            project = field.getJavaProject();
        } else if (elementQuerySpecification.getElement() instanceof IMethod) {
            IMethod method = ((IMethod) elementQuerySpecification.getElement());
            search = method.getElementName();
            // do property name magic
            if (search.startsWith("set")) {
                search = StringUtils.uncapitalize(search.substring(3));
            }
            getTypeHierachy(monitor, requiredTypeNames, method.getDeclaringType());
            project = method.getJavaProject();
        } else {
            search = elementQuerySpecification.getElement().getElementName();
        }

        int type = ((ElementQuerySpecification) querySpecification).getElement().getElementType();
        if (type == IJavaElement.TYPE) {
            searchFor = SEARCH_FOR_TYPES;
        } else if (type == IJavaElement.FIELD || type == IJavaElement.METHOD) {
            searchFor = SEARCH_FOR_FIELDS;
        }
    } else {
        searchFor = ((PatternQuerySpecification) querySpecification).getSearchFor();
        search = ((PatternQuerySpecification) querySpecification).getPattern();
    }

    List<ISearchQuery> queries = new ArrayList<ISearchQuery>();
    BeansSearchScope scope = BeansSearchScope.newSearchScope();
    if (searchFor == SEARCH_FOR_TYPES) {
        queries.add(new BeanClassQuery(scope, search, true, false));
    } else if (searchFor == SEARCH_FOR_FIELDS) {
        queries.add(new BeanPropertyQuery(scope, search, true, false));
        queries.add(new BeanReferenceQuery(scope, search, true, false));
    }

    for (ISearchQuery query : queries) {
        query.run(monitor);

        BeansSearchResult searchResult = (BeansSearchResult) query.getSearchResult();
        for (Object obj : searchResult.getElements()) {
            Match[] matches = searchResult.getMatches(obj);
            if (matches != null && matches.length > 0) {
                for (Match match : matches) {
                    if (match.getElement() instanceof IBean) {
                        IBean bean = (IBean) match.getElement();
                        IType type = JdtUtils.getJavaType(bean.getElementResource().getProject(),
                                bean.getClassName());
                        if (project == null || (type != null && project.isOnClasspath(type))) {
                            if (searchFor == SEARCH_FOR_FIELDS) {
                                // check if the match fits to the selected class
                                String beanClass = BeansModelUtils.getBeanClass(bean, null);
                                if (requiredTypeNames.contains(beanClass)) {
                                    requestor.reportMatch(match);
                                }
                            } else {
                                requestor.reportMatch(match);
                            }
                        }
                    } else {
                        requestor.reportMatch(match);
                    }
                }
            }
        }
    }
}

From source file:org.summer.dsl.model.ui.refactoring.JdtRenameRefactoringProcessorFactory.java

License:Open Source License

public JavaRenameProcessor createRenameProcessor(IJavaElement element) {
    try {/*from w  w w .j ava  2  s  .  c o m*/
        switch (element.getElementType()) {
        case IJavaElement.TYPE:
            return new RenameTypeProcessor((IType) element);
        case IJavaElement.FIELD:
            return new RenameFieldProcessor((IField) element);
        case IJavaElement.METHOD:
            if (((IMethod) element).isConstructor())
                break;
            if (Flags.isStatic(((IMethod) element).getFlags()))
                return new RenameNonVirtualMethodProcessor((IMethod) element);
            else
                return new RenameVirtualMethodProcessor((IMethod) element);
        case IJavaElement.TYPE_PARAMETER:
            return new RenameTypeParameterProcessor((ITypeParameter) element);
        case IJavaElement.LOCAL_VARIABLE:
            return new RenameLocalVariableProcessor((ILocalVariable) element);

        }
    } catch (JavaModelException exc) {
        LOG.error("Error creating refactoring processor for " + element.getElementName(), exc);
    }
    return null;
}

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

License:Apache License

/**
 * Test an event on a field. This event should be filtered on save, so that
 * instead of showing a user spent x amount of time on the field, we show that
 * a user spent x amount of time on the field's parent type.
 *//*from   w ww  . j a v a 2s. c om*/
@Test
public void testFilter_exsitingElement_field() throws Exception {
    JavaEditor editor = closeAndOpenEditor();
    IDocument document = getDocument(editor);
    String fieldName = "aFieldName";
    String methodText = format("private int %s = 1;", fieldName);
    int offset = document.get().indexOf("{") + 1;
    int len = 0;
    document.replace(offset, len, methodText);

    offset = document.get().indexOf(fieldName);
    len = fieldName.length();
    editor.getSelectionProvider().setSelection(new TextSelection(offset, len));

    IJavaElement element = getElementAtOffset(editor);
    assertEquals(IJavaElement.FIELD, element.getElementType());

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

    Thread.sleep(20);

    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);

    // The filtered event should be on the field's parent, not on the field
    // itself:
    assertEquals(element.getParent(), event.getElement());
}

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

License:Apache License

/**
 * Checks whether the given Java element is a field.
 * @param element the element to check./*ww w.  ja  v a  2s  .  com*/
 * @return true if the element is a field, false otherwise.
 */
private boolean isField(IJavaElement element) {
    return element.getElementType() == IJavaElement.FIELD;
}

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/>
 * // w w  w.  j  av a  2s. co 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;
}