package com.metarnet.model;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 注释测试
* @author yanfan
* 不使用已指定的数据类型,而使用注释来标注其数据类型
* 其中有四个类:MainTest(主函数,用于测试)
* TestNote(MODEL,用于存放数据)
* NoteEnum(定义注释中的属性可以存放的值)
* Note(注释)
* 使用@Note(Name=NoteEnum.String)来标注一个数据
*/
public class MainTest {
public static void main(String[] args) {
TestNote note = new TestNote();
try {
note.setTestInt(123);
note.setTestDate(new Date());
note.setTestString("Test");
//note.setTestString("Test");
System.out.println("TestDate:"+note.getTestDate());
System.out.println("TestInt:"+note.getTestInt());
System.out.println("TestString:"+note.getTestString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 在此声明三个参数,全部使用String类型,但使用注释标注它的类型
* @author yanfan
*
*/
class TestNote {
@Note(Name=NoteEnum.String)
protected String TestString;
@Note(Name=NoteEnum.Int)
protected String TestInt;
@Note(Name=NoteEnum.Date)
protected String testDate;
public Object getTestString() throws Exception {
return GET("TestString");
}
public void setTestString(Object testString) throws Exception{
SET("TestString", testString);
}
public Object getTestInt() throws Exception {
return GET("TestInt");
}
public void setTestInt(Object testInt) throws Exception{
SET("TestInt", testInt);
}
public Object getTestDate() throws Exception {
return GET("testDate");
}
public void setTestDate(Object testDate) throws Exception{
SET("testDate", testDate);
}
private void SET(String Name,Object value) throws Exception
{
Field field = TestNote.class.getDeclaredField(Name);
if(field.getAnnotation(Note.class).Name() == NoteEnum.Date)
{
field.set(this,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value));
}else if(field.getAnnotation(Note.class).Name() == NoteEnum.Int){
field.set(this,value.toString());
}else {
field.set(this,value);
}
}
private Object GET(String Name)throws Exception
{
Field field = TestNote.class.getDeclaredField(Name);
if(field.getAnnotation(Note.class).Name() == NoteEnum.Date)
{
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(field.get(this).toString());
}else if(field.getAnnotation(Note.class).Name() == NoteEnum.Int){
return Integer.parseInt(field.get(this).toString());
}else {
return field.get(this).toString();
}
}
}
/**
* 定义可以在注释中选择的参数
* @author yanfan
*
*/
enum NoteEnum {
String,
Date,
Int
}
/**
* 定义一个可以使用在参数上的注释
* @author yanfan
*
*/
/**
* CONSTRUCTOR : 构造器的声明
* FIELD : 域声明(包括enum实例)
* LOCAL_VARIABLE : 局部变量声明
* METHOD : 方法声明
* PACKAGE : 包声明
* PARAMETER : 参数声明
* TYPE : 类、接口 (包括注解类型) 或enum声明
*/
@Target(ElementType.FIELD)
/**
* SOURCE : 注释将被编译器丢掉
* CLASS : 注释在class文件中可用,但会被VM丢弃
* RUNTIME : VM将在运行时也保留注释,因此可以通过反射机制读取注释的信息。
*/
@Retention(RetentionPolicy.RUNTIME)
@interface Note {
public NoteEnum Name();//返回为枚举类型
}
(这是执行正确返回的值,可以明显的看出来其数据类型)
当我将测试类型设置为这个之后,
分享到:
相关推荐
数据描述符(descriptor)和存取描述符均具有以下可选键值:当且仅当该属性的 conBgurable 为 true 时,该属性描述符才能够被改变,同时该属
自己造轮子:一款实用的Android广告栏实现过程2016-01-23 安卓应用频道 安卓应用频道(点击上方公众号,可快速关注)来源:dongjunkun链接:
很多时候,都听人家在说不要重复制造轮子,要站在巨人的肩膀上等....不过让我感到有点困惑的是,怎么样才叫做不要重复制造轮子?如何才能站在巨人的肩旁上?现在网络如此发达,资源如此丰富,开源社区也发展的很好。...
NULL 博文链接:https://basil1292.iteye.com/blog/459911
不要再重复造轮子了,Hutool这款开源工具类库贼好使(csdn)————程序
重复造轮子。 笔记 谷歌测试文档:见 致谢 所有版权声明移至此处: // Copyright 2006, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // ...
CountDownTimerForTableView学习自,重复造轮子来练习
通过这个程序,你可以轻松地与讯飞星火API建立连接,并实现与模型的交互。无论是简单的问答还是复杂的问题,它都能准确理解并给出满意的答案。而且,它还具备联系上下文的能力,能够更好地理解问题的语境,从而提供...
1、App/Common/Conf/db.php 数据库连接参数修改; 2、App/Api/Conf/config.php 微信小程序的appid、secret、mchid、key、notify_url,SELF_ROOT的参数修改; 3、ThinkPHP\Library\Vendor\wxpay\lib\WxPay.... ...
在根据SGI-STL仿造轮在的过程中,学习c++和数据结构
winform通用开发框架 这个主要就是为了一般管理系统重复避免造轮子,不需要在关注权限 页面等,新增功能只需要新增form界面并完成自己的业务,在系统配置即可。 个人主页有基于该框架开发的简易进销存软件和喷码软件
Unity套件是Unity3D开发的工具包集合,集成常见的开发组件以免于重复造轮子。 套件设计初衷则是根据业务需求自由组合搭配其中的组件,项目在任何阶段都可以轻松接收。 由多个部分组成的套件,例如:基础组件,...
# 【Unity造轮子】实现一个类csgo的武器轮盘功能 # 作者信息 姓名:向宇 博客:[https://xiangyu.blog.csdn.net/](https://xiangyu.blog.csdn.net/) # 文章说明: ...
天天造轮子,手写面试常见源码.zip
尝试实现下几种常用Blog API,重复造轮子,是为了锻炼Java编程技巧。 目前支持: getUsersBlogs getUserInfo newPost getPost editPost getRecentPosts deletePost getUsersBlogs getCategories NewMediaObject new...
快速入门原型 此存储库包含常用的 Maven 原型模板。 安装: 查看模板 从命令行转到项目根文件夹。 例如:D:/workspace/muzongyan-quickstart-archetypes/quickstart-springmvc-mybatis 执行以下 maven 命令来安装 ...
WAF 绕过-权限控制之代码混淆及行为造轮子#Safedog 代码层手写及脚本绕过变量覆盖,加密混淆,异或生成#BT Aliyun 代码层手写及脚本绕过编码解码
【unity造轮子】2d敌人AI的实现,并自动躲避障碍物
java实现excel转pdf文件,并且支持excel单元格中带有图片的转换 基于 aspose,Excel转为 pdf工具类 Java Excel转为PDF(aspose)工具类 复造轮子 复制即用