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.eclipselabs.stlipse.javaeditor.JavaCompletionProposalComputer.java

License:Open Source License

private StringBuilder resolveBeanPropertyName(IJavaElement element) throws JavaModelException {
    StringBuilder result = new StringBuilder();
    int elementType = element.getElementType();
    String elementName = element.getElementName();
    if (elementType == IJavaElement.FIELD) {
        result.append(elementName);/*from  w ww.  j  ava 2 s.  c o  m*/
    } else if (elementType == IJavaElement.METHOD) {
        IMethod method = (IMethod) element;
        if (Flags.isPublic(method.getFlags()) && (isSetter(method) || isGetter(method))) {
            result.append(BeanPropertyVisitor.getFieldNameFromAccessor(elementName));
        }
    }
    return result;
}

From source file:org.fastcode.popup.actions.easycreate.CopyMemberAction.java

License:Open Source License

/**
 * This method is used by both copying from existing member or creating new
 * member./*from  w  ww  . j av  a 2 s .c  o  m*/
 *
 * @param type
 * @param newField
 * @throws Exception
 *
 */
protected void copySelectedMember(final IType type, final IJavaElement element, String newField)
        throws Exception {
    final GlobalSettings globalSettings = GlobalSettings.getInstance();

    if (element == null) {
        openError(this.editorPart.getSite().getShell(), "Selection Error",
                "Please select some field and try again.");
        return;
    }

    switch (element.getElementType()) {
    case IJavaElement.TYPE:
        if (!isMemberNameSelected(type, (ITextSelection) this.selection)) {
            openError(this.editorPart.getSite().getShell(), "Selection Error",
                    "Please select part or whole name of a method/field/type and try again.");
            return;
        }
        if (!element.equals(type)) {
            openError(this.editorPart.getSite().getShell(), "Selection Error",
                    "Please select part or whole name of primary type and try again.");
            return;
        }
        final InputDialog typeInputDialog = new InputDialog(this.editorPart.getSite().getShell(), "New Name",
                "Enter a new name for class or names (space separated)", EMPTY_STR, null);
        if (typeInputDialog.open() == Window.CANCEL) {
            return;
        }
        final String newTypeName = typeInputDialog.getValue();

        final ICompilationUnit retCompUnit = copyType(type, newTypeName, (ITextSelection) this.selection);
        if (retCompUnit != null) {
            final IEditorPart javaEditor = JavaUI.openInEditor(retCompUnit);
            JavaUI.revealInEditor(javaEditor, (IJavaElement) retCompUnit.findPrimaryType());
        }
        break;
    case IJavaElement.IMPORT_DECLARATION:
        final IImportDeclaration importDeclaration = (IImportDeclaration) element;
        copyImport(type, importDeclaration, (ITextSelection) this.selection);
        break;
    case IJavaElement.FIELD:
        final IField field = (IField) element;
        if (!isMemberNameSelected(field, (ITextSelection) this.selection)) {
            openError(this.editorPart.getSite().getShell(), "Selection Error",
                    "Please select part or whole name of a method/field and try again.");
            return;
        }

        //final boolean createGetterSetter = globalSettings.isGetterSetterForPrivateFields();

        if (isEmpty(newField) && (isPrivate(field.getFlags()) || isProtected(field.getFlags()))
                || this.allowMultiple || doesGetterSetterExist(field) != GETTER_SETTER.NONE) {
            final InputDialog fieldInputDialog = new InputDialog(this.editorPart.getSite().getShell(),
                    "New Name", "Enter a new name for field or names (space separated)", EMPTY_STR, null);
            if (fieldInputDialog.open() == Window.CANCEL) {
                return;
            }
            newField = fieldInputDialog.getValue();
        } else if (isEmpty(newField)) {
            newField = "copyOf";
        }

        copyField(type, field, newField, (ITextSelection) this.selection);
        break;
    case IJavaElement.METHOD:
        if (!isMemberNameSelected((IMethod) element, (ITextSelection) this.selection)) {
            MessageDialog.openError(this.editorPart.getSite().getShell(), "Selection Error",
                    "Please select part or whole name of a method/field and try again.");
            return;
        }
        boolean copyBody = true;
        if (type.isClass()) {
            if (globalSettings.getCopyMethodBody() == CREATE_OPTIONS_CHOICE.NEVER_CREATE) {
                copyBody = false;
            } else if (globalSettings.getCopyMethodBody() == CREATE_OPTIONS_CHOICE.ASK_TO_CREATE) {
                final MessageDialogWithToggle dialogWithToggle = MessageDialogWithToggle.openYesNoQuestion(
                        this.editorPart.getSite().getShell(), "Copy Method Body",
                        "Would you like to copy method's body as well?", "Remember Decision", false,
                        Activator.getDefault().getPreferenceStore(), P_ASK_FOR_COPY_METHOD_BODY);
                if (dialogWithToggle.getReturnCode() != MESSAGE_DIALOG_RETURN_YES) {
                    copyBody = false;
                }
            }
        }
        copyMethod(type, (IMethod) element, (ITextSelection) this.selection, copyBody);
        break;
    default:
        break;
    }
}

