Example usage for org.eclipse.jdt.core.search IJavaSearchConstants ANNOTATION_TYPE

List of usage examples for org.eclipse.jdt.core.search IJavaSearchConstants ANNOTATION_TYPE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search IJavaSearchConstants ANNOTATION_TYPE.

Prototype

int ANNOTATION_TYPE

To view the source code for org.eclipse.jdt.core.search IJavaSearchConstants ANNOTATION_TYPE.

Click Source Link

Document

The searched element is an annotation type.

Usage

From source file:com.arcbees.gwtp.plugin.core.presenter.CreatePresenterPage.java

License:Apache License

private void selectContentSlot(Text contentSlot) {
    final List<ResolvedSourceField> contentSlots = new ArrayList<ResolvedSourceField>();

    String stringPattern = "ContentSlot";
    int searchFor = IJavaSearchConstants.ANNOTATION_TYPE;
    int limitTo = IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE;
    int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    SearchPattern searchPattern = SearchPattern.createPattern(stringPattern, searchFor, limitTo, matchRule);

    IJavaProject project = getJavaProject();
    IJavaElement[] elements = new IJavaElement[] { project };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);

    SearchRequestor requestor = new SearchRequestor() {
        @Override//from   w  w  w  .  j  a  v  a 2 s .  co m
        public void acceptSearchMatch(SearchMatch match) {
            // TODO
            System.out.println(match);

            ResolvedSourceField element = (ResolvedSourceField) match.getElement();
            contentSlots.add(element);
        }
    };

    SearchEngine searchEngine = new SearchEngine();
    SearchParticipant[] particpant = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    try {
        searchEngine.search(searchPattern, particpant, scope, requestor, new NullProgressMonitor());
    } catch (CoreException e) {
        // TODO
        e.printStackTrace();
    }

    ResolvedSourceField[] contentListArray = new ResolvedSourceField[contentSlots.size()];
    contentSlots.toArray(contentListArray);

    ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new ILabelProvider() {
        @Override
        public void addListener(ILabelProviderListener listener) {
        }

        @Override
        public void dispose() {
        }

        @Override
        public Image getImage(Object element) {
            return null;
        }

        @Override
        public String getText(Object element) {
            ResolvedSourceField rsf = (ResolvedSourceField) element;
            String name = rsf.getElementName();
            IType type = rsf.getDeclaringType();
            return type.getElementName() + "." + name;
        }

        @Override
        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        @Override
        public void removeListener(ILabelProviderListener listener) {
        }
    });

    dialog.setElements(contentListArray);
    dialog.setTitle("Choose A Content Slot");

    // User pressed cancel
    if (dialog.open() != Window.OK) {
        contentSlot.setText("");
        return;
    }

    Object[] result = dialog.getResult();
    if (result == null || result.length < 1) {
        contentSlot.setText("");
    } else {
        ResolvedSourceField rsf = (ResolvedSourceField) result[0];
        contentSlot.setText(rsf.readableName());
        stringMap.put("contentSlotImport", "import " + rsf.getDeclaringType().getFullyQualifiedName() + ";");
    }
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JaxWsUtils.java

License:Open Source License

/**
 * Searches all the Java types from a Java project that are annotated with @WebService.
 *
 * @param jp the containing Java project
 * @param monitor the progress monitor//from ww w  .ja v  a2s .  c  o m
 * @param includeClasses true to include classes in the search results
 * @param includeInterfaces true to include the interfaces in the search results
 * @return a Map
 * <p>
 * Key = the qualified name of the annotated type<br />
 * Value = the associated service name (in the target WSDL)
 * </p>
 *
 * @throws CoreException if the search could not be launched
 */
