Recommenders/CodeSearch

From Eclipsepedia

Jump to: navigation, search

Code Recommenders - Code Search Engine

If Siri were a code search engine... what would you ask it?

This page collects your demands on what search questions a code search engine should be able to answer. Do you have some frequently reoccurring searches you ask your code search engine? Then add it on this page (no matter how complex or simple the question is). Note, JDT's code search offers a quite sophisticated code search engine that allows you to specify many different options such as

  • [Search String] what literal to look for
  • [Limit to] what kind or where in code you are looking for:
    • any location
    • references
    • declarations
    • implementor
  • [Match Location] where in code on statement level the search literal should match in a compilation unit (specified in Match Location Dialog),
    • in (any kind of) declaration
      • Imports
      • super type declarations
      • annotations
      • field types
      • local variable types
      • method return types
      • method parameter types
      • thrown exception types
    • in (any kind of) parameterized types
      • type parameter bounds
      • wildcard bounds
      • type arguments
    • in expressions:
      • cast expressions
      • catch clauses
      • instanceof expressions
      • class instance creations
      • thrown exception types
  • [Search for] which type of java element you are looking for when searching for a string:
    • Type
    • Constructor
    • Method
    • Field
    • Package
  • [Search in] where' to search for potential hits (sources, required projects, JRE libraries, App libraries), and
  • [Scope] which further limits the Search in clause.

Quite a lot of different locations you can specify where JDT is searching for the occurrence of the given literal. This is, however, sometimes not enough if you want to use more complex queries. What do you have in mind? What query would you like to ask Siri?

[tobias and I added a few sample queries using his SQL-like syntax. It doesn't pay out well but it's still in here for the records]

Siri, ...

... who calls this method? (solvable by JDT call hierarchy) (target type, method, try-catch, finally not defined)

UsedMethods:org.test.SomeClass.doSomething 
UsedMethodsInTry:org.test.SomeClass.doSomething
UsedMethodsInFinally:org.test.SomeClass.doSomething

ANY  where USED_METHODS contains (Type.MethodName(**)*, ..)
ANY  where USED_METHODS contains (*Button.setText, ..) // returns any call to any Button.setText ( join over all overloaded methods)
(Types, Methods, Try-Catch-Finally)  where USED_METHODS contains (*Button.setText, ..) // returns any call to any Button.setText ( join over all overloaded methods)

... who calls these 3 methods together in a method?

Type:method 
AND UsedMethods:(+org.test.SomeClass.method1 +org.test.SomeClass.method2 +org.test.SomeClass.method3)
Methods  where USED_METHODS contains (+*Button.setText +*Button.xy) // do we need + for and? or do we think + is implicit/required?
// SIDE_NOTE: *Button --> should be Button only, i.e., (.*).Button, i.e., exact simple-name match

... who uses this type in his own code? (as in JDT's References workspace search)

UsedTypes:java.util.List
ANY  where USED_TYPE contains (+*Button) // do we need + for and? or do we think + is implicit/required?


... who uses these 3 types together in a method?

Type:method AND UsedTypes:(+java.util.List +java.util.Map +java.lang.Exception)
ANY  where USED_TYPE contains (+*Button, *Label,*String) // do we need + for and? or do we think + is implicit/required?


... who extends this type in his own code? (as in JDT class hierarchy)

For direct descendants:
ExtendedTypes:java.lang.Exception
For the whole hierarchy:
AllExtendedTypes:java.lang.Exception


TYPES  where EXTENDS  oneof (*Button, *String)
TYPES  where EXTENDS  is  *Button


... tell me which public methods exist in my libraries that return an instance of type X?

Type:method AND Modifiers:public AND ReturnType:java.util.List
METHODS  where is Public AND return-type is *List


... tell me which public, static methods exist in my library (file-locations) that return an instance of type X?

METHODS  where is (+public +static) AND return-type is *List  in-file (<file-locations>) // open question:  in vs. oneof , own keywords? file-list this is an OR
static X *


... tell me which classes exist in my project that offer static fields or methods?

TYPES  have/contains/has???
   (
     ( (METHODS, FIELDS) where modifier (+public +static) ) 
  )
static * *


... tell me which classes exist in my project that offer static fields or methods of (return) type IWorkbench?

TYPES  have/contains/has???
   (
     (METHODS, FIELDS) where modifier (+public +static)  return-type is IWorkbench  in-project <project>// return-type for fields?
  )
static *IWorkbench *(..)

... find all Type/Method/Constructor/Package/Field declarations that are named like "someName*" (JDT search) Type, method (including constructors) and field declarations are indexed with a FriendlyName:

FriendlyName:someName*
ANY  where name is someName*
* someName*(..)

... find all references in $location of (one of:) Type/Method/Constructor/Package/Field declarations named like "someName*" (JDT search - call hierarchy)

Possible based on file location

FriendlyName:someName* AND ResourcePath:C:/eclipseworkspace/project/*

as well as project

FriendlyName:someName* AND ProjectName:TestProject
ANY  USED_TYPES/USED_METHODS/USED_FIELD contains someName* // how to deal with fully qualified names? --> index fqName to allow exact search


... find all implementors of Type/Method named like "someName*" (JDT search)

All implementors of type Iterable:

ImplementedTypes:java.lang.Iterable

All implementors of method iterator()

OverriddenMethods:java.lang.Iterable.iterator

Interface:

 TYPES where implements contains (TypeName)

override method x:

 TYPES where overriddenMethods contains (MethodName) // --> method should know with method it overrides

... find all static methods with "IJavaElement" in the parameter list, that return "String" and contain the case-insensitive word "label" in its name

Type:method AND Modifiers:static AND ParameterTypes:java.util.List AND ReturnType:java.lang.String AND FriendlyName:*Label*

At the moment, indexing happens case sensitive. A workaround would be

Type:method AND Modifiers:static AND ParameterTypes:java.util.List AND ReturnType:java.lang.String AND FriendlyName:(*Label* *label)
 METHODS where  modifier contains (static) AND return-type is/oneof (String)  and name is *label* // case-insensitive is not implemented yet.
static *String *label*(.., IJavaElement, ..)

... find all methods with a "String" as the only parameter, that return a "String" and contain the word "html" as well as either "strip" or "remove" in its name

ParameterTypes:java.lang.String AND ParameterCount:1 AND FriendlyName:*html* AND FriendlyName:(*strip* *remove*)
 METHODS where parameter-types contains (String),  return-type is/oneof (String)  and name is *label* // case-insensitive is not implemented yet.
*String *html*(String)


... find all methods that override the method "iterator" of "Iterable" The type containing the method can be queried like this

OverriddenMethods:java.lang.Iterable.iterator

... find all methods (in class/package xyz) which return an instance stored in a local variable where the name of said variable isn't "result" (for cleaning up code)

Type:method AND ReturnType:* AND !ReturnVariableExpressions:result

... find all places in the code where the nesting level of loops and if statements is greater than four (find deeply nested for/if/while constructs)

... tell me where instanceof is used with type Xyz

InstanceofTypes:*Xyz

... find all loops that iterate over type A and which call B.foo(A)

... tell me where an exception is thrown that uses a certain type when building the message argument

... give me a list of all methods that contain a SuppressWarning annotations to suppress "rawtype"

Type:method AND Annotations:*SuppressWarnings*

... find all code that converts between bytes and chars without giving an encoding (must find Stream <-> Reader/Writer plus String.getBytes() and new String(byte[]) calls plus some calls in IOUtils).