Package org.eclipse.jdt.core

# Interface IMethod

All Superinterfaces:
  `org.eclipse.core.runtime.IAdaptable`, `IAnnotatable`, `IJavaElement`, `IMember`, `IParent`, `ISourceManipulation`, `ISourceReference`

public interface IMethod extends IMember, IAnnotatable
Represents a method (or constructor) declared in a type.

Restriction:
  This interface is not intended to be implemented by clients.

## Field Summary

### Fields inherited from interface org.eclipse.jdt.core.IJavaElement

`ANNOTATION, CLASS_FILE, COMPILATION_UNIT, FIELD, IMPORT_CONTAINER, IMPORT_DECLARATION, INITIALIZER, JAVA_MODEL, JAVA_MODULE, JAVA_PROJECT, LOCAL_VARIABLE, METHOD, PACKAGE_DECLARATION, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT, TYPE, TYPE_PARAMETER`

## Method Summary

Modifier and Type
Method
Description
`IMemberValuePair`
`getDefaultValue()`
Returns a `member value pair` representing the default value of this method if any, or `null` if this method's parent is not an annotation type, or else if this method does not have a default value.
`String`
`getElementName()`
Returns the simple name of this method.
`String[]`
`getExceptionTypes()`
Returns the type signatures of the exceptions this method throws, in the order declared in the source.
`String`
`getKey()`
Returns the binding key for this method only if the given method is `resolved`.
`int`
`getNumberOfParameters()`
Returns the number of parameters of this method.
`String[]`
`getParameterNames()`
Returns the names of parameters in this method.
`ILocalVariable[]`
`getParameters()`
Returns the parameters of this method.
`String[]`
`getParameterTypes()`
Returns the type signatures for the parameters of this method.
`String[]`
`getRawParameterNames()`
Returns the names of parameters in this method.
`String`
`getReturnType()`
Returns the type signature of the return value of this method.
`String`
`getSignature()`
Returns the signature of this method.
`ITypeParameter`
`getTypeParameter(String name)`
Returns the type parameter declared in this method with the given name.
`ITypeParameter[]`
`getTypeParameters()`
Returns the formal type parameters for this method.
`String[]`
`getTypeParameterSignatures()`
Deprecated.
Use `getTypeParameters()` instead
`boolean`
`isConstructor()`
Returns whether this method is a constructor.
`boolean`
`isLambdaMethod()`
Returns whether this method represents a lambda expression.
`boolean`
`isMainMethod()`
Returns whether this method is a main method.
`boolean`
`isMainMethodCandidate()`
Returns whether this method is a main method candidate.
`boolean`
`isResolved()`
Returns whether this method represents a resolved method.
`boolean`
`isSimilar(IMethod method)`
Returns whether this method is similar to the given method.

### Methods inherited from interface org.eclipse.core.runtime.IAdaptable

`getAdapter`

### Methods inherited from interface org.eclipse.jdt.core.IAnnotatable

`getAnnotation, getAnnotations`

### Methods inherited from interface org.eclipse.jdt.core.IJavaElement

`exists, getAncestor, getAttachedJavadoc, getCorrespondingResource, getElementType, getHandleIdentifier, getJavaModel, getJavaProject, getOpenable, getParent, getPath, getPrimaryElement, getResource, getSchedulingRule, getUnderlyingResource, isReadOnly, isStructureKnown`

### Methods inherited from interface org.eclipse.jdt.core.IMember

`getCategories, getClassFile, getCompilationUnit, getDeclaringType, getFlags, getJavadocRange, getOccurrenceCount, getType, getTypeRoot, isBinary`

### Methods inherited from interface org.eclipse.jdt.core.IParent

`getChildren, hasChildren`

### Methods inherited from interface org.eclipse.jdt.core.ISourceManipulation

`copy, delete, move, rename`

### Methods inherited from interface org.eclipse.jdt.core.ISourceReference

`exists, getNameRange, getSource, getSourceRange`

