File: ConstructorsLock.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (47 lines) | stat: -rw-r--r-- 1,757 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import org.checkerframework.checker.lock.qual.*;

// Initializers and constructors are synchronized over 'this'
// but not over their class's fields
public @GuardedBy({}) class ConstructorsLock {

    static class MyClass {
        public Object field;
    }

    final MyClass unlocked = new MyClass();

    @GuardedBy("this") MyClass guardedThis = new MyClass();

    @GuardedBy("unlocked") MyClass guardedOther = new MyClass();

    static final MyClass unlockedStatic = new MyClass();

    @GuardedBy("unlockedStatic") MyClass nonstaticGuardedByStatic = new MyClass();
    // :: error: (expression.unparsable.type.invalid)
    static @GuardedBy("unlocked") MyClass staticGuardedByNonStatic = new MyClass();
    static @GuardedBy("unlockedStatic") MyClass staticGuardedByStatic = new MyClass();

    Object initializedObject1 = unlocked.field;
    Object initializedObject2 = guardedThis.field;
    // :: error: (lock.not.held)
    Object initializedObject3 = guardedOther.field;
    // :: error: (expression.unparsable.type.invalid)
    Object initializedObject4 = staticGuardedByNonStatic.field;
    // :: error: (lock.not.held)
    Object initializedObject5 = nonstaticGuardedByStatic.field;
    // :: error: (lock.not.held)
    Object initializedObject6 = staticGuardedByStatic.field;

    ConstructorsLock() {
        unlocked.field.toString();
        guardedThis.field.toString();
        // :: error: (lock.not.held)
        guardedOther.field.toString();
        // :: error: (expression.unparsable.type.invalid)
        staticGuardedByNonStatic.field.toString();
        // :: error: (lock.not.held)
        nonstaticGuardedByStatic.field.toString();
        // :: error: (lock.not.held)
        staticGuardedByStatic.field.toString();
    }
}