Reflection все классы пакеты java

Discovering Class Members

There are two categories of methods provided in Class for accessing fields, methods, and constructors: methods which enumerate these members and methods which search for particular members. Also there are distinct methods for accessing members declared directly on the class versus methods which search the superinterfaces and superclasses for inherited members. The following tables provide a summary of all the member-locating methods and their characteristics.

Class Methods for Locating Fields

Class API List of members? Inherited members? Private members?
getDeclaredField() no no yes
getField() no yes no
getDeclaredFields() yes no yes
getFields() yes yes no

Class Methods for Locating Methods

Class API List of members? Inherited members? Private members?
getDeclaredMethod() no no yes
getMethod() no yes no
getDeclaredMethods() yes no yes
getMethods() yes yes no

Class Methods for Locating Constructors

Class API List of members? Inherited members? Private members?
getDeclaredConstructor() no N/A 1 yes
getConstructor() no N/A 1 no
getDeclaredConstructors() yes N/A 1 yes
getConstructors() yes N/A 1 no

1 Constructors are not inherited.

Given a class name and an indication of which members are of interest, the ClassSpy example uses the get*s() methods to determine the list of all public elements, including any which are inherited.

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Member; import static java.lang.System.out; enum ClassMember < CONSTRUCTOR, FIELD, METHOD, CLASS, ALL >public class ClassSpy < public static void main(String. args) < try < Classc = Class.forName(args[0]); out.format("Class:%n %s%n%n", c.getCanonicalName()); Package p = c.getPackage(); out.format("Package:%n %s%n%n", (p != null ? p.getName() : "-- No Package --")); for (int i = 1; i < args.length; i++) < switch (ClassMember.valueOf(args[i])) < case CONSTRUCTOR: printMembers(c.getConstructors(), "Constructor"); break; case FIELD: printMembers(c.getFields(), "Fields"); break; case METHOD: printMembers(c.getMethods(), "Methods"); break; case CLASS: printClasses(c); break; case ALL: printMembers(c.getConstructors(), "Constuctors"); printMembers(c.getFields(), "Fields"); printMembers(c.getMethods(), "Methods"); printClasses(c); break; default: assert false; >> // production code should handle these exceptions more gracefully > catch (ClassNotFoundException x) < x.printStackTrace(); >> private static void printMembers(Member[] mbrs, String s) < out.format("%s:%n", s); for (Member mbr : mbrs) < if (mbr instanceof Field) out.format(" %s%n", ((Field)mbr).toGenericString()); else if (mbr instanceof Constructor) out.format(" %s%n", ((Constructor)mbr).toGenericString()); else if (mbr instanceof Method) out.format(" %s%n", ((Method)mbr).toGenericString()); >if (mbrs.length == 0) out.format(" -- No %s --%n", s); out.format("%n"); > private static void printClasses(Class c) < out.format("Classes:%n"); Class[] clss = c.getClasses(); for (Class cls : clss) out.format(" %s%n", cls.getCanonicalName()); if (clss.length == 0) out.format(" -- No member interfaces, classes, or enums --%n"); out.format("%n"); > >

This example is relatively compact; however the printMembers() method is slightly awkward due to the fact that the java.lang.reflect.Member interface has existed since the earliest implementations of reflection and it could not be modified to include the more useful getGenericString() method when generics were introduced. The only alternatives are to test and cast as shown, replace this method with printConstructors() , printFields() , and printMethods() , or to be satisfied with the relatively spare results of Member.getName() .

Читайте также:  Создаем глобальную переменную php

Samples of the output and their interpretation follows. User input is in italics.

$ java ClassSpy java.lang.ClassCastException CONSTRUCTOR Class: java.lang.ClassCastException Package: java.lang Constructor: public java.lang.ClassCastException() public java.lang.ClassCastException(java.lang.String)

Since constructors are not inherited, the exception chaining mechanism constructors (those with a Throwable parameter) which are defined in the immediate super class RuntimeException and other super classes are not found.

$ java ClassSpy java.nio.channels.ReadableByteChannel METHOD Class: java.nio.channels.ReadableByteChannel Package: java.nio.channels Methods: public abstract int java.nio.channels.ReadableByteChannel.read (java.nio.ByteBuffer) throws java.io.IOException public abstract void java.nio.channels.Channel.close() throws java.io.IOException public abstract boolean java.nio.channels.Channel.isOpen()

The interface java.nio.channels.ReadableByteChannel defines read() . The remaining methods are inherited from a super interface. This code could easily be modified to list only those methods that are actually declared in the class by replacing get*s() with getDeclared*s() .

