mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 12:09:14 +00:00
8345953: JEP 492: instantiating local classes in a different static context should not be allowed Reviewed-by: vromero
102 lines
2.3 KiB
Java
102 lines
2.3 KiB
Java
/*
|
|
* @test /nodynamiccopyright/
|
|
* @bug 8322882 8345953
|
|
* @summary Disallow attempts to access a free variable proxy field from a static method
|
|
* @compile/fail/ref=LocalFreeVarStaticInstantiate.out -XDrawDiagnostics LocalFreeVarStaticInstantiate.java
|
|
*/
|
|
|
|
class LocalFreeVarStaticInstantiate {
|
|
|
|
// local class in method
|
|
static void foo(Object there) {
|
|
class Local {
|
|
{
|
|
there.hashCode();
|
|
}
|
|
|
|
static {
|
|
new Local(); // can't get there from here
|
|
}
|
|
|
|
static Runnable r = () -> {
|
|
new Local(); // can't get there from here
|
|
};
|
|
}
|
|
}
|
|
|
|
// local class in lambda
|
|
static Runnable foo = () -> {
|
|
Object there = "";
|
|
class Local {
|
|
{
|
|
there.hashCode();
|
|
}
|
|
|
|
static {
|
|
new Local(); // can't get there from here
|
|
}
|
|
|
|
static Runnable r = () -> {
|
|
new Local(); // can't get there from here
|
|
};
|
|
}
|
|
};
|
|
|
|
// local class in switch
|
|
static Object bar = switch (foo) {
|
|
case Runnable r -> {
|
|
Object there = "";
|
|
class Local {
|
|
{
|
|
there.hashCode();
|
|
}
|
|
|
|
static {
|
|
new Local(); // can't get there from here
|
|
}
|
|
|
|
static Runnable r = () -> {
|
|
new Local(); // can't get there from here
|
|
};
|
|
}
|
|
yield r;
|
|
}
|
|
};
|
|
|
|
// local class in instance init
|
|
{
|
|
Object there = "";
|
|
class Local {
|
|
{
|
|
there.hashCode();
|
|
}
|
|
|
|
static {
|
|
new Local(); // can't get there from here
|
|
}
|
|
|
|
static Runnable r = () -> {
|
|
new Local(); // can't get there from here
|
|
};
|
|
}
|
|
}
|
|
|
|
// local class in static init
|
|
static {
|
|
Object there = "";
|
|
class Local {
|
|
{
|
|
there.hashCode();
|
|
}
|
|
|
|
static {
|
|
new Local(); // can't get there from here
|
|
}
|
|
|
|
static Runnable r = () -> {
|
|
new Local(); // can't get there from here
|
|
};
|
|
}
|
|
}
|
|
}
|