在之前的博客中介绍过C#的Attribute(特性),简单的说,特性主要就是利用反射技术,在运行期获取关注类的相关标注信息,然后利用这些标注信息对关注的类进行处理,最近因为工作的原因,需要看一下Java,Java和C#其实是非常想象的两种语言,其实语言很多都相像,都在互相学习么,在Java中有注解这个名词,其实就是C#中的Attribute,原理和C#一样,利用反射技术获取运行时类的信息。

如果想要了解注解所表示的意思,那么必须提供一个能够解析这个注解的工具,这个应该不用做过多的解释,今天抽空,看了网上的例子,然后自己写了一个Java的实例,自己封装了一个注解处理类,当然不通用,话不u盾偶说了,直接上代码。相关的类都在下面
package com.liuyu.Annotation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.annotation.*; public class AnnotationUtil {
public static void getFruitInfo(Class<?> clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ String strFruitName=" 水果名称:";
String strFruitColor=" 水果颜色:";
String strFruitProvicer="供应商信息:"; Field[] fields = clazz.getDeclaredFields(); for(Field field :fields){
if(field.isAnnotationPresent(Fruite.class)){
Fruite fruitName = (Fruite) field.getAnnotation(Fruite.class);
strFruitName=strFruitName+fruitName.value();
System.out.println(strFruitName);
}
else if(field.isAnnotationPresent(Fruite.class)){
FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class); Method md= getGetMethod(clazz, field.getName()); Object s= md.invoke(clazz,fruitColor.fruitColor()); System.out.println(s.toString());
strFruitColor=strFruitColor+fruitColor.fruitColor().toString();
System.out.println(strFruitColor);
}
else if(field.isAnnotationPresent(Provider.class)){
Provider fruitProvider= (Provider) field.getAnnotation(Provider.class); Method mdSet= getSetMethod(clazz, field.getName());
Method mdGet= getGetMethod(clazz, field.getName()); Object sValue; try {
Object g= clazz.newInstance();
mdSet.invoke(g,new Object[]{fruitProvider.id()+fruitProvider.name()+fruitProvider.address()}); sValue= mdGet.invoke(g, new Object[]{}); System.out.println(sValue.toString());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} strFruitProvicer=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+fruitProvider.address(); System.out.println(strFruitProvicer);
}
} } @SuppressWarnings({ "rawtypes", "unchecked" })
public static Method getGetMethod(Class objectClass, String fieldName) { StringBuffer sb = new StringBuffer(); sb.append("get"); sb.append(fieldName.substring(0, 1).toUpperCase()); sb.append(fieldName.substring(1)); try { return objectClass.getMethod(sb.toString()); } catch (Exception e) { } return null; }
@SuppressWarnings("rawtypes")
public static Method getSetMethod( Class objectClass, String fieldName) { try { Class[] parameterTypes = new Class[1]; Field field = objectClass.getDeclaredField(fieldName); parameterTypes[0] = field.getType(); StringBuffer sb = new StringBuffer(); sb.append("set"); sb.append(fieldName.substring(0, 1).toUpperCase()); sb.append(fieldName.substring(1)); @SuppressWarnings("unchecked")
Method method = objectClass.getMethod(sb.toString(), parameterTypes); return method; } catch (Exception e) { e.printStackTrace(); } return null; } public static void invokeSet(Object o, String fieldName, Object value) { Method method = getSetMethod(o.getClass(), fieldName); try { method.invoke(o, new Object[] { value }); } catch (Exception e) { e.printStackTrace(); } } /** * 执行get方法 * * @param o执行对象 * @param fieldName属性 */ public static Object invokeGet(Object o, String fieldName) { Method method = getGetMethod(o.getClass(), fieldName); try { return method.invoke(o, new Object[0]); } catch (Exception e) { e.printStackTrace(); } return null; } }