## Method Details

### getDefaultValue

IMemberValuePair getDefaultValue() throws JavaModelException
Returns a `member value pair` representing the default value of this method if any, or `null` if this method's parent is not an annotation type, or else if this method does not have a default value.

Note that `IMemberValuePair.getValue()` might return `null`. Please see this method for more details.

Returns:
  a member pair value if any, or `null` if none
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.4

### getElementName

String getElementName()
Returns the simple name of this method. For a constructor, this returns the simple name of the declaring type. Note: This holds whether the constructor appears in a source or binary type (even though class files internally define constructor names to be `"<init>"`). For the class initialization methods in binary types, this returns the special name `"<clinit>"`. This is a handle-only method.

Specified by:
  `getElementName` in interface `IJavaElement`
Returns:
  the simple name of this method

### getExceptionTypes

String[] getExceptionTypes() throws JavaModelException
Returns the type signatures of the exceptions this method throws, in the order declared in the source. Returns an empty array if this method throws no exceptions.

For example, a source method declaring `"throws IOException"`, would return the array `{"QIOException;"}`.

The type signatures may be either unresolved (for source types) or resolved (for binary types), and either basic (for basic types) or rich (for parameterized types). See `Signature` for details.

Returns:
  the type signatures of the exceptions this method throws, in the order declared in the source, an empty array if this method throws no exceptions
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
  - `Signature`

### getTypeParameterSignatures

String[] getTypeParameterSignatures() throws JavaModelException
Deprecated.
Use `getTypeParameters()` instead
Returns the formal type parameter signatures for this method. Returns an empty array if this method has no formal type parameters.

The formal type parameter signatures may be either unresolved (for source types) or resolved (for binary types). See `Signature` for details.

Returns:
  the formal type parameter signatures of this method, in the order declared in the source, an empty array if none
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.0
See Also:
  - `Signature`

### getTypeParameters

ITypeParameter[] getTypeParameters() throws JavaModelException
Returns the formal type parameters for this method. Returns an empty array if this method has no formal type parameters.

Returns:
  the formal type parameters of this method, in the order declared in the source, an empty array if none
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.1

### getNumberOfParameters

int getNumberOfParameters()
Returns the number of parameters of this method. This is a handle-only method.

Returns:
  the number of parameters of this method

### getParameters

ILocalVariable[] getParameters() throws JavaModelException
Returns the parameters of this method.

An empty array is returned, if the method has no parameters.

For binary types, associated source is used to retrieve the `name range`, `source range` and the `flags`.

These local variables can be used to retrieve the `parameter annotations`.

Returns:
  the parameters of this method
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.7

### getKey

String getKey()
Returns the binding key for this method only if the given method is `resolved`. A binding key is a key that uniquely identifies this method. It allows access to:

- generic info for parameterized methods
- the actual return type for references to `Object.getClass()`
- the actual parameter types and return type for references to signature polymorphic methods from class MethodHandle

If the given method is not resolved, the returned key is simply the java element's key.

Returns:
  the binding key for this method
Since:
  3.1
See Also:
  - `IBinding.getKey()`
  - `BindingKey`
  - `isResolved()`

### getParameterNames

String[] getParameterNames() throws JavaModelException
Returns the names of parameters in this method. For binary types, associated source or attached Javadoc are used to retrieve the names. If none can be retrieved, then these names are invented as "arg"+i, where i starts at 0. Returns an empty array if this method has no parameters.

For example, a method declared as `public void foo(String text, int length)` would return the array `{"text","length"}`.

Returns:
  the names of parameters in this method, an empty array if this method has no parameters
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.

### getParameterTypes

String[] getParameterTypes()
Returns the type signatures for the parameters of this method. Returns an empty array if this method has no parameters. This is a handle-only method.

For example, a source method declared as `public void foo(String text, int length)` would return the array `{"QString;","I"}`.