public static Map<String, String> getJaxAnnotatedJavaTypes(IJavaProject jp, IProgressMonitor monitor,
        final boolean includeClasses, final boolean includeInterfaces) throws CoreException {

    jp.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
    jp.getProject().build(IncrementalProjectBuilder.FULL_BUILD, monitor);

    final Map<String, String> classNameToServiceName = new HashMap<String, String>();
    SearchPattern pattern = SearchPattern.createPattern(WEB_WS_ANNOTATION, IJavaSearchConstants.ANNOTATION_TYPE,
            IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

    // This is what we do when we find a match
    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {

            // We get the Java type that is annotated with @WebService
            if (match.getElement() instanceof IType) {

                // Find the annotation
                IType type = (IType) match.getElement();
                if (type.isInterface() && includeInterfaces || type.isClass() && includeClasses) {

                    IAnnotation ann = type.getAnnotation(WEB_WS_ANNOTATION);
                    if (!ann.exists())
                        ann = type.getAnnotation(SHORT_WEB_WS_ANNOTATION);

                    // Get the service name and store it
                    if (ann.exists()) {
                        String serviceName = null;
                        for (IMemberValuePair pair : ann.getMemberValuePairs()) {
                            if ("serviceName".equalsIgnoreCase(pair.getMemberName())) {
                                serviceName = (String) pair.getValue();
                                break;
                            }
                        }

                        if (serviceName == null)
                            serviceName = type.getElementName() + "Service";

                        classNameToServiceName.put(type.getFullyQualifiedName(), serviceName);
                    }
                }
            }
        }
    };

    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createJavaSearchScope(new IJavaElement[] { jp }, false), requestor, monitor);

    return classNameToServiceName;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JaxWsUtils.java

License:Open Source License

/**
 * Deletes all the classes annotated with @WebServiceClient.
 *
 * @param jp the containing Java project
 * @param monitor the progress monitor/*  w ww  . ja  v a2 s  .c  om*/
 * @throws CoreException if the classes annotated with @WebServiceClient could not be listed
 */
public static void removeWebServiceClient(IJavaProject jp, final IProgressMonitor monitor)
        throws CoreException {

    SearchPattern pattern = SearchPattern.createPattern(WEB_WS_CLIENT_ANNOTATION,
            IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

    // This is what we do when we find a match
    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {

            // We get the Java type that is annotated with @WebService
            if (match.getElement() instanceof IType) {
                IType type = (IType) match.getElement();
                ICompilationUnit cu = type.getCompilationUnit();
                if (cu != null && cu.getCorrespondingResource() != null)
                    cu.getCorrespondingResource().delete(true, monitor);
                else
                    PetalsCommonPlugin.log("A WS client could not be deleted (no compilation unit).",
                            IStatus.ERROR);
            }
        }
    };

    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createJavaSearchScope(new IJavaElement[] { jp }, false), requestor, monitor);
}

From source file:com.gwtplatform.plugin.wizard.NewPresenterWizardPage.java

License:Apache License

