Java リフレクションの高度な
通常、リフレクションには最初の方法を使用します。まず、Class.forName(clssName)を見てみましょう:
注: 受信するclassNameはクラスのフルパスである必要があります。それ以外の場合は、エラーが発生します。 >java lang.ClassNotFoundException
が報告されます。
public static Class<?> forName(String className) throws ClassNotFoundException { return forName0(className, true, ClassLoader.getCallerClassLoader()); }
java.lang.ClassNotFoundException
。// Returns the invoker's class loader, or null if none. // NOTE: This must always be invoked when there is exactly one intervening // frame from the core libraries on the stack between this method's // invocation and the desired invoker. static ClassLoader getCallerClassLoader() { // NOTE use of more generic Reflection.getCallerClass() Class caller = Reflection.getCallerClass(3); // This can be null if the VM is requesting it if (caller == null) { return null; } // Circumvent security check since this is package-private return caller.getClassLoader0(); }
通过调用forName0方法,使用ClassLoader.getCallerClassLoader()
来加载ClassLoader
// Package-private to allow ClassLoader access native ClassLoader getClassLoader0();
在获取ClassLoader调用claser.getClassLoader0() 这是一个私有包,可以避免安全检查。
public static Class<?> forName(String className) throws ClassNotFoundException { return forName0(className, true, ClassLoader.getCallerClassLoader()); }
1、ClassLoader到底是个什么东西
我们反射通常使用第一种方式,首先来看看Class.forName(clssName):
注意:传入的className必须是类的全路径,否则会报错java.lang.ClassNotFoundException
。
// Returns the invoker's class loader, or null if none. // NOTE: This must always be invoked when there is exactly one intervening // frame from the core libraries on the stack between this method's // invocation and the desired invoker. static ClassLoader getCallerClassLoader() { // NOTE use of more generic Reflection.getCallerClass() Class caller = Reflection.getCallerClass(3); // This can be null if the VM is requesting it if (caller == null) { return null; } // Circumvent security check since this is package-private return caller.getClassLoader0(); }
通过调用forName0方法,使用ClassLoader.getCallerClassLoader()
forName0 メソッドを呼び出して、ClassLoader.getCallerClassLoader()
を使用してClassLoader をロードします
// Package-private to allow ClassLoader access native ClassLoader getClassLoader0();
claser.getClassLoader0() を呼び出して ClassLoader を取得します。これはセキュリティを回避できるプライベート パッケージです。小切手。
rrreee1. ClassLoader とは
通常、リフレクションには
最初の方法
注: 受信する className はクラスの
フル パスである必要があります。
java.lang.ClassNotFoundException
が報告されます。 🎜rrreee🎜 forName0 メソッドを呼び出して、ClassLoader.getCallerClassLoader()
を使用して🎜ClassLoader をロードします🎜🎜rrreee🎜 claser.getClassLoader0() を呼び出して ClassLoader を取得します。これはセキュリティを回避できるプライベート パッケージです。小切手。 🎜rrreee🎜1. ClassLoader とは何ですか? 🎜🎜 上記は Java の高度なリフレクションの内容です。詳しくは、PHP 中国語 Web サイト (www.php.cn) をご覧ください。 🎜🎜🎜🎜🎜

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の乱数ジェネレーターのガイド。ここでは、Java の関数について例を挙げて説明し、2 つの異なるジェネレーターについて例を挙げて説明します。

Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。