$ java ClassSpy ClassMember FIELD METHOD Class: ClassMember Package: -- No Package -- Fields: public static final ClassMember ClassMember.CONSTRUCTOR public static final ClassMember ClassMember.FIELD public static final ClassMember ClassMember.METHOD public static final ClassMember ClassMember.CLASS public static final ClassMember ClassMember.ALL Methods: public static ClassMember ClassMember.valueOf(java.lang.String) public static ClassMember[] ClassMember.values() public final int java.lang.Enum.hashCode() public final int java.lang.Enum.compareTo(E) public int java.lang.Enum.compareTo(java.lang.Object) public final java.lang.String java.lang.Enum.name() public final boolean java.lang.Enum.equals(java.lang.Object) public java.lang.String java.lang.Enum.toString() public static T java.lang.Enum.valueOf (java.lang.Class,java.lang.String) public final java.lang.Class java.lang.Enum.getDeclaringClass() public final int java.lang.Enum.ordinal() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final void java.lang.Object.wait() hrows java.lang.InterruptedException public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll()

In the fields portion of these results, enum constants are listed. While these are technically fields, it might be useful to distinguish them from other fields. This example could be modified to use java.lang.reflect.Field.isEnumConstant() for this purpose. The EnumSpy example in a later section of this trail, Examining Enums, contains a possible implementation.

In the methods section of the output, observe that the method name includes the name of the declaring class. Thus, the toString() method is implemented by Enum , not inherited from Object . The code could be amended to make this more obvious by using Field.getDeclaringClass() . The following fragment illustrates part of a potential solution.

Источник

Reflection все классы пакеты java

Боже, хоть одна всеняемая статья про Рефлекшн для новичков. А то, другие авторы сразу выввливают кучу методов, без объяснений для нубов. Спасибо, автор!

Грамотно написанный класс в Java: — У меня всё как надо, всё что нужно скрыть, помечено Private, я вообще не вызываю багов вовремя работы программы. 💪😎👍 Reflection API: — 😂 Я вас умоляю, не смешите мои подковы методы).

На собеседовании : — Reflection API, для чего он нужен? — Он позволяет наиболее циничный способом попрать принцип инкапсуляции в java.

Вызов метода мяу обычным способом : Кот, мяукни! Вызов метода мяу через рефлексию : Мяу, прозвучи от того кота!

 clazz = Class.forName(Cat.class.getName()); 

Вместо того, чтобы сразу получить объект класса, мы сначала получаем имя класса (которое нам известно), а потом по нему получаем объект класса. Или это как-то связано с полным именем (learn.javarush.Cat) ?

Я статью только начал читать, но уже появился вопрос, причём, видимо, элементарный: если класс Cat наследует класс Animal, то зачем в наследнике повторять те же поля private String name; private int age; Они же ему переходят по наследству? Вопрос второй: как понять эти строки Class clazz = Class.forName(className); Cat cat = (Cat) clazz.newInstance(); Если стоит задача создать объект класса, чьё имя на момент компиляции неизвестно, то откуда в тексте программы это самое неизвестное имя? Я бы мог представить такую строку: Object obj = clazz.newInstance(); Она была бы универсальной для любого типа. И она есть в этом же коде ниже, в методе createObject(), но он же не вызывается вроде. Вызывается createCat()

вернулся сюда из 34 уровня лекции 8 задачи 2, запомните как создать обж с конструктором который принимает аргумент.

 На этом наша лекция подошла к концу! Она получилась довольно большой, но сегодня ты узнал много нового :) 

И сегодня, и вчера, и позавчера. Смог прожевать только за три вечера. Не уверен, что прожевал тщательно, и не случится изжоги. 🤣

Источник

Package java.lang.reflect

Provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods, and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within encapsulation and security restrictions.

Classes in this package, along with java.lang.Class accommodate applications such as debuggers, interpreters, object inspectors, class browsers, and services such as Object Serialization and JavaBeans that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class.

AccessibleObject allows suppression of access checks if the necessary ReflectPermission is available.

Array provides static methods to dynamically create and access arrays.

Java programming language and JVM modeling in core reflection

The components of core reflection, which include types in this package as well as Class , Package , and Module , fundamentally present a JVM model of the entities in question rather than a Java programming language model. A Java compiler, such as javac , translates Java source code into executable output that can be run on a JVM, primarily class files. Compilers for source languages other than Java can and do target the JVM as well.

The translation process, including from Java language sources, to executable output for the JVM is not a one-to-one mapping. Structures present in the source language may have no representation in the output and structures not present in the source language may be present in the output. The latter are called synthetic structures. Synthetic structures can include methods, fields, parameters, classes and interfaces. One particular kind of synthetic method is a bridge method. It is possible a synthetic structure may not be marked as such. In particular, not all class file versions support marking a parameter as synthetic. A source language compiler generally has multiple ways to translate a source program into a class file representation. The translation may also depend on the version of the class file format being targeted as different class file versions have different capabilities and features. In some cases the modifiers present in the class file representation may differ from the modifiers on the originating element in the source language, including final on a parameter and protected , private , and static on classes and interfaces.

Besides differences in structural representation between the source language and the JVM representation, core reflection also exposes runtime specific information. For example, the class loaders and protection domains of a Class are runtime concepts without a direct analogue in source code.

Источник

Оцените статью