-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Remove JUnit5
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: theory | ||
custom_name: Accessing PSI Elements | ||
files: | ||
- name: src/jetbrains/plugindev/course/access/Main.kt | ||
visible: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Accessing PSI Elements | ||
|
||
--- | ||
[com.intellij.psi.util.PsiTreeUtil](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/psi/util/PsiTreeUtil.java) | ||
is a utility class in the IntelliJ Platform SDK that provides methods for navigating and querying the PSI tree of a project. | ||
|
||
The [com.intellij.psi.util.PsiTreeUtil.findChildrenOfType](https://github.com/JetBrains/intellij-community/blob/30cfa651ac2b9c50163368b56ee87ce1944543ec/platform/core-api/src/com/intellij/psi/util/PsiTreeUtil.java#L197C64-L197C64) method is used to find all children of a specified type within a given PSI element. | ||
It's particularly useful when you need to locate all instances of a particular element type, such as classes, methods, or variables, within a file or a code block. | ||
|
||
**Syntax:** | ||
```java | ||
public static @Unmodifiable @NotNull <T extends PsiElement> Collection<T> findChildrenOfType(@Nullable PsiElement element, @NotNull Class<? extends T> aClass) | ||
``` | ||
|
||
**Parameters:** | ||
* **element**: The PSI element within which to search for children. This could be a PsiFile, a PsiClass, or any other PSI element. | ||
* **aClass**: The class type of the elements you are searching for. For example, PsiClass.class to find all classes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.jetbrains.academy.ij.plugin.course.annotations | ||
|
||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.JavaPsiFacade | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.PsiType | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.psi.util.TypeConversionUtil | ||
|
||
fun addAnnotations(psiFile: PsiFile){ | ||
val project = psiFile.project | ||
WriteCommandAction.runWriteCommandAction(project) { | ||
val psiMethods = PsiTreeUtil.findChildrenOfType(psiFile, PsiMethod::class.java) | ||
|
||
for (psiMethod in psiMethods) { | ||
if (shouldAddOverrideAnnotation(psiMethod)) { | ||
addOverrideAnnotation(psiMethod, project) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun addOverrideAnnotation(psiMethod: PsiMethod, project: Project) { | ||
val psiElementFactory = JavaPsiFacade.getElementFactory(project) | ||
val overrideAnnotation = psiElementFactory.createAnnotationFromText("@Override", null) | ||
psiMethod.modifierList.addBefore(overrideAnnotation, psiMethod.modifierList.firstChild) | ||
} | ||
|
||
private fun shouldAddOverrideAnnotation(psiMethod: PsiMethod): Boolean { | ||
// Logic to determine if this method overrides a method from its superclass | ||
val containingClass = psiMethod.containingClass ?: return false | ||
|
||
val superClasses = containingClass.supers | ||
val methodName = psiMethod.name | ||
val parameterTypes = psiMethod.parameterList.parameters.map { it.type } | ||
|
||
for (superClass in superClasses) { | ||
val superMethods = superClass.methods | ||
for (superMethod in superMethods) { | ||
if (superMethod.name == methodName | ||
&& superMethod.parameterList.parameters.map { it.type } == parameterTypes | ||
&& isReturnTypeCompatible(superMethod.returnType, psiMethod.returnType) | ||
) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
private fun isReturnTypeCompatible(superReturnType: PsiType?, subReturnType: PsiType?): Boolean { | ||
if (superReturnType == null || subReturnType == null) return false | ||
return TypeConversionUtil.isAssignable(superReturnType, subReturnType) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: edu | ||
custom_name: Psi Manipulation Advanced | ||
files: | ||
- name: src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt | ||
visible: true | ||
placeholders: | ||
- offset: 871 | ||
length: 247 | ||
placeholder_text: TODO() | ||
- name: test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt | ||
visible: false |