private static final String[] ANIMAL_TYPES = new String[]{
"com.example.reflectdemo.base.Cat",
"com.example.reflectdemo.base.Elephant",
"com.example.reflectdemo.base.Dog"
};
public static void main(String... args){
List
public class TestBean {
private String id;
public TestBean(String id) throws IllegalArgumentException, NotImplementedException {
this.id = id;
}
public String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
}
public class ExecutableTest {
public static void main(String... args) throws Exception{
for (Constructor constructor : TestBean.class.getConstructors()){
System.out.println("getName: " + constructor.getName());
System.out.println();
System.out.println("getModifiers: " + Modifier.toString(constructor.getModifiers()));
System.out.println();
System.out.println("getTypeParameters:");
for (TypeVariable t : constructor.getTypeParameters()){
System.out.println("type var:" + t.getName());
}
System.out.println();
System.out.println("getParameterCount:" + constructor.getParameterCount());
System.out.println();
System.out.println("getParameterTypes:");
for (Class cls : constructor.getParameterTypes()){
System.out.println(cls.getName());
}
System.out.println();
System.out.println("getExceptionTypes:");
for (Class cls : constructor.getExceptionTypes()){
System.out.println(cls.getName());
}
}
}
}
输出结果为:
getName: com.example.reflectdemo.reflectbase.TestBean
getModifiers: public
getTypeParameters:
type var:T
type var:R
getParameterCount:1
getParameterTypes:
java.lang.String
getExceptionTypes:
java.lang.IllegalArgumentException
sun.reflect.generics.reflectiveObjects.NotImplementedException
class Base implements Callable {
@Override
public T call() throws Exception {
return null;
}
}
public final class BaseClassInfo extends Base implements Runnable, Serializable {
@Override
public void run() {
}
public static void main(String... args){
Class cls = BaseClassInfo.class;
System.out.println("getName:" + cls.getName());
System.out.println();
System.out.println("getCanonicalName:" + cls.getCanonicalName());
System.out.println();
System.out.println("getSimpleName:" + cls.getSimpleName());
System.out.println();
System.out.println("getSuperclass:" + cls.getSuperclass());
System.out.println();
System.out.println("getPackage:" + cls.getPackage());
System.out.println();
for (Class c : cls.getInterfaces()){
System.out.println("interface : " + c.getSimpleName());
}
System.out.println();
for (TypeVariable> typeVariable : cls.getTypeParameters()){
System.out.println("type var : " + typeVariable.getTypeName());
}
System.out.println();
System.out.println("getModifiers:" + Modifier.toString(cls.getModifiers()));
}
}
输出结果为:
getName:com.example.reflectdemo.classdetail.BaseClassInfo
getCanonicalName:com.example.reflectdemo.classdetail.BaseClassInfo
getSimpleName:BaseClassInfo
getSuperclass:class com.example.reflectdemo.classdetail.Base
getPackage:package com.example.reflectdemo.classdetail
interface : Runnable
interface : Serializable
type var : T
type var : R
getModifiers:public final
Class类型判断,实例如下:
public class ClassTypeTest {
public static void main(String... args){
Runnable runnable = new Runnable() {
@Override
public void run() {
printClassType(getClass());
}
};
System.out.println("匿名内部类");
runnable.run();
class M implements Runnable{
@Override
public void run() {
printClassType(getClass());
}
}
System.out.println("方法内部类");
new M().run();
System.out.println("内部类");
new ClassTypeTest().new T().run();
System.out.println("静态内部类");
new S().run();
System.out.println("枚举");
printClassType(EnumTest.class);
System.out.println("接口");
printClassType(Runnable.class);
System.out.println("数组");
printClassType(int[].class);
System.out.println("int");
printClassType(int.class);
System.out.println("注解");
printClassType(AnnTest.class);
}
class T implements Runnable{
@Override
public void run() {
printClassType(getClass());
}
}
static class S implements Runnable{
@Override
public void run() {
printClassType(getClass());
}
}
enum EnumTest{
A, B, C
}
@interface AnnTest{
}
private static void printClassType(Class cls){
System.out.println("Class:" + cls.getName());
System.out.println("isAnonymousClass:" + cls.isAnonymousClass());
System.out.println("isLocalClass:" + cls.isLocalClass());
System.out.println("isMemberClass:" + cls.isMemberClass());
System.out.println("isEnum:" + cls.isEnum());
System.out.println("isInterface:" + cls.isInterface());
System.out.println("isArray:" + cls.isArray());
System.out.println("isPrimitive:" + cls.isPrimitive());
System.out.println("isAnnotation:" + cls.isAnnotation());
if (cls.isEnum()){
System.out.println("getEnumConstants:");
for (Object o : cls.getEnumConstants()){
System.out.println(o);
}
}
if (cls.isArray()){
System.out.println("getComponentType:" + cls.getComponentType());
}
System.out.println();
}
}
public class InnerClassTest {
public static void main(String... args){
System.out.println("getClasses");
for (Class cls : InnerClassTest.class.getClasses()){
System.out.println(cls.getName());
}
}
public interface I{
}
public class A implements I{
}
public class B implements I{
}
}
public class TestBean {
private final Integer id;
private final String name;
public TestBean(Integer id, String name) throws IllegalArgumentException, NotImplementedException {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "TestBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public class ConstructorTest {
public static void main(String... args) throws Exception{
for (Constructor constructor : TestBean.class.getConstructors()){
TestBean bean = (TestBean) constructor.newInstance(1, "Test");
System.out.println("newInstance:" + bean);
}
}
}
输出结果为:
newInstance:TestBean{id=1, name='Test'}
2.3.4. 属性信息
对象属性是类型中最主要的信息之一,主要通过 Field 表示,首先通过相关方法获取 Field 实例,然后进行属性值操作。
所涉及的方法如下:
方法
含义
getFields
获取public字段
getField(String name)
获取特定public字段
getDeclaredFields
获取所有的的属性
getDeclaredField
获取特定字段
Field 继承自 AccessibleObject 实现 Member 接口,拥有 AccessibleObject、AnnotatedElement、Member 相关功能,其核心方法如下:
方法
含义
isEnumConstant
是否枚举常量
getType
获取类型
get
获取属性值
getBoolean
获取boolean值
getByte
获取byte值
getChar
获取chat值
getShort
获取short值
getInt
获取int值
getLong
获取long值
getFloat
获取float值
getDouble
获取double值
set
设置属性值
setBoolean
设置boolean值
setByte
设置byte值
setChar
设置char值
setShort
设置short值
setInt
设置int值
setLong
设置long值
setFloat
设置float值
setDouble
设置double值
实例如下:
public enum EnumTest {
A
}
public class FieldBean {
private EnumTest aEnum;
private String aString;
private boolean aBoolean;
private byte aByte;
private char aChar;
private short aShort;
private int anInt;
private long aLong;
private float aFloat;
private double aDouble;
}
public class FieldTest {
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
FieldBean fieldBean = new FieldBean();
Field aEnum = getByName("aEnum");
Field aString = getByName("aString");
Field aBoolean = getByName("aBoolean");
Field aByte = getByName("aByte");
Field aChar = getByName("aChar");
Field aShort = getByName("aShort");
Field anInt = getByName("anInt");
Field aLong = getByName("aLong");
Field aFloat = getByName("aFloat");
Field aDouble = getByName("aDouble");
aEnum.set(fieldBean, EnumTest.A);
System.out.println("isEnumConstant: " + aEnum.isEnumConstant());
System.out.println("set and get enum : " + aEnum.get(fieldBean));
aString.set(fieldBean, "Test");
System.out.println("set and get String : " + aString.get(fieldBean));
aBoolean.setBoolean(fieldBean, true);
System.out.println("set and get Boolean : " + aBoolean.getBoolean(fieldBean));
aByte.setByte(fieldBean, (byte) 1);
System.out.println("set and get Byte : " + aByte.getByte(fieldBean));
aChar.setChar(fieldBean, 'a');
System.out.println("set and get Char : " + aChar.getChar(fieldBean));
aShort.setShort(fieldBean, (short) 1);
System.out.println("set and get Short : " + aShort.getShort(fieldBean));
anInt.setInt(fieldBean, 1);
System.out.println("set and get Int : " + anInt.getInt(fieldBean));
aLong.setLong(fieldBean, 1L);
System.out.println("set and get Long : " + aLong.getLong(fieldBean));
aFloat.setFloat(fieldBean, 1f);
System.out.println("set and get Float : " + aLong.getFloat(fieldBean));
aDouble.setDouble(fieldBean, 1.1);
System.out.println("set and get Double : " + aLong.getDouble(fieldBean));
}
private static Field getByName(String name) throws NoSuchFieldException {
Field field = FieldBean.class.getDeclaredField(name);
field.setAccessible(true);
return field;
}
}
在数据库系统中存储过程是必不可少的利器,存储过程是预先编译好的为实现一个复杂功能的一段Sql语句集合。它的优点我就不多说了,说一下我碰到的问题吧。我在项目开发的过程中需要用存储过程来实现一个功能,其中涉及到判断一张表是否已经建立,没有建立就由存储过程来建立这张表。
CREATE OR REPLACE PROCEDURE TestProc
IS
fla
使用jsonp不能发起POST请求。
It is not possible to make a JSONP POST request.
JSONP works by creating a <script> tag that executes Javascript from a different domain; it is not pos
(一)Keepalived
(1)安装
# cd /usr/local/src
# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
# tar zxvf keepalived-1.2.15.tar.gz
# cd keepalived-1.2.15
# ./configure
# make &a