序文:
最近、同社はビジネスコンポーネント化プロセスを経ており、ルーティングの実装ではJavaの反射メカニズムを使用しています。使用されているので、私はそれを慎重に勉強して要約したいと思います。実際、以前のEventBus 2.xバージョン、レトロフィット、またはアーリービューアノテーションフレームワークであろうと、Javaの反射メカニズムを多かれ少なかれ使用しています。
Java反射メカニズムとは何ですか?
Java反射メカニズムは、実行状態で、どのクラスでも、このクラスのすべての特性と方法を知ることができるということです。任意のオブジェクトについては、動的に取得された任意の方法を呼び出し、オブジェクトメソッドを動的に呼び出すことがJava反射メカニズムと呼ばれます。
反射メカニズムはどのような機能を提供しますか?
Java反射メカニズムクラス:
java.lang.class; //クラスJava.lang.Reflect.Constructor; // constructorメソッドJava.lang.reflect.field; //クラスメンバー変数java.lang.reflt.method; //クラスメソッドjava.lang.reflect.modifier; //アクセス権限
Java反射メカニズムの実装:
1.)クラスオブジェクトを取得します
//最初の方法はオブジェクトGetClassメソッドパーソン= new person(); class <?> class1 = person.getClass(); // 2番目の方法はclass属性class1 = persons.class; try {// class classの静的メソッドを使用することです。 (classNotFoundException e){e.printstacktrace();}2.)クラスオブジェクトの概要情報を取得します
Boolean isprimitive = class1.isprimitive(); //それが基本タイプであるかどうかを判断しますboolean isarray = class1.isarray(); //コレクションクラスのboolean isannotation = class1.Isannotation(); //判断class1.isenum(); // enum class class boolean isanonymousclass = class1.isanonymousclass(); //匿名の内部クラスのboolean isannotationpresent = class1.isannotationpresent(deprecated.class);パッケージ名パスパッケージapackage = class1.getPackage(); // classパッケージ情報string simplename = class1.getsimplename(); // class class name int modifiers = class1.getmodifiers(); class1.getDeclaringClass(); //外部クラス
3.)クラスオブジェクトの属性、メソッド、コンストラクターなどを取得する
field [] allfields = class1.getDeclaredfields(); //クラスオブジェクトのすべての属性を取得field = class1.getfields(); // classオブジェクトのパブリック属性を取得{field agefield = class1.getDeclaredfield( "age"); class} catch(nosuchfieldexception e){e.printstacktrace();} methods = class1.getDeclaredMethods(); //クラスオブジェクトのすべての宣言方法[] allmethods = class1.getMethods(); Object class <? = class1.getDeclaredConstructor(new class [] {string.class}); //指定された宣言コンストラクターpublicConstructor = class1.getConstructor(new class [] {}); //指定された宣言宣言パブリックコンストラクター} catch(nosuchmethodexception e){nosuchmethodecation [] {e.printstrace(); class1.getAnnotations(); //クラスオブジェクトのすべての注釈を取得しますannotation annotation = class1.getAnnotation(deprecated.class); //クラスオブジェクトタイプの指定されたアノテーションを取得しますgenericsuperclass = class1.getgenericsuperclass();クラスオブジェクトのインターフェイス4.)クラスオブジェクトの動的生成
//クラスオブジェクトの最初のメソッドは、newInstance()メソッドを呼び出してオブジェクトを生成しますobj = class1.newinstance(); object [] {"lcj"});5.)動的に関数を呼び出します
{//新しいオブジェクトを生成する:newinstance()メソッドオブジェクトobj = class1.newinstance()を使用してください。 //オブジェクトが人のサブクラスであるかどうかを判断します。 //まず、メソッドに対応するメソッドメソッドメソッドメソッド= class1.getDeclaredMethod( "setage"、new class [] {int.class}); //指定された関数を呼び出して、パラメーターmethod.invoke(obj、28)を渡します。 method = class1.getDeclaredMethod( "getage"); Object result.Invoke(obj、new class [] {{});} catch(instantiationexception e){e.printstacktrace();} catch(illegalaccessexception e){e.printstacktrace();} catch(nosuchmethodexception e){e.printstacktrace() e.printstacktrace();} 6.)反射メカニズムを介してジェネリックタイプを取得します
たとえば、次の構造
//クラスクラスのパブリッククラスの人々<t> {} //人のクラス継承人クラスのパブリッククラスパーソン<t>人<trention <string> emberments personinterface <integer> {} // personinterface interface public interface personinterface <t> {} {}一般的なタイプを取得します
人<string> person = new person <>(); //最初の方法は、オブジェクトGetClassメソッドclass <?> class1 = person.getClass()を使用することです。型Genericsuperclass = class1.getGenericsuperclass(); // typetype [] interfaceTypes = class1.getGenericInterfaces(); //クラスオブジェクトのすべてのインターフェイスの型版を取得しますgetComponentType(GenericSuperClass); getComponentType(InterfaceTypes [0]);
GetComponentType実装
プライベートクラス<? type [] altualtypearguments =((parameterizedType)type).getactualTypearguments(); if(actualtypearguments!= null && eractytepearguments.length> 0){componentType =(class <?>)artualTypearguments [0]; }} else if(genericarraytypeのタイプインスタンス){//パラメーター化されたタイプまたはタイプ変数componenttype =(class <?>)((genericarraytype)タイプ).getGenericComponentType();}その{componentType =(componenttype;} returnty; 6.)反射メカニズムを介して注釈情報を取得します
ここでは、例としてメソッドの注釈情報を取得することに焦点を当てています
試してください{//まず、このメソッドに対応するメソッドオブジェクトを取得する必要があります方法method = class1.getDeclaredMethod( "jumptogoodsdetail"、new class [] {string.class、string.class}); annotation [] annotations1 = method.getannotations(); //すべてのメソッドアノテーション情報Annotation Annotation1 = method.getannotation(routeruri.class); //指定された注釈情報Typevariableを取得[] typevariables1 = method.gettypeparameters(); annotation [] [] parameterAnnotationSarray = method.getParameterAnnotations(); //すべてのパラメーターアノテーション情報class <?> [] parametertypes = method.getParameterTypes(); //すべてのパラメータータイプ[] GenericParametertypes = genericParameterTypes method.getReturnType(); //メソッドのreturnタイプを取得int modifiers = method.getModifiers(); //メソッドのアクセス権限を取得} catch(nosuchmethodexception e){e.printstacktrace();}反射メカニズムのアプリケーションシナリオ:
反射メカニズムの利点と短所:
利点:ランタイムタイプの判断、動的クラスの読み込み、および動的プロキシ使用リフレクション。
短所:パフォーマンスは問題です。反射は、一連の説明操作と同等です。 JVMに何をすべきかを通知すると、パフォーマンスは直接Javaコードよりもはるかに遅いです。
要約:
Javaの反射メカニズムは、日常のビジネス開発ではめったに使用されませんが、いくつかの基本的なフレームワークの構築に広く使用されています。今日、私はそれを簡単に要約して学びましたが、後でそれを補うために使用することはまだ多くの未知の知識があります。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。