• Keine Ergebnisse gefunden

Exercise 2 Confined Types

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise 2 Confined Types"

Copied!
3
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

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 2015) Practice Sheet 6

Date of Issue: 26.05.15

Deadline: 02.06.15 (before the lecture as PDF via E-Mail)

Exercise 1 Immutability

Discuss for the following Java classes whether or not they are immutable with respect to the definitions of immutability given in the lecture.

a) public final class ImmList {

public static final ImmList EMPTY = new ImmList (0 , null);

private final int head ; private final ImmList tail ;

private ImmList (int head , ImmList tail ) { this. head = head ;

this. tail = tail ; }

public ImmList cons (int i) { return new ImmList (i , this);

}

public int head () { if( isEmpty ()) {

throw new UnsupportedOperationException (" head of empty list " );

}

return this. head ; }

public ImmList tail () { if( isEmpty ()) {

throw new UnsupportedOperationException (" tail of empty list " );

}

return this. tail ; }

public boolean isEmpty () { return this == EMPTY ; }

public int length () { ImmList it = this; int len = 0;

while(! it . isEmpty ()) { len += 1;

it = it . tail ();

}

return len ; }

}

(2)

b) public final class CompactString { private char[] data ;

private int start ; private int end ; private int hash = -1;

public CompactString ( String s) { this. data = s. toCharArray ();

this. start = 0;

this. end = s. length () - 1;

}

private CompactString (char[] data , int start , int end ) { this. data = data ;

this. start = start ; this. end = end ; }

public CompactString substring (int start , int end ) { if( start < 0 || this. start + end > this. end ) {

throw new IllegalArgumentException (" Out of bounds " );

}

return new CompactString (this. data , this. start + start , this. start + end );

}

public void compact () {

char[] newData = new char[ end - start + 1];

for(int i= start ; i <= end ; i ++) { newData [i - start ] = data [i ];

}

this. data = newData ; this. start = 0;

this. end = newData . length - 1;

this. hash = -1;

}

@Override

public int hashCode () { if(this. hash >= 0) {

return this. hash ; } else {

int newhash = 0;

for(int i= start ; i <= end ; i ++) { newhash += i ^ data [i ];

}

this. hash = newhash ; return newhash ; }

}

@Override

public boolean equals ( Object o) {

if(o == null || !( o instanceof CompactString )) { return false;

}

return this. toString (). equals ((( CompactString )o ). toString ());

}

@Override

public String toString () {

return new String ( data , start , end - start + 1);

} }

Exercise 2 Confined Types

Examine the code, available with this practice sheet on the web, with respect to confinedness. The classesProofTreeNodeIt undProofContainershould provide the externally visible interfaces and are thus not confined.

a) Examine the classConfinedList. Can we declare this class as confined? Can we modify the implementation to make it confined?

(3)

b) Examine the classProofTreeNodeand answer the same questions as above.

c) Examine the classPTNIteratorand answer the same questions as above.

d) Download and install the tool “kacheck” from the lecture homepage. This tool is able to check the confinedness of Java classes based on the bytecode of these classes.

Compile Java source files mentioned in this exercise using the java compiler. Afterwards run the command

kacheck -violationson the folder the class files are in.

Compare the output of the tool with the results you expected. Try to explain unexpected results.

Hint: The tool outputs violations for all classes of the Java runtime that are indirectly used. These outputs can be ignored.

Exercise 3 Alias Modes

Consider the classConfinedListfrom the exercise above. The array stored in the fielddatashould be part of the representation of aConfinedList-object, i.e. we annotate it withrep.

class ConfinedList <T > extends AbstractList <T > implements List <T >, RandomAccess , Cloneable , Serializable { ...

private /* rep */ transient Object [] data ; ...

}

a) Is it possible to annotate the remaining parts of the classConfinedList, such that it conforms to the programming discipline presented in the lecture (slide 4.27)? If not, what is the problem with the implementation?

b) Which properties can the programming discipline guarantee? Compare them with the guarantees given by confined types.

Referenzen

ÄHNLICHE DOKUMENTE

dukte des Weltmarktführers) und geben Sie dann Beispiele für Error, Fault und Failure

Wird diese Methode nur innerhalb der Klasse verwendet, sollte diese auch anstatt public als private definiert

So entfällt diese gemäss Art. 18 Abs. 1 E-DSG, wenn eine der folgen- den Voraussetzungen erfüllt ist: die betroffene Person verfügt bereits über die entsprechenden

Die Definition deutet an, daß die Projektentwicklung als eine komplexe Perspektive zu verstehen ist, die über einzelne Vorstellungen bezüglich Nutzungskonzept, Architektur,

Women, on the other hand, have been seen to take care of the household (domus) and were therefore more likely to be in the private sphere 6. According to this understanding,

public static final class Memento { private final int state;. private Memento(int state) { this.state

Bei der Gründung von gemischtwirtschaftlichen Un- ternehmen wurde in der früheren Rechtsprechung generell davon ausgegangen, dass die Auswahl des Kooperationspartners

Private governance is currently being evoked as a viable solution to many public policy goals. However, in some circumstances it has shown to produce more harm than