📜  Java中的方法类isSynthetic()方法

📅  最后修改于: 2022-05-13 01:54:47.282000             🧑  作者: Mango

Java中的方法类isSynthetic()方法

Java.lang.reflectMethod 类帮助我们获取类或接口上单个方法的信息。此类还提供对类方法的访问并在运行时调用它们。

Method 类的 isSynthetic() 方法:该函数检查 Method Object 是否是合成构造。如果方法是合成构造,则函数返回 true,否则返回 false。

合成构造:合成构造是Java编译器为内部目的而创建的类、字段和方法。

句法:

public boolean isSynthetic()

返回值:当且仅当 Method 是 JVM 指定的合成构造时,此方法才返回true 。否则返回false

下面的程序说明了 Method 类的 isSynthetic() 方法:

示例 1:

在下面的程序中,当 main 方法创建嵌套私有类 Demo 的对象并尝试访问私有变量名称“message”时。当它被编译时,它将创建一个合成方法。该合成方法的详细信息可以通过使用 isSynthetic() 方法获取该方法的对象来获得,如下面的程序所示。

// Java program to demonstrate isSynthetic()
// method of Method Class.
  
import java.lang.reflect.Method;
  
public class GFG {
  
    // create Demo class
    private static final class Demo {
        private String message = "A Computer Science"
                                 + " portal for geeks";
    }
  
    // create main method
    public static void main(String args[])
    {
  
        try {
            // create object of Demo class
            Demo obj = new Demo();
  
            // print message of nested demo class
            System.out.println("private Message"
                               + " variable of Demo class"
                               + obj.message);
  
            // get class object of demo class
            Class classobj = obj.getClass();
  
            // get list of declared method objects
            // of class object of Demo
            Method[] methods = classobj.getDeclaredMethods();
  
            // loop through method list
            for (Method method : methods) {
  
                // check method is Synthetic or not
                boolean isSynthetic = method.isSynthetic();
  
                // print result
                System.out.println(method
                                   + " method is Synthetic Method :"
                                   + isSynthetic);
            }
        }
        catch (Exception e) {
  
            // Print Exception if any Exception occurs
            e.printStackTrace();
        }
    }
}
输出:
private Message variable of Demo classA Computer Science portal for geeks
static java.lang.String GFG$Demo.access$100(GFG$Demo) method is Synthetic Method :true

示例 2:返回 BigInteger 类的所有 Synthetic 构造方法的程序。

说明: 在此方法中,首先创建 BigInteger 类对象。创建 BigInteger 类的类对象后,通过调用类 Object 的 getMethods() 创建一个方法对象列表。遍历方法列表并通过检查 Method is Synthetic 或不使用 isSynthetic() 来获取 Synthetic Method。最后打印合成方法名称。

// Java program to Demonstrate isSynthetic()
// method of Method Class.
  
import java.lang.reflect.Method;
import java.math.BigInteger;
  
public class GFG {
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // create class object for class BigInteger
            Class c = BigInteger.class;
  
            // get list of Method object
            Method[] methods = c.getMethods();
  
            System.out.println("Synthetic Methods"
                               + " of BigInteger Class are");
            // Loop through Methods list
            for (Method m : methods) {
  
                // check whether the method is
                // Synthetic Method or not
                if (m.isSynthetic()) {
                    // Print Method name
                    System.out.println("Method: "
                                       + m.getName());
                }
            }
        }
        catch (Exception e) {
            // print Exception is any Exception occurs
            e.printStackTrace();
        }
    }
}
输出:
Synthetic Methods of BigInteger Class are
Method: compareTo

参考:

  • https://docs.oracle.com/javase/8/docs/api/java Java
  • https://nagakishoresidde.wordpress.com/2015/10/18/synthetic-constructs-in-java/