From source file:org.grails.ide.eclipse.editor.gsp.search.FindTagReferences.java

License:Open Source License

/**
 * only check for tags if the java element states that 
 * is a field in a taglib//w w  w  .j  ava 2  s . co m
 * @param elt
 * @return the tag name if the specification corresponds to a tag, or else null
 */
boolean shouldSearchForTagRefs(IJavaElement elt) {
    // strangely, if the referenced tag is from a class file, then the type is ILocalVariable
    if (elt.getElementType() == IJavaElement.LOCAL_VARIABLE) {
        elt = elt.getParent();
    }
    if (elt.getElementType() == IJavaElement.FIELD) {
        ICompilationUnit unit = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (unit instanceof GroovyCompilationUnit) {
            if (GrailsWorkspaceCore.isTagLibClass((GroovyCompilationUnit) unit)) {
                tagField = elt;
                tagName = tagField.getElementName();
            }
        } else {
            // could be a built in tag
            IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
            if (type != null && type.isReadOnly() && type.getElementName().endsWith("TagLib")) {
                IPackageFragment frag = (IPackageFragment) elt.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (frag.getElementName().equals("org.codehaus.groovy.grails.plugins.web.taglib")) {
                    tagField = elt;
                    tagName = tagField.getElementName();
                }
            }
        }
    }
    return tagField != null;
}

From source file:org.grails.ide.eclipse.search.action.ControllerActionSearch.java

License:Open Source License

public ControllerActionSearch(QuerySpecification specification) throws JavaModelException {
    if (specification instanceof ElementQuerySpecification) {
        ElementQuerySpecification spec = (ElementQuerySpecification) specification;
        if (SearchUtil.wantsReferences(spec)) {
            IJavaSearchScope scope = spec.getScope();
            IJavaElement el = spec.getElement();
            if (el.getElementType() == IJavaElement.METHOD || el.getElementType() == IJavaElement.FIELD) {
                IMember targetAction = (IMember) el;
                IType targetType = (IType) el.getParent();
                String targetActionName = targetAction.getElementName();
                IJavaProject project = targetAction.getJavaProject();
                if (project != null) {
                    GrailsProject grailsProject = GrailsWorkspaceCore.get().create(project);
                    if (grailsProject != null) {
                        init(grailsProject, targetType.getElementName(), targetActionName, scope);
                    }/*from   w w  w.j  a va2 s  . co  m*/
                }
            }
        }
    }
}

From source file:org.jboss.tools.seam.ui.search.SeamSearchEngine.java

License:Open Source License

/**
 * Checks if the given element is IField
 * /*from  ww w  . j  av a2  s.  co  m*/
 * @param element
 * @return
 */
public static boolean isField(IJavaElement element) {
    if (element == null)
        return false;

    return (element.getElementType() == IJavaElement.FIELD);
}

From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentSymbolHandler.java

License:Open Source License

private void collectChildren(ITypeRoot unit, IJavaElement[] elements, ArrayList<SymbolInformation> symbols)
        throws JavaModelException {
    for (IJavaElement element : elements) {
        if (element.getElementType() == IJavaElement.TYPE) {
            collectChildren(unit, ((IType) element).getChildren(), symbols);
        }/*from   w  w w.j  ava2s  .  c  om*/
        if (element.getElementType() != IJavaElement.FIELD && element.getElementType() != IJavaElement.METHOD) {
            continue;
        }

        SymbolInformation si = new SymbolInformation();
        si.setName(element.getElementName());
        si.setKind(new Double(mapKind(element)));
        if (element.getParent() != null)
            si.setContainerName(element.getParent().getElementName());
        si.setLocation(JDTUtils.toLocation(element));
        symbols.add(si);
    }
}

From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentSymbolHandler.java

License:Open Source License

