outline
play

Outline Working with unknown objects Generic Objects CS1007: - PDF document

Outline Working with unknown objects Generic Objects CS1007: Object Oriented Design Java Beans and Programming in Java Lecture #15 Nov 17 Shlomo Hershkop Reading: 7.3-7.8 shlomo@cs.columbia.edu Last time Working with the


  1. Outline • Working with unknown objects • Generic Objects CS1007: Object Oriented Design • Java Beans and Programming in Java Lecture #15 Nov 17 Shlomo Hershkop • Reading: 7.3-7.8 shlomo@cs.columbia.edu Last time Working with the unknown • Working with objects • Generally when you have Object from some class, • Overview of types – you wrote it yourself, so have doc/source • Comparing types – Using standard library, have docs • .class object – Unknown class, have no idea how to: • Shallow copy • Instantiated • Deep copy • Construct • If you don’t know how to use, probably not a good • Combination idea to use ☺ 1

  2. Reflection Reflection • Ability of running program to find out about its • Class getSuperclass() objects and classes • Class[] getInterfaces() • Class object reveals • Package getPackage() – superclass • Field[] getDeclaredFields() – interfaces – package • Constructor[] getDeclaredConstructors() – names and types of fields • Method[] getDeclaredMethods() – names, parameter types, return types of methods – parameter types of constructors Enumerating Fields Enumerating Constructors for (Constructor c : cons) • Print the names of all static fields of the { Math class: Class[] params = cc.getParameterTypes(); System.out.print("Rectangle("); Field[] fields = boolean first = true; Math.class.getDeclaredFields(); for (Class p : params) { for (Field f : fields) if (first) first = false; else if (Modifier.isStatic(f. getModifiers ())) System.out.print(", "); System.out.print(p.getName()); System.out.println(f.getName()); } System.out.println(")"); } 2

  3. Output Getting a single method descriptor • Supply method name Rectangle() • Supply array of parameter types Rectangle(java.awt.Rectangle) • Example: Get Rectangle.contains(int, int): Rectangle(int, int, int, int) Method m = Rectangle.class.getDeclaredMethod( Rectangle(int, int) "contains", int.class, int.class); Rectangle(java.awt.Point, • Example: Get default Rectangle constructor: java.awt.Dimension) Constructor c = Rectangle(java.awt.Point) Rectangle.class.getDeclaredConstructor(); • getDeclaredMethod, getDeclaredConstructor are Rectangle(java.awt.Dimension) varargs methods Invoking a Method Inspecting Objects • Supply implicit parameter (null for static methods) • Can obtain object contents at runtime • Supply array of explicit parameter values • Useful for generic debugging tools • Wrap primitive types • Need to gain access to private fields • Unwrap primitive return value Class c = obj.getClass(); • Example: Call System.out.println("Hello, World") the hard Field f = c.getDeclaredField(name); way. f.setAccessible(true); Method m = • Throws exception if security manager disallows access PrintStream.class.getDeclaredMethod( • Access field value: "println", String.class); Object value = f.get(obj); m.invoke(System.out, "Hello, World!"); f.set(obj, value); • invoke is a varargs method • Use wrappers for primitive types 3

  4. Inspecting Objects Inspecting Array Elements • Example: Peek inside string tokenizer • Use static methods of Array class Ch7/code/reflect2/FieldTester.java • Output • Object value = Array.get(a, i); int currentPosition=0 Array.set(a, i, value); int newPosition=-1 int maxPosition=13 • int n = Array.getLength(a); java.lang.String str=Hello, World! java.lang.String delimiters=, • Construct new array: boolean retDelims=false boolean delimsChanged=false char maxDelimChar=, Object a = Array.newInstance(type, length); --- int currentPosition=5 . . . The cast problem Generic Types • A generic type has one or more type variables Iterator itr = person.Iterator() • Type variables are instantiated with class or interface types while(itr.hasnext()){ • Cannot use primitive types, e.g. no ArrayList<int> • When defining generic classes, use type variables in definition: Person P = (Person)itr.next(); public class ArrayList<E> … { public E get(int i) { . . . } } public E set(int i, E newValue) { . . . } . . . private E[] elementData; } • NOTE: If S a subtype of T, ArrayList<S> is not a subtype of ArrayList<T>. 4

  5. Generic types • Generic method = method with type parameter(s) • Advantages? public class Utils { public static <E> void fill(ArrayList<E> a, E value, int count) { for (int i = 0; i < count; i++) • Disadvantages? a.add(value); } } • A generic method in an ordinary (non-generic) class • Type parameters are inferred in call ArrayList<String> ids = new ArrayList<String>(); Utils.fill(ids, "default", 10); // calls Utils.<String>fill Type Bounds Type Bounds • Type variables can be constrained with type bounds • Overcome limitation with type bound: • Constraints can make a method more useful • The following method is limited: public static <E, F extends E> void append( ArrayList<E> a, ArrayList<F> b, int count) public static <E> void append(ArrayList<E> a, { ArrayList<E> b, int count) for (int i = 0; i < count && i < b.size(); i++) { a.add(b.get(i)); for (int i = 0; i < count && i < b.size(); i++) } a.add(b.get(i)); } • extends means "subtype", i.e. extends or implements • Can specify multiple bounds: • Cannot append an ArrayList<Rectangle> to an ArrayList<Shape> E extends Cloneable & Serializable 5

  6. Wildcards Wildcards • Wildcards restrict methods that can be called: • Definition of append never uses type F. Can simplify with wildcard: ArrayList<? Extends E>.set method has the form ? extends E add(? extends E newElement) public static <E> void append( • You cannot call this method! ArrayList<E> a, ArrayList<? extends E> b, int • No value matches ? extends E because ? is count) unknown { • Ok to call get: for (int i = 0; i < count && i < b.size(); i++) a.add(b.get(i)); ? extends E get(int i) } • Can assign return value to an element of type E • Typical example--start with Wildcards public static <E extends Comparable<E>> E getMax(ArrayList<E> a) • Wildcards can be bounded in opposite direction • ? super F matches any supertype of F { public static <F> void append( E max = a.get(0); ArrayList<? super F> a, ArrayList<F> b, int count) for (int i = 1; i < a.size(); i++) if (a.get(i).compareTo(max) > 0) max = { a.get(i); for (int i = 0; i < count && i < b.size(); i++) return max; a.add(b.get(i)); } } • Safe to call ArrayList<? super F>.add: • E extends Comparable<E> so that we can call boolean add(? super F newElement) compareTo • Can pass any element of type F (but not a supertype!) 6

  7. Advantage/disadvantage • Too restrictive--can't call with • Really good to move errors to compile ArrayList<GregorianCalendar> time and not run time • GregorianCalendar does not implement • How to be backwards compatible? Comparable<GregorianCalendar>, only Comparable<Calendar> • Wildcards to the rescue: public static <E extends Comparable<? super E>> E getMax(ArrayList<E> a) Erasure Limitations of Generics • Virtual machine does not know about generic types • Cannot replace type variables with • Type variables are erased--replaced by type bound or Object if unbounded • Ex. ArrayList<E> becomes primitive types public class ArrayList { • Cannot construct new objects of generic public Object get(int i) { . . . } public Object set(int i, Object newValue) { . . . } type . . . private Object[] elementData; a.add(new E()); // Error--would erase to new } • Ex. getmax becomes Object() public static Comparable getMax(ArrayList a) // E extends Comparable<? super E> erased to Comparable • Erasure necessary to interoperate with legacy (pre-JDK 5.0) code 7

  8. workaround Limits II • Use class literals • Cannot form arrays of parameterized types • Comparable<E>[] is illegal. Remedy: public static <E> void ArrayList<Comparable<E>> fillWithDefaults(ArrayList<E>, Class<? extends E> cl, int count) • Cannot reference type parameters in a static throws InstantiationException, context (static fields, methods, inner classes) IllegalAccessException • Cannot throw or catch generic types { for (int i = 0; i < count; i++) • Cannot have type clashes after erasure. Ex. a.add(cl.newInstance()); GregorianCalendar cannot implement } Comparable<GregorianCalendar> since it already implements Comparable<Calendar>, • Call as fillWithDefaults(a, Rectangle.class, count) and both erase to Comparable Beyond Objects Idea of Components • More functionality than a single class • Object represent a single concept (usually) • Reuse and customize in multiple contexts • Sometimes hard to reuse in complex • "Plug components together" to form applications behavior • Successful model: Visual Basic controls – calendar • Would like an idea of a Object, a few – graph object, which we can add some behavior – database – link to robot or instrument necessary to accomplish a specific task • Components composed into program inside builder environment • Target all users, not just programmers 8

  9. Introducing Java Beans • Java component model • Bean has – methods (just like classes) – properties – events 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend