IntelliJ IDEA: Structural Search & Replace



Modern IDEs are very powerful tools that can help a developer in a variety of situations. But the downside of this power is that most of the features are in the shadows - few people suspect their presence.



A simple example of one such function

, , IDEA F2, ? , ? - , .



Structural Search & Replace (SSR). , .



. , :



  1. 3D- jMonkeyEngine, , - .
  2. plantuml-native-image, PlantUML GraalVM native-image.


, . ...





, - , . , ( , jMonkeyEngine): synchronized (. "Item 82 — Document thread safety" "11 Concurrency" Joshua Bloch "Effective Java").



, , , .. , , , (deal locks).



, synchronized :



:



class ClassA {
    public synchronized void someMethod() {
        // ...
    }
}


:



class ClassA {
    public void someMethod() {
        synchronized(this) {
            // ...
        }
    }
}


, . :



class ClassA {
    private final Object sync = new Object();
    public void someMethod() {
        synchronized(sync) {
            // ...
        }
    }
}


.



, ?



synchronized . . jMonkeyEngine, 117 , , . .



? IntelliJ IDEA: Structural Search & Replace.



Structural Search.



IDEA.



jMonkeyEngine, (Edit -> Find -> Search Structurally...) :







  1. ;
  2. .


, ?



, ( ), , - ( HTML).



, , . Existing Templates...:







:







, , . , .



, synchronized , .. by design. :



synchronized $type$ $method$ ($ptype$ $param$) {
    $statement$;
}


. , , .



, $. . $ (, synchronized), , . "", . -, , .



"" . , .



.., , , , - ( void), - . , .



, , ( ) . ?



, .



, , , , , .



.. $param$, , , . Add Filter Count.







, 0 . :







… . , $statement$.



, Find , 18 :







!



. Script Filter



? , :



synchronized($Obj$) {
    $statement$;
}


, , $Obj$ — ? , Object. Type Object. , . .. :







, ( !). , … , ( with type hierarchy ).



- , . .



, . , — Script. .



, Groovy, true false. , $. __context__ __log__.



. — Psi . :





? . $Obj$. Groovy: println. $Obj$ Script :



println(Obj)
return true






, , "". synchronized, , , println?



IDEA. : Help->Show Log in.... .. KDE, Show Log in Dolphin. . - , . :



2020-07-05 15:03:00,998 [14151177] INFO - STDOUT - PsiReferenceExpression:pending
2020-07-05 15:03:01,199 [14151378] INFO - STDOUT - PsiReferenceExpression:source
2020-07-05 15:03:01,216 [14151395] INFO - STDOUT - PsiThisExpression:this
2020-07-05 15:03:01,219 [14151398] INFO - STDOUT - PsiReferenceExpression:receiveObjectLock
2020-07-05 15:03:01,222 [14151401] INFO - STDOUT - PsiReferenceExpression:invoke
2020-07-05 15:03:01,226 [14151405] INFO - STDOUT - PsiReferenceExpression:chatServer
2020-07-05 15:03:01,231 [14151410] INFO - STDOUT - PsiReferenceExpression:obj
2020-07-05 15:03:01,236 [14151415] INFO - STDOUT - PsiReferenceExpression:sync
2020-07-05 15:03:01,242 [14151421] INFO - STDOUT - PsiReferenceExpression:image
2020-07-05 15:03:01,377 [14151556] INFO - STDOUT - PsiClassObjectAccessExpression:TerrainExecutorService.class
2020-07-05 15:03:01,409 [14151588] INFO - STDOUT - PsiReferenceExpression:byteBuffer
2020-07-05 15:03:01,429 [14151608] INFO - STDOUT - PsiReferenceExpression:lock
2020-07-05 15:03:01,432 [14151611] INFO - STDOUT - PsiReferenceExpression:eventQueue
2020-07-05 15:03:01,456 [14151635] INFO - STDOUT - PsiReferenceExpression:sensorData.valuesLock
2020-07-05 15:03:01,593 [14151772] INFO - STDOUT - PsiReferenceExpression:createdLock
2020-07-05 15:03:01,614 [14151793] INFO - STDOUT - PsiReferenceExpression:taskLock
2020-07-05 15:03:01,757 [14151936] INFO - STDOUT - PsiReferenceExpression:loaders
2020-07-05 15:03:01,765 [14151944] INFO - STDOUT - PsiReferenceExpression:threadLock


.., , Obj :



  • PsiThisExpression — this;
  • PsiClassObjectAccessExpression — Class (synchronized (TerrainExecutorService.class) {...});
  • PsiReferenceExpression — , .


. .. Obj PsiThisExpression PsiClassObjectAccessExpression, true.



PsiReferenceExpression? , . ?



, , — . .. Java JetBrains GitHub IDEA, . .



Psi. :



if (Obj instanceof com.intellij.psi.PsiThisExpression) return true
if (Obj instanceof com.intellij.psi.PsiClassObjectAccessExpression) return true
if (Obj instanceof com.intellij.psi.PsiReferenceExpression) {
    def var = Obj.advancedResolve(false).element
    if (var instanceof com.intellij.psi.PsiParameter) return true
    if (var instanceof com.intellij.psi.PsiLocalVariable) {
        return !(var.initializer instanceof com.intellij.psi.PsiNewExpression)
    }
    if (var instanceof com.intellij.psi.PsiField) {
        return !var.hasModifier(com.intellij.lang.jvm.JvmModifier.PRIVATE) &&
               !var.hasModifier(com.intellij.lang.jvm.JvmModifier.PROTECTED)
    }
}
return true


12 . , .. . , . , Psi .





, . , . , , IDE , - .



IDEA . , , , , . , . : Structural search inspection.



(File->Settings...->Editor->Inspections):







, . , . Ok . , Open object sync. - , , .



, IDEA , :







--!!! ! ! , . .idea/inspectionProfiles, .



Structural Replace



Structural Search, IDEA — Structural Replace

(Edit -> Find -> Replace Structurally...):







Search Structurally, . . , , , $. . , Groovy .



. :



classInitializationSupport.initializeAtRunTime(WindowPropertyGetter.class, AWT_SUPPORT);


:



classInitializationSupport.initializeAtRunTime("sun.awt.X11.WindowPropertyGetter", AWT_SUPPORT);


.. . .



. Replace Structurally. :



classInitializationSupport.initializeAtRunTime($Clazz$.class, AWT_SUPPORT)


$Clazz$ , .. .



:



classInitializationSupport.initializeAtRunTime("$FullClass$", AWT_SUPPORT)


- $FullClass$. :



Clazz.type.canonicalText


.. , $Clazz$, .



:







Find :







, ( Preview Replacement). - ( ) ( Replace All).



, Psi, , ?



Structural Replace as Intention



IDEA — Intentions. , , .. - .



, :



public static void main(String[] args) {
    List<Integer> list = Arrays.asList(0, 10, 20, 30);
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}


for Alt+Enter, . , for-each:



public static void main(String[] args) {
    List<Integer> list = Arrays.asList(0, 10, 20, 30);
    for (Integer integer : list) {
        System.out.println(integer);
    }
}


, ? , Structural Replace intention. , Structural Search. , , :











, :











! Intention!





, IntelliJ IDEA. , , , .



. . , , .



. . Java!




All Articles