public static int mapKind(IJavaElement element) {
    //      /**// w w  w . j  a va  2s.  c o m
    //      * A symbol kind.
    //      */
    //      export enum SymbolKind {
    //        File = 1,
    //        Module = 2,
    //        Namespace = 3,
    //        Package = 4,
    //        Class = 5,
    //        Method = 6,
    //        Property = 7,
    //        Field = 8,
    //        Constructor = 9,
    //        Enum = 10,
    //        Interface = 11,
    //        Function = 12,
    //        Variable = 13,
    //        Constant = 14,
    //        String = 15,
    //        Number = 16,
    //        Boolean = 17,
    //        Array = 18,
    //      }
    switch (element.getElementType()) {
    case IJavaElement.ANNOTATION:
        return 7; // TODO: find a better mapping 
    case IJavaElement.CLASS_FILE:
    case IJavaElement.COMPILATION_UNIT:
        return 1;
    case IJavaElement.FIELD:
        return 8;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
        return 2;
    case IJavaElement.INITIALIZER:
        return 9;
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        return 13;
    case IJavaElement.METHOD:
        return 12;
    case IJavaElement.PACKAGE_DECLARATION:
        return 3;
    case IJavaElement.TYPE:
        try {
            return (((IType) element).isInterface() ? 11 : 5);
        } catch (JavaModelException e) {
            return 5; //fallback 
        }
    }
    return 15;
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsElementFactory.java

License:Open Source License

/**
 * Attempts to create a new JAX-RS element from the given {@link Annotation}
 * . Multiple elements can be returned, for example if a java method is
 * annotated with a JAX-RS annotation (ex: <code>@Path()</code>), the parent
 * type becomes a JAX-RS Subresource.//from   w w  w . j ava 2  s.c  o  m
 * 
 * @param element
 * @param ast
 * @param metamodel
 * @return the created JAX-RS element or null if the given Java annotation
 *         is not a valid one.
 * @throws CoreException
 */
public static Set<IJaxrsElement> createElements(final IAnnotation javaAnnotation, final CompilationUnit ast,
        final JaxrsMetamodel metamodel, final IProgressMonitor progressMonitor) throws CoreException {
    // unsupported annotation (eg: on package declaration, or underlying java
    // element does not exist or not found) are ignored
    if (javaAnnotation != null) {
        switch (javaAnnotation.getParent().getElementType()) {
        case IJavaElement.TYPE:
            return internalCreateElements((IType) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        case IJavaElement.METHOD:
            return createElements((IMethod) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        case IJavaElement.FIELD:
            return createElements((IField) javaAnnotation.getParent(), ast, metamodel, progressMonitor);
        }
    }
    return Collections.emptySet();
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsMetamodel.java

License:Open Source License

/**
 * Process a single Java Element change//w  w w. j  av  a 2 s  . com
 * 
 * @param delta
 * @param progressMonitor
 * @throws CoreException
 */
public void processJavaElementChange(final JavaElementChangedEvent delta,
        final IProgressMonitor progressMonitor) throws CoreException {
    try {
        Logger.debug("Processing {}", delta);
        readWriteLock.writeLock().lock();
        final IJavaElement element = delta.getElement();
        final CompilationUnit ast = delta.getCompilationUnitAST();
        final int deltaKind = delta.getKind();
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            processProject(progressMonitor);
            break;
        case IJavaElement.ANNOTATION:
            processJavaAnnotationChange((IAnnotation) element, deltaKind, ast, progressMonitor);
            break;
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
        case IJavaElement.FIELD:
            processJavaElementChange(element, deltaKind, ast, progressMonitor);
            break;
        default:
            // ignore
            break;
        }
    } finally {
        this.initializing = false;
        progressMonitor.done();
        readWriteLock.writeLock().unlock();
        setBuildStatus(Status.OK_STATUS);
        Logger.debug("Done processing Java changes: " + getStatus());
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.domain.JaxrsMetamodel.java

License:Open Source License

/**
 * Search for the JAX-RS java-based elements matching the given
 * {@link IJavaElement} in the metamodel.
 * //from w w  w .  j  a  v  a  2  s.c  o  m
 * @param element
 * @param ast
 * @return the matching JAX-RS Elements or an empty set if no JAX-RS
 *         element matched in the metamodel.
 * @throws JavaModelException
 */
private List<IJaxrsElement> searchJaxrsElements(final IJavaElement element) throws JavaModelException {
    if (element == null) {
        return Collections.emptyList();
    }
    try {
        readWriteLock.readLock().lock();
        final List<IJaxrsElement> result = new ArrayList<IJaxrsElement>();
        final Term javaElementTerm = new Term(FIELD_JAVA_ELEMENT, Boolean.TRUE.toString());
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            final Term javaProjectIdentifier = new Term(FIELD_JAVA_PROJECT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, javaProjectIdentifier));
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            final Term packageFragmentRootIdentifier = new Term(FIELD_PACKAGE_FRAGMENT_ROOT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, packageFragmentRootIdentifier));
            break;
        case IJavaElement.COMPILATION_UNIT:
            final Term compilationUnitTerm = new Term(FIELD_COMPILATION_UNIT_IDENTIFIER,
                    element.getHandleIdentifier());
            result.addAll(searchJaxrsElements(javaElementTerm, compilationUnitTerm));
            break;
        case IJavaElement.TYPE:
        case IJavaElement.FIELD:
        case IJavaElement.METHOD:
            final IJaxrsElement foundElement = this.elements.get(element.getHandleIdentifier());
            if (foundElement != null) {
                result.add(foundElement);
            }
            break;
        }
        return result;
    } finally {
        readWriteLock.readLock().unlock();
    }

}