Prof. Dr. A. Poetzsch-Heffter Mathias Weber, M.Sc.
University of Kaiserslautern Department of Computer Science Software Technology Group
Advanced Aspects of Object-Oriented Programming (SS 2014) Practice Sheet 5
Date of Issue: 20.05.14Deadline: 27.05.14 (before the lecture as PDF via E-Mail)
Exercise 1 Generics
a) Implement a generic static methodflattenhaving the following signature
public static <... > List <... > flatten ( List <... > l)
The method takes a list of lists of anything and flattens it by one level. Replace each ellipsis such that your implementation provides maximum re-usability without violating type correctness.
b) (optional)Java does not support the creation of generic arrays, i.e. new List<E>[], new List<String>[], new E[], where E is a declared type variable, are illegal expressions. In order to find out the reasons for it, assume that the statementList<String>[] stringLists = new List<String>[1];is legal.
Write a code snippet that leads to aClassCastExceptionat a cast, that has been inserted by the type erasure. Your code should be legal Java except for the given statement.Hint: Take advantage of the covariance of arrays and try to assign a list of a different type tostringLists[0].
Exercise 2 Featherweight Java
a) Use the rules given in Fig. 2 in the paper "Featherweight Java: A Minimal Core Calculus for Java and GJ" to check for correct typing of the following program.
class Person extends Object { String name ;
int age ;
Person ( String name , int age ) { super();
this. name = name ; this. age = age ; }
int getAge () { return this. age ; }
}
class Student extends Person { int studentId ;
Student ( String name , int age , int studentId ) { super( name , age );
this. studentId = studentId ; }
Person me () {
return ( Person )this; }
}
class Main extends Object { Main () {super(); } Person main () {
return new Person (" Berta " , new Student (" Bob " , 22 , 12345). getAge ());
} }
Check if the program is type correct according to the type rules of Featherweight Java. Do a detailed proof of the type correctness of theMainobject using the procedure shown in the lecture.
You may assume that the typesintandStringare built in and valid types in Featherweight Java with the same meaning and syntax as the corresponding types in Java.
b) Give a step-by-step reduction of the expression
new Person (" Berta " , new Student (" Bob " , 22 , 12345). getAge ());
as shown in the lecture.
c) Extend Featherweight Java with aletconstruct. The syntax should look like in the following example:
let x = e1 in e2
wheree1ande2are expressions and the variablexcan be used ine2. Give the needed typing rule(s) as well as the needed reduction rule(s).
Exercise 3 Aliasing
a) Give two examples fordynamic aliasing, one where aliasing is desired and one where aliasing has undesired effects.
b) Describe the relationship between capturing, leaking and aliasing.
c) Give a solution to the signers issue on slide 8 of chapter 4.