Type Information

The need for RTTI

  • Because it is a dynamically bound method, the proper behavior will occur even though it is called through a generic reference.
  • That’s what RTTI means: At run time, the type of an object is identified.
  • You want the bulk of your code to know as little as possible about specific types of objects, and to just deal with the general representation of a family of objects.
  • With RTTI, you can ask a reference the exact type that it’s referring to, and thus select and isolate special cases.

The Class object

  • How type information is represented at run time? This is accomplished through a special kind of object called the Class object, which contains information about the class.
  • Java performs its RTTI using the Class object, even if you’re doing something like a cast.
  • Each time you write and compile a new class, a single Class object is also created.
  • This happens when the program makes the first reference to a static member of that class.
  • The constructor is also a static method of a class. Therefore, creating a new object of that class using the new operator also counts as a reference to a static member of the class.
  • A Java program isn’t completely loaded before it begins, but instead pieces of it are loaded when necessary.
  • Once the Class object for that type is in memory, it is used to create all objects of that type.
  • Each Class object is loaded only when it’s needed, and the static initialization is performed upon class loading.
  • Anytime you want to use type information at run time, you must first get a reference to the appropriate Class object.
  • With the Class object you can find out just about everything you want to know about a type.
  • You can discover an object’s entire class hierarchy at run time.
  • The newlnstance( ) method of Class is a way to implement a "virtual constructor".

Class Literals

  • There’s a standard field called TYPE that exists for each of the primitive wrapper classes. The TYPE field produces a reference to the Class object for the associated primitive type.
  • Creating a reference to a Class object using ".class" doesn’t automatically initialize the Class object.
  • Effectively, initialization is "as lazy as possible."
  • Class.forName( ) initializes the class immediately in order to produce the Class reference.

Generic class references

  • A Class reference really does indicate the exact type of what it’s pointing to: an object of the class Class.
  • To constrain the type of Class object that the Class reference is pointing to, using the generic syntax.
  • The ordinary class reference can be reassigned to any other Class object, whereas the generic class reference can only be assigned to its declared type.
  • Integer is inherited from Number. But the Integer Class object is not a subclass of the Number Class object.
  • The benefit of Class<?> is that it indicates that you aren’t just using a non-specific class reference by accident, or out of ignorance.
  • In order to create a Class reference that is constrained to a type or any subtype, you combine the wildcard with the extends keyword to create a bound.
  • The reason for adding the generic syntax to Class references is only to provide compile-time type checking.
  • In any event, because of the vagueness, the return value of up.newlnstance( ) is not a precise type, but just an Object.

New cast syntax

  • The cast( ) method takes the argument object and casts it to the type of the Class reference.
  • The new casting syntax is useful for situations where you can’t just use an ordinary cast.

Checking before a cast

  • It won’t allow you to perform a downcast assignment without using an explicit cast, to tell it that you have extra information that allows you to know that it is a particular type.
  • The keyword instanceof tells you if an object is an instance of a particular type.
  • It’s important to use instanceof before a downcast when you don’t have other information that tells you the type of the object.
  • There’s a rather narrow restriction on instanceof: You can compare it to a named type only, and not to a Class object.

A dynamic instanceof

  • The isInstance( ) method has eliminated the need for the instanceof expressions.

Registered factories

  • The only way to reuse the name Factory as seen above is by qualifying typeinfo.factory.Factory.

instanceof vs. Class equivalence

  • instanceof and islnstance( ) produce exactly the same results, as do equals( ) and ==.
  • If you compare the actual Class objects using ==, there is no concern with inheritance—it’s either the exact type or it isn’t.

Reflection: runtime class information

  • The type must be known at compile time in order for you to detect it using RTTI and to do something useful with the information. the compiler must know about all the classes you’re working with.
  • Suppose you’re given a reference to an object that’s not in your program space. In fact, the class of the object isn’t even available to your program at compile time.
  • This design-time configuration requires that any component be instantiable, that it exposes parts of itself, and that it allows its properties to be read and modified.
  • Reflection provides the mechanism to detect the available methods and produce the method names.
  • Another compelling motivation for discovering class information at run time is to provide the ability to create and execute objects on remote platforms, across a network.
  • Objects of these types are created by the JVM at run time to represent the corresponding member in the unknown class.
  • The class information for anonymous objects can be completely determined at run time, and nothing need be known at compile time.
  • The JVM will simply look at the object and see that it belongs to a particular class (just like ordinary RTTI). Before anything can be done with it, the Class object must be loaded.
  • The true difference between RTTI and reflection is that with RTTI, the compiler opens and examines the .class file at compile time.
  • With reflection, the .class file is unavailable at compile time.

