在之前的博客中介绍过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. SSL handshake failed: SSL 错误:在证书中检测到违规的密钥用法。

    问题:在WINDOWS中创建的SVN Server,在Linux client中无法连接.原因:WINDOWS中的证书无法被Linux正确识别,因此需要修改证书,以使双方都可以正确识别. 修改方法如下 ...

  2. GGS: Sybase to Oracle

    Step 1: Start the GGSCI on Source and Target Source Target Oracle GoldenGate Command Interpreter for ...

  3. stringlist

    #ifndef _STRINGLIST_HPP_#define _STRINGLIST_HPP_ #include "../global.hpp"#include <type ...

  4. SOS.dll(SOS 调试扩展)

      SecAnnotate.exe(.NET 安全批注器工具) SignTool.exe(签名工具) Sn.exe(强名称工具) SOS.dll(SOS 调试扩展)   SqlMetal.exe(代码 ...

  5. [转]教大家如何打造使用Tcpview(tcp查看器

    原文地址:教大家如何打造使用Tcpview(tcp查看器)作者:jybasenet3   一玩 VS 对战平台的同学有一次发现了一个可以踢人的方法,就是用 TcpView 把那个连 接关掉.后来VS ...

  6. jQuery 操作Cookie 存储 读取 删除等

    <script type="text/javascript" src="/UI.Web.CRM.Main/js/jquery-2.1.1.min.js"& ...

  7. 关于qt5在win7下发布 & 打包

    QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...

  8. java解析xml禁止校验dtd

    参考: http://shansun123.iteye.com/blog/1020425 http://blog.csdn.net/hailanzhijia/article/details/60049 ...

  9. CentOS6.5安装tomcat7

    把下载的apache-tomcat-7.0.19.tar.gz文件上传到服务器的根目录    #tar zxvf apache-tomcat-7.0.19.tar.gz    #mv apache-t ...

  10. android 开发上传图片遇到返回 FileNotFoundException

    目的:带有参数上传图片 环境:客户端开发 android studio  ,服务器端:visual studio 2012 (c# asp.net) ************************* ...