先看代码:
package com.reflect; import java.lang.reflect.Method; import junit.framework.TestCase; import static java.lang.System.out; public class TestSynthetic extends TestCase { public void testSynthetic() { try { Method[] methods = User.class.getDeclaredMethods(); for (Method method : methods) { out.println(method.toString() + ", " + method.isSynthetic()); } } catch (SecurityException e) { e.printStackTrace(); } } class User { private int age; private String name; private User() { } private User(int age, String name) { this.age = age; this.name = name; } private int getAge() { return age; } private void setAge(int age) { this.age = age; } private String getName() { return name; } private void setName(String name) { this.name = name; } } }
private java.lang.String com.reflect.TestSynthetic$User.getName(), false private void com.reflect.TestSynthetic$User.setName(java.lang.String), false private int com.reflect.TestSynthetic$User.getAge(), false private void com.reflect.TestSynthetic$User.setAge(int), false
package com.reflect; import java.lang.reflect.Method; import junit.framework.TestCase; import static java.lang.System.out; public class TestSynthetic extends TestCase { public void testSynthetic() { try { User user = new User(); user.age = 1; Method[] methods = User.class.getDeclaredMethods(); for (Method method : methods) { out.println(method.toString() + ", " + method.isSynthetic()); } } catch (SecurityException e) { e.printStackTrace(); } } class User { private int age; private String name; private User() { } private User(int age, String name) { this.age = age; this.name = name; } private int getAge() { return age; } private void setAge(int age) { this.age = age; } private String getName() { return name; } private void setName(String name) { this.name = name; } } }
private java.lang.String com.reflect.TestSynthetic$User.getName(), false private void com.reflect.TestSynthetic$User.setName(java.lang.String), false static void com.reflect.TestSynthetic$User.access$1(com.reflect.TestSynthetic$User,int), true private int com.reflect.TestSynthetic$User.getAge(), false private void com.reflect.TestSynthetic$User.setAge(int), false
static void com.reflect.TestSynthetic$User.access$1(com.reflect.TestSynthetic$User,int), true
上面只是修改了测试代码,并没有修改User这个内部类,就可以增加User的方法。