Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Markus Reitz
Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik
Practice Sheet 8: Fortgeschrittene Aspekte objektorientierter Programmierung (SS 2008)
Date of Issue: 03. Juni 2008 Deadline: 09. Juni 2008 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Memory Management
i m p o r t j a v a . u t i l .∗; i m p o r t j a v a . i o .∗;
p u b l i c c l a s s MemoryProblem {
p r o t e c t e d S t r i n g g e t L a r g e S t r i n g ( ) { t r y {
b y t e[ ] b u f f e r=new b y t e[ 5 0 0 0 0 0 0 ] ; new Random ( ) . n e x t B y t e s ( b u f f e r ) ;
r e t u r n new S t r i n g ( b u f f e r , " ISO−8859−1 " ) ; }
c a t c h( U n s u p p o r t e d E n c o d i n g E x c e p t i o n ex ) { throw new E r r o r ( ex ) ;
} }
p u b l i c S t r i n g g e t F r a g m e n t O f L a r g e S t r i n g ( ) { r e t u r n g e t L a r g e S t r i n g ( ) . s u b s t r i n g ( 2 3 , 4 2 ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g s ) { L i s t<S t r i n g> r e s u l t=new A r r a y L i s t<S t r i n g>( ) ; f o r(i n t i=0 ; i<1 0 0 0 ; i++) {
S y s t e m . o u t . p r i n t ( " . " ) ;
r e s u l t . add (new MemoryProblem ( ) . g e t F r a g m e n t O f L a r g e S t r i n g ( ) ) ; }
S y s t e m . o u t . p r i n t l n ( r e s u l t . s i z e ( ) ) ; }
}
a) Why does the given code fragment produce ajava.lang.OutOfMemoryError?
b) Fix the fragment so it is executed as expected, i.e. the memory error does not occur and the list whose reference is stored in the variableresult is populated with the fragments according to the given source code.You are not allowed to alter the implementation ofgetLargeStringandgetFragmentOfLargeString. These methods have to be used by the reworked version, too.
c) What is the design rationale for the trouble-causing API method?
Exercise 2 Confined Types
Apache Ant(Another Neat Tool) is a build tool written in the Java programming language. The current version’s source code is available athttp://ant.apache.org. In this exercise, the packageorg.apache.tools.ant.taskdefs.email has to be analysed with respect to confinedness.
a) Which classes are potential candidates for confinedness and which classes can be definitely excluded from a confinedness analysis?
b) Pick two items out of the set of identified classes and check for violation or fulfillment of the criteria C1 - C8. If necessary, adapt the criteria for inner classes.