Class getDeclaredMethods() in Java

getDeclaredMethods(): This method is available in the java.lang.Class of Java.

Syntax:

Method[] java.lang.Class.getDeclaredMethods() throws SecurityException

This method returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

Note:

1. If this Class object represents a type that has multiple declared methods with the same name and parameter types, but different return types, then the returned array has a Method object for each such method.

2. If this Class object represents a type that has a class initialization method <clinit>, then the returned array does not have a corresponding Method object.

3. If this Class object represents a class or interfaces with no declared methods, then the returned array has length 0.

4. If this Class object represents an array type, a primitive type, or void, then the returned array has length 0.

Parameters: NA

Returns: the array of Method objects representing all the declared methods of This class.

Throws:

1. SecurityException - If a security manager, s, is present and any of the following conditions are met:

a). the caller's class loader is not the same as the class loader of this class and invocation of s.checkPermission method with RuntimePermission("accessDeclaredMembers")denies access to the declared methods within this class.

b). the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.

Approach

Java

package com.Class;

import java.util.Arrays;

public class ClassgetDeclaredMethods {

    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.String");

        System.out.println(Arrays.toString(class1.getDeclaredMethods()));
    }
}

Output:

[byte[] java.lang.String.value(), public boolean java.lang.String.equals(java.lang.Object), public int java.lang.String.length(), public java.lang.String java.lang.String.toString(), public int java.lang.String.hashCode(), public void java.lang.String.getChars(int,int,char[],int), public int java.lang.String.compareTo(java.lang.Object), public int java.lang.String.compareTo(java.lang.String), public int java.lang.String.indexOf(int,int), public int java.lang.String.indexOf(java.lang.String), public int java.lang.String.indexOf(java.lang.String,int), public int java.lang.String.indexOf(int), static int java.lang.String.indexOf(byte[],byte,int,java.lang.String,int), static void java.lang.String.checkIndex(int,int), public static java.lang.String java.lang.String.valueOf(char[]), public static java.lang.String java.lang.String.valueOf(java.lang.Object), public static java.lang.String java.lang.String.valueOf(long), public static java.lang.String java.lang.String.valueOf(char[],int,int), public static java.lang.String java.lang.String.valueOf(int), public static java.lang.String java.lang.String.valueOf(char), public static java.lang.String java.lang.String.valueOf(double), public static java.lang.String java.lang.String.valueOf(float), public static java.lang.String java.lang.String.valueOf(boolean), byte java.lang.String.coder(), private static java.lang.Void java.lang.String.rangeCheck(char[],int,int), static void java.lang.String.checkBoundsOffCount(int,int,int), boolean java.lang.String.isLatin1(), public char java.lang.String.charAt(int), public int java.lang.String.codePointAt(int), public int java.lang.String.codePointBefore(int), public int java.lang.String.codePointCount(int,int), public int java.lang.String.offsetByCodePoints(int,int), static void java.lang.String.checkBoundsBeginEnd(int,int,int), public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException, void java.lang.String.getBytes(byte[],int,byte), public byte[] java.lang.String.getBytes(), public void java.lang.String.getBytes(int,int,byte[],int), public byte[] java.lang.String.getBytes(java.nio.charset.Charset), public boolean java.lang.String.contentEquals(java.lang.StringBuffer), public boolean java.lang.String.contentEquals(java.lang.CharSequence), private boolean java.lang.String.nonSyncContentEquals(java.lang.AbstractStringBuilder), public boolean java.lang.String.regionMatches(int,java.lang.String,int,int), public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int), public boolean java.lang.String.startsWith(java.lang.String,int), public boolean java.lang.String.startsWith(java.lang.String), public int java.lang.String.lastIndexOf(java.lang.String), public int java.lang.String.lastIndexOf(int), static int java.lang.String.lastIndexOf(byte[],byte,int,java.lang.String,int), public int java.lang.String.lastIndexOf(int,int), public int java.lang.String.lastIndexOf(java.lang.String,int), public java.lang.String java.lang.String.substring(int,int), public java.lang.String java.lang.String.substring(int), public boolean java.lang.String.isEmpty(), public java.lang.String java.lang.String.replace(char,char), public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence), public boolean java.lang.String.matches(java.lang.String), public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String), public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String), public java.lang.String[] java.lang.String.split(java.lang.String,int), public java.lang.String[] java.lang.String.split(java.lang.String), public java.lang.String java.lang.String.toLowerCase(), public java.lang.String java.lang.String.toLowerCase(java.util.Locale), public java.lang.String java.lang.String.toUpperCase(), public java.lang.String java.lang.String.toUpperCase(java.util.Locale), public java.lang.String java.lang.String.trim(), public java.lang.String java.lang.String.strip(), public java.lang.String java.lang.String.stripLeading(), public java.lang.String java.lang.String.stripTrailing(), private int java.lang.String.indexOfNonWhitespace(), public java.util.stream.Stream java.lang.String.lines(), public java.lang.String java.lang.String.repeat(int), private int java.lang.String.lastIndexOfNonWhitespace(), private static int java.lang.String.outdent(java.util.List), public boolean java.lang.String.isBlank(), public char[] java.lang.String.toCharArray(), public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object[]), public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object[]), public java.lang.String java.lang.String.resolveConstantDesc(java.lang.invoke.MethodHandles$Lookup), public java.lang.Object java.lang.String.resolveConstantDesc(java.lang.invoke.MethodHandles$Lookup) throws java.lang.ReflectiveOperationException, public java.util.stream.IntStream java.lang.String.codePoints(), public boolean java.lang.String.equalsIgnoreCase(java.lang.String), public int java.lang.String.compareToIgnoreCase(java.lang.String), public boolean java.lang.String.endsWith(java.lang.String), public java.lang.CharSequence java.lang.String.subSequence(int,int), public java.lang.String java.lang.String.concat(java.lang.String), public boolean java.lang.String.contains(java.lang.CharSequence), public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence[]), public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable), public java.lang.String java.lang.String.indent(int), public java.lang.String java.lang.String.stripIndent(), public java.lang.String java.lang.String.translateEscapes(), public java.util.stream.IntStream java.lang.String.chars(), public java.lang.Object java.lang.String.transform(java.util.function.Function), public java.lang.String java.lang.String.formatted(java.lang.Object[]), public static java.lang.String java.lang.String.copyValueOf(char[],int,int), public static java.lang.String java.lang.String.copyValueOf(char[]), public native java.lang.String java.lang.String.intern(), static void java.lang.String.checkOffset(int,int), static java.lang.String java.lang.String.valueOfCodePoint(int), public java.util.Optional java.lang.String.describeConstable(), private static java.lang.String java.lang.String.lambda$stripIndent$3(int,java.lang.String), private static java.lang.String java.lang.String.lambda$indent$2(int,java.lang.String), private static java.lang.String java.lang.String.lambda$indent$1(java.lang.String), private static java.lang.String java.lang.String.lambda$indent$0(java.lang.String,java.lang.String)]


