java.lang.MethodType Class in Java (original) (raw)
Last Updated : 08 Jul, 2021
MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method.
- All the instances of methodType are immutable. This means the objects of the methodType class cannot be overridden by any other objects data.
- In more or less situations the methodType Objects are derived from Byte code instructions.
- Like all other classes, the MethodType class can also individually represent CONSTANT_MethodType to the pool of constants.
- Most of the methods of class MethodType are defined using Static cause these methods need to bound to their Methodtype class rather than to the Objects.
Syntax: Class declaration
public final class MethodType extends Object implements Serializable {
// MethodType extends Object Class which is root of class hierarchy // The serialization interface has no methods or fields and serves only // to identify the semantics of being serializable.
}
Methods of MethodType class:
Methods | Description |
---|---|
appendParameterTypes(Class<?>… ptypesToInsert) | This method Finds or creates a method type with additional parameter types. |
appendParameterTypes(List<Class<?>> ptypesToInsert) | This method Finds or creates a method type with additional parameter types. |
changeParameterType() | This method finds or creates a method type with a single different parameter type. |
changeReturnType() | This method finds or creates a method type with a different return type. |
dropParameterTypes() | This method finds or creates a method type with some parameter types omitted. |
equals() | This method Compares the specified object with this type for equality. |
erase() | This method Erases all reference types to Object. |
fromMethodDescriptorString() | This method finds or creates an instance of a method type, given the spelling of its bytecode descriptor. |
generic() | This method Converts all types, both reference and primitive, to Object. |
genericMethodType(int objectArgCount) | This method finds or creates a method type whose components are all Object. |
genericMethodType(int objectArgCount, boolean finalArray) | This methodfinds or creates a method type whose components are Object with an optional trailing Object[] array. |
hashCode() | This method returns the hash code value for this method type. |
hasPrimitives() | This method reports if this type contains a primitive argument or return value. |
hasWrappers() | This method reports if this type contains a wrapper argument or return value. |
insertParameterTypes(int num, Class<) | This method Finds or creates a method type with additional parameter types. |
insertParameterTypes(int num, List<Class<?>> ptypesToInsert) | This method Finds or creates a method type with additional parameter types. |
methodType(Class<?> rtype) | This method Finds or creates a method type with the given components. |
methodType(Class rtype, Class ptype0) | This method Finds or creates a method type with the given components. |
methodType(Class rtype, Class[] ptypes) | This method Finds or creates an instance of the given method type. |
methodType(Class rtype, Class ptype0, Class<?>… ptypes) | This method Finds or creates a method type with the given components. |
methodType(Class rtype, List> ptypes) | This method Finds or creates a method type with the given components. |
methodType(Class<?> rtype, MethodType ptypes) | This method Finds or creates a method type with the given components. |
parameterArray() | This method Presents the parameter types as an array (a convenience method). |
parameterCount() | This method returns the number of parameter types in this method type. |
parameterList() | This method Presents the parameter types as a list (a convenience method). |
parameterType(int num) | This method returns the parameter type at the specified index, within this method type. |
returnType() | This method returns the return type of this method type. |
toMethodDescriptorString() | This method Produces a bytecode descriptor representation of the method type. |
toString() | This method returns a string representation of the method type, of the form “(PT0,PT1…)RT”. |
unwrap() | This method Converts all wrapper types to their corresponding primitive types. |
wrap() | This method Converts all primitive types to their corresponding wrapper types. |
Example Methods of this class
Java
import
java.lang.invoke.MethodHandle;
import
java.lang.invoke.MethodHandles;
import
java.lang.invoke.MethodType;
class
GFG {
`` static
void
hello()
`` {
`` System.out.println(
"GeeksForGeeks"
);
`` }
`` public
static
void
main(String[] args)
throws
Throwable
`` {
`` MethodHandles.Lookup lookup
`` = MethodHandles.lookup();
`` MethodHandle mh = lookup.findStatic(
`` GFG.
class
,
"hello"
,
`` MethodType.methodType(
void
.
class
));
`` mh.invokeExact();
`` }
}
Similar Reads
- java.lang.reflect.Method Class in Java java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to oc 3 min read
- Java.lang.Void Class in Java Java.lang.Void class is a placeholder that holds a reference to a class object if it represents a void keyword. It is an uninstantiable placeholder. Well, uninstantiable means that this class has a private constructor and no other constructor that we can access from outside. Methods of lang.void cla 1 min read
- Java.lang.Number Class in Java Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method 9 min read
- Java.lang.Package Class in Java In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loade 9 min read
- java.lang.reflect.Modifier Class in Java The java.lang.reflect.Modifier class contains methods used to get information about class, member and method access modifiers. The modifiers are represented as int value with set bits at distinct positions. The int value represents different modifiers. These values are taken from the tables in secti 7 min read
- java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria 4 min read
- Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It 15+ min read
- java.lang.reflect.Parameter Class in Java java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for 4 min read
- Java.lang.Enum Class in Java Enum class is present in java.lang package. It is the common base class of all Java language enumeration types. For information about enums, refer enum in java Class Declaration public abstract class Enum extends Object implements Comparable, Serializable As we can see, that En 8 min read
- java.lang.reflect.Field Class in Java The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t 5 min read