package com.liuyu.Annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.*; @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Provider {
/**
* 供应商编号
* @return
*/
public int id() default -1; /**
* 供应商名称
* @return
*/
public String name() default ""; /**
* 供应商地址
* @return
*/
public String address() default "";
}
package com.liuyu.Annotation;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor { public enum Color{ BULE,RED,GREEN} Color fruitColor() default Color.GREEN;
package com.liuyu.main;

import com.liuyu.Annotation.*;
import com.liuyu.Annotation.FruitColor.Color; import java.lang.annotation.*; public class FruitTest { @Fruite("Apple")
private String appleName; @FruitColor(fruitColor=Color.RED)
private String appleColor; @Provider(id=1,name="陕西红富士集团",address="陕西省西安市延安路89号红富士大厦")
private String appleProvider; public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleColor() {
return appleColor;
} public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleName() {
return appleName;
} public void setAppleProvider(String appleProvider) {
this.appleProvider = appleProvider;
}
public String getAppleProvider() {
return appleProvider;
} public void displayName(){
System.out.println("水果的名字是:苹果");
}
}
package com.liuyu.main;

import java.lang.reflect.InvocationTargetException;

import com.liuyu.Annotation.*;

public class maintest {
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { AnnotationUtil.getFruitInfo(FruitTest.class); } }

以下是C#的一个例子,网上来的。可以和上面的比较下,很类似。

下面的代码定义了一个名字为widebright的自定义的属性,然后还有测试使用的例子,用到了“反射”了,其实我也接触不多“反射”这个东西,就是个提供运行时获取类结构等的支持的东西了,还可以用来动态加载库等,好像。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Reflection;
namespace WindowsFormsApplication1
{
//这里利用AttributeUsage 来设置我们的自定义属性的应用范围,这里定义的可以用于类,结构和方法的声明
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method,
AllowMultiple = true)] // multiuse attribute
class widebright : Attribute //从Attribute 继承,写一个自定义属性
{
private string strname;
public widebright(string name)
{
strname = name;
}
//添加一个允许外部访问的属性
public string Name
{
get { return strname; }
set { strname = Name; }
}
}
//写一个测试类
[widebright("widebright")]
class widebrightTestClass
{
private string name = "hahaa";
[widebright("test1")]
public void TestMethod()
{
System.Console.WriteLine("哈哈,第一个测试通过");
}
[widebright("test2")]
public void TestMethod2(string parm)
{
System.Console.WriteLine("哈哈,第二个测试通过,传过来的参数是:{0}", parm);
}
public void TestMethod3()
{
System.Console.WriteLine("哈哈,第三个测试,name的值为{0}", this.name);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
widebrightTestClass testClass = new widebrightTestClass();
//在程序运行时,获取自己添加进去的“自定义属性”
Type type = testClass.GetType();
testClass.TestMethod3 ();
//利用反射 运行时修改对象的私有属性
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Instance);
type.GetField ("name",flags ).SetValue (testClass,"widebright");
//再次调用方法,应该可以看到name私有属性确实被修改成widebright了
testClass.TestMethod3 ();
foreach (Attribute attr in type.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(widebright))
{
System.Console.WriteLine("testClass 对象具有 widebrihgt这个自定义属性");
} }
//测试 widebright”自定义属性“时候存在,获取type的所有方法
foreach (MethodInfo mInfo in type.GetMethods())
{ foreach (Attribute attr in
Attribute.GetCustomAttributes(mInfo))
{
// Check for the AnimalType attribute.
if (attr.GetType() == typeof(widebright))
{
Console.WriteLine(
" {0}方法有一个 名字为{1} 的\"widebright\" 属性.",
mInfo.Name, ((widebright)attr).Name );
if (((widebright)attr).Name == "test2" )
{
//动态调用testClass对象的方法
mInfo.Invoke(testClass, new string [] {((widebright)attr).Name});
}
else
{
//第一个方法没有参数,所以传个null给他
mInfo.Invoke (testClass, null);
}
}else{
Console.WriteLine(
" {0}方法不具有\"widebright\" 属性.",
mInfo.Name ); } }
}
} } 运行程序后将打印:
哈哈,第三个测试,name的值为hahaa
哈哈,第三个测试,name的值为widebright
testClass 对象具有 widebrihgt这个自定义属性
TestMethod方法有一个 名字为test1 的"widebright" 属性.
哈哈,第一个测试通过
TestMethod2方法有一个 名字为test2 的"widebright" 属性.
哈哈,第二个测试通过,传过来的参数是:test2
很有意思用法,呵呵, widebright手记