Some other methods of Class

getDeclaredConstructors()This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. 

getDeclaredField(String)This method returns a Field object that reflects the specified field of the class or interface represented by this Class object. 

getDeclaredFields()This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

getDeclaredMethod(String, Class...)This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

getDeclaredMethods()This method returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

getDeclaringClass()If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared. 

getEnclosingClass()This method returns the immediately enclosing class of the underlying class.

getEnclosingConstructor()If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.

getEnclosingMethod()If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.

getEnumConstants()This method returns the elements of this enum class or null if this class object does not represent an enum type.

getField(String)his method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. 

getFields()This method returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

getGenericInterfaces()This method returns the Types representing the interfaces directly implemented by the class or interface represented by this Class object.

getGenericSuperclass()This method returns the Type representing the direct superclass of the entity (class, interface, primitive type, or void) represented by this Class object.

getInterfaces()This method returns the interfaces directly implemented by the class or interface represented by this Class object.

getMethod(String, Class...)This method returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

getModifiers()This method returns the Java language modifiers for this class or interface, encoded in an integer.

getModule()This method returns the module that this class or interface is a member of.

getName()This method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

getNestHost()This method returns the nest host of the nest to which the class or interface represented by this Class object belongs. 

getNestMembers()This method returns an array containing Class objects representing all the classes and interfaces that are members of the nest to which the class or interface represented by this Class object belongs.

getPackage()This method gets the package of this class.

getPackageName()This method returns the fully qualified package name.

getProtectionDomain()This method returns the ProtectionDomain of this class.

getRecordComponents()This method returns an array of RecordComponent objects representing all the record components of this record class, or null if this class is not a record class.

No comments:

Post a Comment