A class method extractor

  • Reflection is in the language to support other Java features, such as object serialization and JavaBeans.
  • There are times when it’s quite useful to dynamically extract information about a class.
  • There is enough support to actually set up and make a method call on an object that’s totally unknown at compile time.

Dynamic proxies

  • A proxy typically acts as a go-between.
  • A proxy can be helpful anytime you’d like to separate extra operations into a different place than the "real object," and especially when you want to easily change from not using the extra operations to using them, and vice versa.
  • All calls made on a dynamic proxy are redirected to a single invocation handler, which has the job of discovering what the call is and deciding what to do about it.
  • The constructor for the invocation handler is usually given the reference to the "real" object so that it can forward requests once it performs its intermediary task.

Null Objects

  • It is useful to introduce the idea of a Null Object that will accept messages for the object that it’s "standing in" for, but will return values indicating that no "real" object is actually there.
  • The place where Null Objects seem to be most useful is "closer to the data," with objects that represent entities in the problem space.
  • In general, the Null Object will be a Singleton, so here it is created as a static final instance.
  • If you are working with interfaces instead of concrete classes, it’s possible to use a DynamicProxy to automatically create the Null Objects.

Mock Objects & Stubs

  • Both Mock Object and Stub pretend to be live objects that deliver real information, rather than being a more intelligent placeholder for null, as Null Object is.

Interfaces and type information

  • The fact that you always have a back door into a class may allow you to solve certain types of problems that could otherwise be difficult or impossible, and the benefits of reflection in general are undeniable.

Thinking in Java——笔记(14)的更多相关文章

  1. Java笔记14:泛型初探

    一.泛型简介 泛型是从Java SE 1.5开始出现的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛 ...

  2. java笔记14之private

    private:        1 是一个权限修饰符        2 可以修饰成员变量和成员方法        被其修饰的成员只能在本类中被访问 class Demo { //int num = 1 ...

  3. JAVA自学笔记14

    JAVA自学笔记14 1.正则表达式 1)是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.其实就是一种规则.有自己的特殊应用 2)组成规则: 规则字符在java.util.rege ...

  4. golang学习笔记14 golang substring 截取字符串

    golang学习笔记14 golang substring 截取字符串golang 没有java那样的substring函数,但支持直接根据 index 截取字符串mystr := "hel ...

  5. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  6. 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理

    1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...

  7. [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good

    [C++学习笔记14]动态创建对象   C#/Java中的反射机制 动态获取类型信息(方法与属性) 动态创建对象 动态调用对象的方法 动态操作对象的属性 前提:需要给每个类添加元数据 动态创建对象 实 ...

  8. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  9. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:


  1. :only-child

    如果某个元素是父元素中唯一的子元素,那将会被匹配 如果父元素中含有其他元素,那将不会被匹配.(注:这里的其他元素并不包含文本节点,如:<p><img/>图片</p> ...

  2. LeetCode----263. Ugly Number(Java)

    package isUgly263; /* * Write a program to check whether a given number is an ugly number. Ugly numb ...

  3. 如何学习java ee

    来看看Sun给出的J2EE 相关技术主要分为几大块. 1. Web Service技术 -  Java API for XML Processing (JAXP) -  Java API for XM ...

  4. uC/OS-II中的中断(转)

    中断是指在程序运行过程中,应内部或外部异步事件的请求中止当前任务,而去处理异步事件所要求的任务的过程. 中断服务函数(ISR)是应中断请求而运行的程序. 中断向量就是中断服务函数(ISR)的入口地址, ...

  5. C# WinForm多线程(一)Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...

  6. 最受Java开发者青睐的Java应用服务器 —— Tomcat

    Tomcat 是一个小型的轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试 JSP 程序的首选.今天,就一起来了解下 Tomcat. Java 应用服务器 Tomc ...

  7. BeautifulSoup详解

    BeautifulSoup BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XM ...

  8. PHP秒杀系统 高并发高性能的极致挑战(完整版)

    需要的联系我,QQ:1844912514 4-1 商品页面开发--服务端代码 8-8 提高数据处理速度-代码改造(一)

  9. JDK安装路径下的JRE与独立安装的JRE区别

    在JDK安装目录下的子文件下,已经默认安装了一个jre.且与独立安装的JRE6所包含的文件几乎完全一样. JDK里面内置的JRE和独立的JRE是有一点差别的: 在JDK安装文件中包含了一个完整的独立版 ...

  10. 一道面试题(C语言)

    题:输入一个数,列出所有加和等于该数的式子. 分析: 以 6 为例: 从上面的分析就比较容易找到规律了. C语言代码: #include <stdio.h> int main() { in ...