C#中的Attribute和Java中的Annotation
在之前的博客中介绍过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的更多相关文章
- 在MySql中如何定义像Java中类型的Boolean类型
在MySql中如何定义像Java中类型的Boolean类型数据..其实,mysql中 是没有直接定义成Boolean这种数据类型.它只能定义成 tinyint(1) ;如果长度是1,tinyint(1 ...
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
- [转]JAVA程序执行顺序,你了解了吗:JAVA中执行顺序,JAVA中赋值顺序
本文主要介绍以下两块内容的执行顺序,熟悉的大虾可以直接飘过. 一.JAVA中执行顺序 静态块 块 构造器 父类构造器 二.JAVA中赋值顺序 静态块直接赋值 块直接赋值 父类继承的属性已赋值 静态变量 ...
- hadoop中Text类 与 java中String类的区别
hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...
- OC中的@interface和java中的区别以及 @implementation @protocol
java 在java中的interface是‘接口’的意思,而java的类声明用class,即接口用interface声明,类是用class声明,是两个独立的部分. 只有在类声明要实现某个接口时, ...
- Scala中集合类型与java中集合类型转换
对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...
- 【Java】Java中的Collections类——Java中升级版的数据结构【转】
一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...
- C++中如何实现像Java中接口功能--C++抽象类(纯虚函数,虚函数)
在Java中定义个接口,之后可以定义不同的类来实现接口,如果有个函数的参数为这个接口的话,就可以对各自的类做出不同的响应. 如: interface animal { public void info ...
- JNI系列——C文件中的方法调用Java中方法
1.创建xxx.jni包并在该包下实现一些Java的方法,和要调用的本地方法 2.实现MainActivity中的按钮点击事件-即点击按钮调用本地的方法 3.在C文件中的方法中回调Java的方法 3. ...
随机推荐
- Microsoft .NET Framework 3.5 for Windowns Server2012R2 GUI
图形化安装,需要安装盘,不需要网络连接
- 未能加载文件或程序集“Report.Basic”或它的某一个依赖项。试图加载格式不正确的程序
出现问题如下: 解决办法: 这是由于没有开启32位程序兼容模式 具体操作如下:找到对应的程序池--------高级设置-------修改“启用32位应用程序”状态修改为true
- MySQL 多实例数据库还原脚本-备份集与端口对应
版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon export]# vi recover.sh #!/bin/bash bakdir=/exp ...
- Andriod wifi 基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- java之redis篇(spring-data-redis整合)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 多线程 1-pthread 和NSThread
一.基本内容介绍: 进程: 正在运行的程序就叫进程 每个进程之间是相互独立的,每个进程均运行在其专用且受保护的内存空间内. 线程: 在程序内工作的基本执行单元(每个进程至 ...
- VS2010常用插件介绍
今天在写JS时,写到500多行时,感觉代码已经很难看了.想到C#代码都有折叠功能,是不是JS也有呢.在选项中找了一下,没有相关了的设置功能,于是就上网找.一找可就不得了,发现了好多好用的插件.都可以在 ...
- C++实现简单的内存池
多进程编程多用在并发服务器的编写上,当收到一个请求时,服务器新建一个进程处理请求,同时继续监听.为了提高响应速度,服务器采用进程池的方法,在初始化阶段创建一个进程池,池中有许多预创建的进程,当请求到达 ...
- 2433: [Noi2011]智能车比赛 - BZOJ
Description 新一届智能车大赛在JL大学开始啦!比赛赛道可以看作是由n个矩形区域拼接而成(如下图所示),每个矩形的边都平行于坐标轴,第i个矩形区域的左下角和右上角坐标分别为(xi,1,yi, ...
- 【POJ】【2987】Firing
网络流/最大权闭合子图 胡伯涛论文里有讲…… sigh……细节处理太伤心了,先是count和ans输出弄反了,改过来顺序时又忘了必须先算出来ans!要是不执行一下Dinic的话count就无意义了…… ...