protected IType chooseAnnotation() {
    IJavaProject project = getJavaProject();

    if (project == null) {
        return null;
    }//w w w .  ja  v  a  2  s.c  o m

    IJavaElement[] elements = new IJavaElement[] { project };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);

    FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialog(getShell(), false,
            getWizard().getContainer(), scope, IJavaSearchConstants.ANNOTATION_TYPE,
            new AnnotationSelectionExtension());
    dialog.setTitle("Annotation Selection");
    dialog.setMessage("Select the annotation to bind");
    dialog.setInitialPattern("*.client.place.");

    if (dialog.open() == Window.OK) {
        return (IType) dialog.getFirstResult();
    }
    return null;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void handleJavaSearch(String proj, String bid, String patstr, String foritems, boolean defs, boolean refs,
        boolean impls, boolean equiv, boolean exact, boolean system, IvyXmlWriter xw) throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);

    our_plugin.waitForEdits();//from   w ww.java2  s .c  om

    int forflags = 0;
    if (foritems == null)
        forflags = IJavaSearchConstants.TYPE;
    else if (foritems.equalsIgnoreCase("CLASS"))
        forflags = IJavaSearchConstants.CLASS;
    else if (foritems.equalsIgnoreCase("INTERFACE"))
        forflags = IJavaSearchConstants.INTERFACE;
    else if (foritems.equalsIgnoreCase("ENUM"))
        forflags = IJavaSearchConstants.ENUM;
    else if (foritems.equalsIgnoreCase("ANNOTATION"))
        forflags = IJavaSearchConstants.ANNOTATION_TYPE;
    else if (foritems.equalsIgnoreCase("CLASS&ENUM"))
        forflags = IJavaSearchConstants.CLASS_AND_ENUM;
    else if (foritems.equalsIgnoreCase("CLASS&INTERFACE"))
        forflags = IJavaSearchConstants.CLASS_AND_INTERFACE;
    else if (foritems.equalsIgnoreCase("TYPE"))
        forflags = IJavaSearchConstants.TYPE;
    else if (foritems.equalsIgnoreCase("FIELD"))
        forflags = IJavaSearchConstants.FIELD;
    else if (foritems.equalsIgnoreCase("METHOD"))
        forflags = IJavaSearchConstants.METHOD;
    else if (foritems.equalsIgnoreCase("CONSTRUCTOR"))
        forflags = IJavaSearchConstants.CONSTRUCTOR;
    else if (foritems.equalsIgnoreCase("PACKAGE"))
        forflags = IJavaSearchConstants.PACKAGE;
    else if (foritems.equalsIgnoreCase("FIELDWRITE"))
        forflags = IJavaSearchConstants.FIELD | IJavaSearchConstants.WRITE_ACCESSES;
    else if (foritems.equalsIgnoreCase("FIELDREAD"))
        forflags = IJavaSearchConstants.FIELD | IJavaSearchConstants.READ_ACCESSES;
    else
        forflags = IJavaSearchConstants.TYPE;

    int limit = 0;
    if (defs && refs)
        limit = IJavaSearchConstants.ALL_OCCURRENCES;
    else if (defs)
        limit = IJavaSearchConstants.DECLARATIONS;
    else if (refs)
        limit = IJavaSearchConstants.REFERENCES;
    else if (impls)
        limit = IJavaSearchConstants.IMPLEMENTORS;

    int mrule = SearchPattern.R_PATTERN_MATCH;
    if (equiv)
        mrule = SearchPattern.R_EQUIVALENT_MATCH;
    else if (exact)
        mrule = SearchPattern.R_EXACT_MATCH;

    SearchPattern pat = SearchPattern.createPattern(patstr, forflags, limit, mrule);
    if (pat == null) {
        throw new BedrockException(
                "Invalid java search pattern `" + patstr + "' " + forflags + " " + limit + " " + mrule);
    }

    FindFilter filter = null;
    if (forflags == IJavaSearchConstants.METHOD) {
        String p = patstr;
        int idx = p.indexOf("(");
        if (idx > 0)
            p = p.substring(0, idx);
        idx = p.lastIndexOf(".");
        if (idx > 0) {
            if (defs)
                filter = new ClassFilter(p.substring(0, idx));
        }
    }

    // TODO: create scope for only user's items
    IJavaElement[] pelt;
    if (ijp != null)
        pelt = new IJavaElement[] { ijp };
    else
        pelt = getAllProjects();

    ICompilationUnit[] working = getWorkingElements(pelt);
    for (ICompilationUnit xcu : working) {
        try {
            BedrockPlugin.logD("WORK WITH " + xcu.isWorkingCopy() + " " + xcu.getPath() + xcu.getSourceRange());
        } catch (JavaModelException e) {
            BedrockPlugin.logD("WORK WITH ERROR: " + e);
        }
    }

    IJavaSearchScope scp = null;
    int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    if (system) {
        fg |= IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES;
        scp = SearchEngine.createWorkspaceScope();
    } else {
        scp = SearchEngine.createJavaSearchScope(pelt, fg);
    }

    SearchEngine se = new SearchEngine(working);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, filter, system);

    BedrockPlugin.logD("BEGIN SEARCH " + pat);
    BedrockPlugin.logD("SEARCH SCOPE " + system + " " + fg + " " + scp);

    try {
        se.search(pat, parts, scp, fh, null);
    } catch (Throwable e) {
        throw new BedrockException("Problem doing Java search: " + e, e);
    }
}

From source file:info.cukes.editor.annotation.ScenarioAnnotationSearch.java

License:Open Source License

/**
 * Parse the current row, looking for a keyword ("Given", "And", etc.). If found, search all java code for that
 * annotation and return all potential matches.
 *///from www. j  av a 2 s .  com