The type signatures may be either unresolved (for source types) or resolved (for binary types), and either basic (for basic types) or rich (for parameterized types). See `Signature` for details.

Returns:
  the type signatures for the parameters of this method, an empty array if this method has no parameters
See Also:
  - `Signature`

### getRawParameterNames

String[] getRawParameterNames() throws JavaModelException
Returns the names of parameters in this method. For binary types, these names are invented as "arg"+i, where i starts at 0 (even if source is associated with the binary or if Javdoc is attached to the binary). Returns an empty array if this method has no parameters.

For example, a method declared as `public void foo(String text, int length)` would return the array `{"text","length"}`. For the same method in a binary, this would return `{"arg0", "arg1"}`.

Returns:
  the names of parameters in this method, an empty array if this method has no parameters
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.2

### getReturnType

String getReturnType() throws JavaModelException
Returns the type signature of the return value of this method. For constructors, this returns the signature for void.

For example, a source method declared as `public String getName()` would return `"QString;"`.

The type signature may be either unresolved (for source types) or resolved (for binary types), and either basic (for basic types) or rich (for parameterized types). See `Signature` for details.

Returns:
  the type signature of the return value of this method, void for constructors
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
  - `Signature`

### getSignature

String getSignature() throws JavaModelException
Returns the signature of this method. This includes the signatures for the parameter types and return type, but does not include the method name, exception types, or type parameters.

For example, a source method declared as `public void foo(String text, int length)` would return `"(QString;I)V"`.

The type signatures embedded in the method signature may be either unresolved (for source types) or resolved (for binary types), and either basic (for basic types) or rich (for parameterized types). See `Signature` for details.

Returns:
  the signature of this method
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
See Also:
  - `Signature`

### getTypeParameter

ITypeParameter getTypeParameter(String name)
Returns the type parameter declared in this method with the given name. This is a handle-only method. The type parameter may or may not exist.

Parameters:
  `name` - the given simple name
Returns:
  the type parameter declared in this method with the given name
Since:
  3.1

### isConstructor

boolean isConstructor() throws JavaModelException
Returns whether this method is a constructor.

Returns:
  true if this method is a constructor, false otherwise
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.

### isMainMethod

boolean isMainMethod() throws JavaModelException
Returns whether this method is a main method. It is a main method if:

- its name is equal to `"main"`
- its return type is `void`
- it is `static` and `public`
- it defines one parameter whose type's simple name is `String[]`

Returns:
  true if this method is a main method, false otherwise
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  2.0

### isMainMethodCandidate

boolean isMainMethodCandidate() throws JavaModelException
Returns whether this method is a main method candidate. It is a main method if:

- its name is equal to `"main"`
- its return type is `void`
- it is `static` and `public`
- it defines one parameter whose type's simple name is `String[]`

Starting with Java 21 there is "preview feature" that allows instance main methods. It is a main method according to this JEP if:

- its name is equal to `"main"`
- its return type is `void`
- it is non-`private`
- it defines one parameter whose type's simple name is `String[]` or no parameter at all

Returns:
  true if this method is a main method, false otherwise
Throws:
  `JavaModelException` - if this element does not exist or if an exception occurs while accessing its corresponding resource.
Since:
  3.36

### isLambdaMethod

boolean isLambdaMethod()
Returns whether this method represents a lambda expression.

Returns:
  true if this method represents a lambda expression, false otherwise.
Since:
  3.10

### isResolved

boolean isResolved()
Returns whether this method represents a resolved method. If a method is resolved, its key contains resolved information.

Returns:
  whether this method represents a resolved method.
Since:
  3.1

### isSimilar

boolean isSimilar(IMethod method)
Returns whether this method is similar to the given method. Two methods are similar if:

- their element names are equal
- they have the same number of parameters
- the simple names of their parameter types are equal

This is a handle-only method.

Parameters:
  `method` - the given method
Returns:
  true if this method is similar to the given method.
Since:
  2.0
See Also:
  - `Signature.getSimpleName(char[])`