The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Overview

## Advanced Language Topics

> The Java platform provides the JNI and reflection API to provide a way to invoke native methods and to get information on loaded classes.

### Java Native Interface

> The ability to interface to code written in other programming languages has been with the Java programming language since its inception. However, for JDK 1.1 the API was completely rewritten and ceremoniously renamed the JNI.

> The JNI defines a standard naming and calling convention so the Java VM can locate and invoke native methods. JNI also offers a set of standard interface functions to call from native code to do such things as access, manipulate, release, or create objects or to call methods in the Java programming language. Finally, the JNI supports an invocation to load, initialize, and invoke the Java VM. In JDK 1.2 a number of new methods add functionality in the areas of library and version management, local reference management, weak global references, array operations, string operations, reflection support, and the invocation API.

### Reflection

> Introduced in JDK 1.1, reflection lets a program written in the Java programming language access information about the fields, methods, and constructors of loaded classes and use reflected field, method, and constructor objects to operate within security restrictions on the fields, methods, and constructors in other objects. The reflection API accommodates applications 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.

> Some clients, such as the serialization service, development tools, and debuggers, need to bypass the default access controls built into the Java programming language when they use reflected members or constructors. These controls govern how method and constructor reflectives can access fields, invoke members, and create new class instances according to whether the field, method, or class is public, private, or protected.

> In JDK 1.2 reflected field, method and constructor objects extend a new base class (`AccessibleObject`) with a flag field that can be set to bypass the default access controls. Flag values for this field are either `True` or `False`, and the default flag value is `False`. If the flag is `True`, access checks are bypassed, and the requested operation proceeds. If the flag is `False`, normal access checks are in force.

> Setting the flag is under the control of the JDK 1.2 security architecture. In addition to the `AccessibleObject` instance, which has the necessary state and methods to set the flag to `True`, a `ReflectPermission` object is needed. The `ReflectPermission` object has methods to grant the necessary permission in the policy file to allow the reflective access.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form