public List<IAnnotation> search(ITextViewer viewer, int line) {
    IDocument doc = viewer.getDocument();
    try {
        IRegion lineRegion = doc.getLineInformation(line);

        final int offs = lineRegion.getOffset();
        final int len = lineRegion.getLength();

        String currentLine = viewer.getDocument().get(offs, len);

        String potentialAnnotationName = annotationInString(currentLine);

        if (potentialAnnotationName == null) {
            return Collections.emptyList();
        }

        /*
         * Found that the row begins with a supported annotation. Try to find a match in code.
         */
        if (supportedAnnotations.containsKey(potentialAnnotationName)) {
            SearchPattern pattern = SearchPattern.createPattern(potentialAnnotationName,
                    IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ALL_OCCURRENCES,
                    SearchPattern.R_EXACT_MATCH);

            /*
             * Keep cached list of matches, unless a resource change has occurred.
             */
            if (matches.get(potentialAnnotationName) != null) {
                return matches.get(potentialAnnotationName);
            }

            try {
                final List<IAnnotation> matchesForAnnotation = new ArrayList<IAnnotation>();
                SearchRequestor requestor = new SearchRequestor() {
                    @Override
                    public void acceptSearchMatch(SearchMatch match) throws CoreException {
                        if (match.getElement() instanceof IMethod) {
                            IAnnotation[] annotations = ((IMethod) match.getElement()).getAnnotations();
                            for (IAnnotation annotation : annotations) {
                                if (supportedAnnotations.containsKey(annotation.getElementName())) {
                                    matchesForAnnotation.add(annotation);
                                }
                            }
                        }
                    }
                };

                if (scopeHasChanged) {
                    updateSearchScope();
                }

                SearchEngine engine = new SearchEngine();

                Activator.getLogservice().log(LogService.LOG_ERROR, "Searching for " + potentialAnnotationName);
                engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                        scope, requestor, null);
                matches.put(potentialAnnotationName, matchesForAnnotation);

                return matchesForAnnotation;

            } catch (CoreException e) {
                Activator.getLogservice().log(LogService.LOG_ERROR, e.getMessage());
            }
        }

    } catch (BadLocationException e) {

    }
    return Collections.emptyList();
}

From source file:net.harawata.mybatipse.refactoring.collector.ResultMapRenameEditCollector.java

License:Open Source License

protected void editJavaReferences(RefactoringStatus result) {
    SearchPattern pattern = SearchPattern.createPattern(MybatipseConstants.ANNOTATION_RESULT_MAP,
            IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { info.getProject() });
    SearchRequestor requestor = new SearchRequestor() {
        @Override//from w  w  w . ja va  2s .  com
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IMethod) {
                IMethod method = (IMethod) element;
                String mapperFqn = method.getDeclaringType().getFullyQualifiedName();
                IAnnotation annotation = method.getAnnotation("ResultMap");
                int oldIdIdx = -1;
                int oldIdLength = info.getOldId().length();
                String newId = info.getNewId();
                if (info.getNamespace().equals(mapperFqn)) {
                    oldIdIdx = annotation.getSource().indexOf("\"" + info.getOldId() + "\"",
                            "@ResultMap(".length());
                }
                // @ResultMap("resultMap1,resultMap2") : this format is not supported.
                // @ReusltMap("resultMap1", "resultMap2") : this format is.
                if (oldIdIdx == -1) {
                    String fullyQualifiedOldId = info.getNamespace() + "." + info.getOldId();
                    oldIdIdx = annotation.getSource().indexOf("\"" + fullyQualifiedOldId + "\"",
                            "@ResultMap(".length());
                    oldIdLength = fullyQualifiedOldId.length();
                    newId = info.getNamespace() + "." + info.getNewId();
                }
                if (oldIdIdx > -1) {
                    int offset = annotation.getSourceRange().getOffset() + oldIdIdx + 1;
                    List<ReplaceEdit> edits = getEdits((IFile) match.getResource());
                    edits.add(new ReplaceEdit(offset, oldIdLength, newId));
                }
            }
        }
    };
    try {
        new SearchEngine().search(pattern, participants, scope, requestor, monitor);
    } catch (CoreException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    }
}

From source file:net.harawata.mybatipse.refactoring.collector.StatementRenameEditCollector.java

License:Open Source License