C#中的Attribute和Java中的Annotation的更多相关文章

  1. 在MySql中如何定义像Java中类型的Boolean类型

    在MySql中如何定义像Java中类型的Boolean类型数据..其实,mysql中 是没有直接定义成Boolean这种数据类型.它只能定义成 tinyint(1) ;如果长度是1,tinyint(1 ...

  2. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  3. [转]JAVA程序执行顺序,你了解了吗:JAVA中执行顺序,JAVA中赋值顺序

    本文主要介绍以下两块内容的执行顺序,熟悉的大虾可以直接飘过. 一.JAVA中执行顺序 静态块 块 构造器 父类构造器 二.JAVA中赋值顺序 静态块直接赋值 块直接赋值 父类继承的属性已赋值 静态变量 ...

  4. hadoop中Text类 与 java中String类的区别

    hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...

  5. OC中的@interface和java中的区别以及 @implementation @protocol

      java 在java中的interface是‘接口’的意思,而java的类声明用class,即接口用interface声明,类是用class声明,是两个独立的部分. 只有在类声明要实现某个接口时, ...

  6. Scala中集合类型与java中集合类型转换

    对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...

  7. 【Java】Java中的Collections类——Java中升级版的数据结构【转】

    一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...

  8. C++中如何实现像Java中接口功能--C++抽象类(纯虚函数,虚函数)

    在Java中定义个接口,之后可以定义不同的类来实现接口,如果有个函数的参数为这个接口的话,就可以对各自的类做出不同的响应. 如: interface animal { public void info ...

  9. JNI系列——C文件中的方法调用Java中方法

    1.创建xxx.jni包并在该包下实现一些Java的方法,和要调用的本地方法 2.实现MainActivity中的按钮点击事件-即点击按钮调用本地的方法 3.在C文件中的方法中回调Java的方法 3. ...

随机推荐

  1. Python脚本控制的WebDriver 常用操作 <十四> 处理button dropdown 的定位

    测试用例场景 模拟选择下拉菜单中数据的操作 Python脚本 测试用HTML代码: <html> <body> <form> <select name=&qu ...

  2. SVM入门

    前言: 又有很长的一段时间没有更新博客了,距离上次更新已经有两个月的时间了.其中一个很大的原因是,不知道写什么好-_-,最近一段时间看了看关于SVM(Support Vector Machine)的文 ...

  3. CentOS安装thrift

    下载thrift依赖的东西  yum -y install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-dev ...

  4. 【转载】MySQL 日志 undo | redo

    本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版 ...

  5. javac。java版本切换

    如果安装有多个Java版本时(有时候有些软件自行安装),怎样方便的进行切换呢.除了常见的设置环境变量外,今天学到了一种新的切换方法: update-alternatives --config java ...

  6. iOS 10的23个隐藏新特性-b

    上周iOS 10正式版推送后,24小时的更新率已经超过15%,实在惊人.虽然有着初期变砖.5S6卡顿.移动VoLTE无法使用.美版无信号等BUG,但不可忽视的是,iOS 10还是带来了很多从前没有的功 ...

  7. 原生js实现仿window10系统日历效果

    舞动的灵魂版js日历,完全采用js实现,故而实现了与语言无关,jsp.asp.net php asp均可使用.无论你是开发软件,还是网站,均是不可或缺的实用代码. 该日历主要实现了获取当前时间时分秒, ...

  8. SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束。

    SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束.

  9. 出现错误:Unable to load configuration. - action - file:/E:/Java/Tomcat7.0/apache-tomcat-7.0.68-windows-x64/apache-tomcat-7.0.68/webapps/SSH2Integrate/WEB-INF/classes/struts.xml:8:43

    严重: Exception starting filter struts2 Unable to load configuration. - action - file:/E:/Java/Tomcat7 ...

  10. Recommender Systems移动互联网个性化游戏推荐

    对于在线商店,主要关心两方面:1. 提升转化率(将不消费的用户转变为消费用户):2. 提升消费额(已经花钱的人,花更多的强). 对比了6种方法:1. 协同过滤:2. slope one:3. 基于内容 ...