JAva反射---インスタンス

1685 ワード

//reflect
// 
			Class<AlertMessage> alertMessageClass = (Class<AlertMessage>) alertMessage.getClass();
// 
			Field[] fsArray = alertMessageClass.getDeclaredFields();
// 
			Method[] methods = alertMessageClass.getMethods();
			for (Field f : fsArray) {
                           // 
				f.setAccessible(true);
				Object val = f.get(alertMessage);
//output:   :msisdn  123
//				System. out .println( "1st : name:" + f.getName()+ "\t value = " +val);
				//get the type of this property
				String type = f.getType().toString(); 
//output:   :class java.lang.String
//				System.out.println("type : " + type);
				
				String typeName = f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1);
//				System.out.println(typeName);
				
				if(f.getType() == java.sql.Timestamp.class ){
					//if val is null,not to format it
					for (Method method : methods) {
						if (method.getName().startsWith( "get" + typeName)){
// get 
//				              System. out .print( "methodName:" +method.getName()+ "/t" );
// get 
//				              System. out .println( "value:" +method.invoke(alertMessage));
				              
				              if(val != null){
									val = sdf.format(method.invoke(alertMessage));
								}
				              
					}
				}
					
				}
				
				//if val is null, change to ""
				if(val == null){
					val = "";
				}