protected void editAnnotation(String annotationType) {
    SearchPattern pattern = SearchPattern.createPattern(annotationType, IJavaSearchConstants.ANNOTATION_TYPE,
            IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { info.getProject() });
    SearchRequestor requestor = new SearchRequestor() {
        @Override//w w w  . j  av  a2 s .  c om
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IMethod) {
                IMethod method = (IMethod) element;
                String mapperFqn = method.getDeclaringType().getFullyQualifiedName();
                for (IAnnotation anno : method.getAnnotations()) {
                    if ("Results".equals(anno.getElementName())) {
                        for (IMemberValuePair resultsParam : anno.getMemberValuePairs()) {
                            if ("value".equals(resultsParam.getMemberName())) {
                                Object resultsValue = resultsParam.getValue();
                                if (resultsValue instanceof Object[]) {
                                    for (Object resultAnno : (Object[]) resultsValue) {
                                        parseResultAnnotation((IAnnotation) resultAnno, mapperFqn,
                                                (IFile) match.getResource());
                                    }
                                } else {
                                    parseResultAnnotation((IAnnotation) resultsValue, mapperFqn,
                                            (IFile) match.getResource());
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        protected void parseResultAnnotation(IAnnotation resultAnno, String mapperFqn, IFile file)
                throws JavaModelException {
            for (IMemberValuePair resultParam : resultAnno.getMemberValuePairs()) {
                String name = resultParam.getMemberName();
                if ("one".equals(name) || "many".equals(name)) {
                    IAnnotation annotation = (IAnnotation) resultParam.getValue();
                    int oldIdIdx = -1;
                    int oldIdLength = info.getOldId().length();
                    String newId = info.getNewId();
                    String source = annotation.getSource();
                    if (info.getNamespace().equals(mapperFqn)) {
                        oldIdIdx = source.indexOf(quotedOldId);
                    }
                    if (oldIdIdx == -1) {
                        oldIdIdx = source.indexOf(quotedFullyQualifiedOldId);
                        oldIdLength = fullyQualifiedOldId.length();
                        newId = fullyQualifiedNewId;
                    }
                    if (oldIdIdx > -1) {
                        int offset = annotation.getSourceRange().getOffset() + oldIdIdx + 1;
                        List<ReplaceEdit> edits = getEdits(file);
                        edits.add(new ReplaceEdit(offset, oldIdLength, newId));
                    }
                    break;
                }
            }
        }
    };

    try {
        new SearchEngine().search(pattern, participants, scope, requestor, monitor);
    } catch (CoreException e) {
        Activator.log(Status.ERROR, e.getMessage(), e);
    }
}

From source file:org.codehaus.groovy.eclipse.codeassist.processors.TypeCompletionProcessor.java

License:Apache License

/**
 * @return//from   www .  j av  a 2 s . c  o m
 */
private int getSearchFor() {
    switch (getContext().location) {
    case EXTENDS:
        return IJavaSearchConstants.CLASS;
    case IMPLEMENTS:
        return IJavaSearchConstants.INTERFACE;
    case EXCEPTIONS:
        return IJavaSearchConstants.CLASS;
    case ANNOTATION:
        return IJavaSearchConstants.ANNOTATION_TYPE;
    default:
        return IJavaSearchConstants.TYPE;
    }
}

From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java

License:Open Source License

/**
 * Translates the string type to the int equivalent.
 *
 * @param type The String type./*w  w  w. ja  v a 2  s . c o  m*/
 * @return The int type.
 */
protected int getType(String type) {
    if (TYPE_ANNOTATION.equals(type)) {
        return IJavaSearchConstants.ANNOTATION_TYPE;
    } else if (TYPE_CLASS.equals(type)) {
        return IJavaSearchConstants.CLASS;
    } else if (TYPE_CLASS_OR_ENUM.equals(type)) {
        return IJavaSearchConstants.CLASS_AND_ENUM;
    } else if (TYPE_CLASS_OR_INTERFACE.equals(type)) {
        return IJavaSearchConstants.CLASS_AND_INTERFACE;
    } else if (TYPE_CONSTRUCTOR.equals(type)) {
        return IJavaSearchConstants.CONSTRUCTOR;
    } else if (TYPE_ENUM.equals(type)) {
        return IJavaSearchConstants.ENUM;
    } else if (TYPE_FIELD.equals(type)) {
        return IJavaSearchConstants.FIELD;
    } else if (TYPE_INTERFACE.equals(type)) {
        return IJavaSearchConstants.INTERFACE;
    } else if (TYPE_METHOD.equals(type)) {
        return IJavaSearchConstants.METHOD;
    } else if (TYPE_PACKAGE.equals(type)) {
        return IJavaSearchConstants.PACKAGE;
    }
    return IJavaSearchConstants.TYPE;
}