Java Constructor isSynthetic()方法及示例
java.lang.reflect.Constructor类 的 isSynthetic() 方法是用来返回布尔值true,如果这个构造器对象是一个合成构造,则该方法返回false,显示该构造器不是一个合成构造。正如我们所知,合成构造是由Java编译器为内部目的而创建的类、字段和方法。在构造函数的例子中,合成构造是由java编译器为内部使用而创建的构造函数。这个方法有助于识别这些构造。
语法
public boolean isSynthetic()
参数: 此方法不接受任何参数。
返回 :当且仅当这个可执行文件是《Java语言规范》所定义的合成结构时,该方法才会返回true。
下面的程序说明了isSynthetic()方法:
程序1 :
// Java program to illustrate isSynthetic() method
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
// create a class object
Class classObj = shape.class;
// get Constructor object
// array from class object
Constructor[] cons = classObj.getConstructors();
// check isSynthetic or not
boolean answer = cons[0].isSynthetic();
// print result
System.out.println("isSynthetic : " + answer);
}
@CustomAnnotation(createValues = "GFG")
public class shape {
@CustomAnnotation(createValues = "GFG")
public shape()
{
}
}
// create a custom annotation
public @interface CustomAnnotation {
public String createValues();
}
}
输出:
isSynthetic : false
程序2
// Java program to illustrate isSynthetic() method
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
// create a class object
Class classObj = String.class;
// get Constructor object
// array from class object
Constructor[] cons = classObj.getConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println("Constructor: "
+ cons[i]);
// get array of isSynthetic
boolean answer = cons[0].isSynthetic();
// print result
System.out.println("isSynthetic : " + answer);
}
}
}
输出:
Constructor: public java.lang.String(byte[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.lang.String) throws java.io.unsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuilder)
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuffer)
isSynthetic : false
Constructor: public java.lang.String(byte[])
isSynthetic : false
Constructor: public java.lang.String(int[], int, int)
isSynthetic : false
Constructor: public java.lang.String()
isSynthetic : false
Constructor: public java.lang.String(char[])
isSynthetic : false
Constructor: public java.lang.String(java.lang.String)
isSynthetic : false
Constructor: public java.lang.String(char[],int,in).String(char[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, int)
isSynthetic : false
参考资料: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#isSynthetic()