Files
openide/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/LocalClassInstantiation.java
Bas Leijdekkers 9d345bbb34 Java: report local class instantiation from a different static context (IDEA-372971)
(cherry picked from commit 6bc936ee3dd849db2db187b03edac58f4c9b2c73)


(cherry picked from commit 18853b7d75a40f6a8761489a5655602be915a1ba)

IJ-MR-169535

GitOrigin-RevId: 058ac27f8f0ef5c605c962e596711dafb1eeb5fc
2025-08-04 12:58:49 +00:00

94 lines
2.9 KiB
Java

class LocalInstantiation {
// local class in method
static void foo(Object there) {
class Local {
{
there.hashCode();
}
static {
//can only be instantiated from its own static context
//cannot be instantiated here
//not allowed to be instantiated heree
//cannot be instantiated outside its static context
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
}
static Runnable r = () -> {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
};
}
}
// local class in lambda
static Runnable foo = () -> {
Object there = "";
class Local {
{
there.hashCode();
}
static {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
}
static Runnable r = () -> {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
};
}
};
// local class in switch
static Object bar = switch (foo) {
case Runnable r -> {
Object there = "";
class Local {
{
there.hashCode();
}
static {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
}
static Runnable r = () -> {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
};
}
yield r;
}
};
// local class in instance init
{
Object there = "";
class Local {
{
there.hashCode();
}
static {
<error descr="'LocalInstantiation.this' cannot be referenced from a static context">new Local()</error>; // should be highlighted as an error
}
static Runnable r = () -> {
<error descr="'LocalInstantiation.this' cannot be referenced from a static context">new Local()</error>; // should be highlighted as an error
};
}
}
// local class in static init
static {
Object there = "";
class Local {
{
there.hashCode();
}
static {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
}
static Runnable r = () -> {
<error descr="Local class 'Local' cannot be instantiated from a different static context">new Local()</error>; // should be highlighted as an error
};
}
}
}