dk.lec.idea.plugin.generate
Class PsiUtil

java.lang.Object
  |
  +--dk.lec.idea.plugin.generate.PsiUtil

public class PsiUtil
extends java.lang.Object

Utility methods accessing the IDEA PSI API (Program Structure Information).


Method Summary
static void addImportStatement(com.intellij.psi.PsiJavaFile javaFile, java.lang.String importStatementOnDemand, com.intellij.psi.PsiElementFactory factory, com.intellij.psi.codeStyle.CodeStyleManager codeStyleManager)
          Add's an importstatement to the javafile and optimizes the imports afterwards.
static com.intellij.psi.PsiMethod findMethodByName(com.intellij.psi.PsiClass clazz, java.lang.String name)
          Find's an existing method with the given name.
static com.intellij.psi.codeStyle.CodeStyleManager getCodeStyleManager(com.intellij.openapi.project.Project project)
          Get's the CodeStyleManager for the project.
static com.intellij.psi.PsiClass getCurrentClass(com.intellij.psi.PsiJavaFile javaFile, com.intellij.openapi.editor.Editor editor)
          Gets the current class for the javafile and relative to the editor's caret.
static com.intellij.psi.PsiField[] getFields(com.intellij.psi.PsiClass clazz)
          Get's the fields for the class.
static java.util.Collection getFieldsAsCollection(com.intellij.psi.PsiClass clazz)
          Get's the fields for the class.
static com.intellij.openapi.project.Project getProject(com.intellij.openapi.actionSystem.DataContext dataContext)
          Get's the IDEA project.
static com.intellij.psi.PsiElementFactory getPsiElemetFactory(com.intellij.psi.PsiManager manager)
          Get's the PSIElementFactory.
static com.intellij.psi.PsiManager getPsiManager(com.intellij.openapi.project.Project project)
          Get's the PSIManager.
static com.intellij.psi.PsiJavaFile getSelectedJavaFile(com.intellij.openapi.actionSystem.DataContext context)
          Convenience method to get the selected Java file given a DataContext.
static com.intellij.psi.PsiJavaFile getSelectedJavaFile(com.intellij.openapi.project.Project project, com.intellij.psi.PsiManager manager)
          Get's the javafile that's currently selected in the editor.
static boolean hasFields(com.intellij.psi.PsiClass clazz)
          Does the class have any fields?
static boolean hasImportStatement(com.intellij.psi.PsiJavaFile javaFile, java.lang.String importStatement)
          Does the javafile have the importstatement.
static boolean isCollectionType(com.intellij.psi.PsiElementFactory factory, com.intellij.psi.PsiField field)
          Is the given field a Collection type?
static boolean isConstantField(com.intellij.psi.PsiField field)
          Returns true if a field is constant.
static boolean isMapTye(com.intellij.psi.PsiElementFactory factory, com.intellij.psi.PsiField field)
          Is the given field a Map type?
static boolean isModifierTransient(com.intellij.psi.PsiField field)
          Returns true if a field has a transient modifier.
static boolean isObjectArrayType(com.intellij.psi.PsiField field)
          Is the given field an Object array type (etc.
static boolean isPrimitiveArrayType(com.intellij.psi.PsiField field)
          Returns true if the given field a primtive array type (e.g., int[], long[], float[]).
static boolean isPrimitiveType(com.intellij.psi.PsiField field)
          Is this field a primitive type
static boolean isStringType(com.intellij.psi.PsiElementFactory factory, com.intellij.psi.PsiField field)
          Is the given field a String type?
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getProject

public static com.intellij.openapi.project.Project getProject(com.intellij.openapi.actionSystem.DataContext dataContext)
Get's the IDEA project.

Parameters:
dataContext - data context.
Returns:
IDEA project.

getPsiManager

public static com.intellij.psi.PsiManager getPsiManager(com.intellij.openapi.project.Project project)
Get's the PSIManager.

Parameters:
project - IDEA project.
Returns:
the PSIManager.

getSelectedJavaFile

public static com.intellij.psi.PsiJavaFile getSelectedJavaFile(com.intellij.openapi.actionSystem.DataContext context)
Convenience method to get the selected Java file given a DataContext. This form of the method is provided purely for convenience.

Parameters:
context - Data context
Returns:
Currently selected Java file or null if no file is selected or the selected file is not a Java file
See Also:
getSelectedJavaFile(Project, PsiManager)

getSelectedJavaFile

public static com.intellij.psi.PsiJavaFile getSelectedJavaFile(com.intellij.openapi.project.Project project,
                                                               com.intellij.psi.PsiManager manager)
Get's the javafile that's currently selected in the editor. Returns null if it's not a javafile.

Parameters:
project - IDEA project.
manager - IDEA manager.
Returns:
The current javafile. Null if not a javafile.

getFields

public static com.intellij.psi.PsiField[] getFields(com.intellij.psi.PsiClass clazz)
Get's the fields for the class.

Parameters:
clazz - class.
Returns:
the fields for the class. If the class doesn't have any fields the array's size is 0.

getFieldsAsCollection

public static java.util.Collection getFieldsAsCollection(com.intellij.psi.PsiClass clazz)
Get's the fields for the class.

Parameters:
clazz - class.
Returns:
the fields for the class. If the class doesn't have any fields the collection is empty.

getPsiElemetFactory

public static com.intellij.psi.PsiElementFactory getPsiElemetFactory(com.intellij.psi.PsiManager manager)
Get's the PSIElementFactory.

The factory can be used to create methods.

Parameters:
manager - PSIManager.
Returns:
the factory.

getCurrentClass

public static com.intellij.psi.PsiClass getCurrentClass(com.intellij.psi.PsiJavaFile javaFile,
                                                        com.intellij.openapi.editor.Editor editor)
Gets the current class for the javafile and relative to the editor's caret. It's possible to get an inner class if the caret is positioned inside the innerclass.

Parameters:
javaFile - javafile for the class to find.
editor - the editor.
Returns:
the class, null if not found.

isConstantField

public static boolean isConstantField(com.intellij.psi.PsiField field)
Returns true if a field is constant. This is identifed as the name of the field is only in uppercase and it has a static modifier.

Parameters:
field - field to check if it's a constant
Returns:
true if constant.

findMethodByName

public static com.intellij.psi.PsiMethod findMethodByName(com.intellij.psi.PsiClass clazz,
                                                          java.lang.String name)
Find's an existing method with the given name. If there isn't a method with the name, null is returned.

Parameters:
clazz - the class
name - name of method to find
Returns:
the existing toString method, null if none exist

isPrimitiveArrayType

public static boolean isPrimitiveArrayType(com.intellij.psi.PsiField field)
Returns true if the given field a primtive array type (e.g., int[], long[], float[]).

Parameters:
field - field.
Returns:
true if field is a primitve array type.

isObjectArrayType

public static boolean isObjectArrayType(com.intellij.psi.PsiField field)
Is the given field an Object array type (etc. String[], Object[])?

Parameters:
field - field.
Returns:
true if it's an Object array type.

isCollectionType

public static boolean isCollectionType(com.intellij.psi.PsiElementFactory factory,
                                       com.intellij.psi.PsiField field)
                                throws com.intellij.util.IncorrectOperationException
Is the given field a Collection type?

Parameters:
factory - element factory.
field - field.
Returns:
true if it's a Collection type.
Throws:
com.intellij.util.IncorrectOperationException - is thrown if the type test failed.

isMapTye

public static boolean isMapTye(com.intellij.psi.PsiElementFactory factory,
                               com.intellij.psi.PsiField field)
                        throws com.intellij.util.IncorrectOperationException
Is the given field a Map type?

Parameters:
factory - element factory.
field - field.
Returns:
true if it's a Map type.
Throws:
com.intellij.util.IncorrectOperationException - is thrown if the type test failed.

isStringType

public static boolean isStringType(com.intellij.psi.PsiElementFactory factory,
                                   com.intellij.psi.PsiField field)
                            throws com.intellij.util.IncorrectOperationException
Is the given field a String type?

Parameters:
factory - element factory.
field - field.
Returns:
true if it's a String type.
Throws:
com.intellij.util.IncorrectOperationException - is thrown if the type test failed.

isPrimitiveType

public static boolean isPrimitiveType(com.intellij.psi.PsiField field)
Is this field a primitive type

Parameters:
field - field.
Returns:
true if this field is a primitive type.

isModifierTransient

public static boolean isModifierTransient(com.intellij.psi.PsiField field)
Returns true if a field has a transient modifier.

Parameters:
field - field.
Returns:
true if this field has a transient modifier

getCodeStyleManager

public static com.intellij.psi.codeStyle.CodeStyleManager getCodeStyleManager(com.intellij.openapi.project.Project project)
Get's the CodeStyleManager for the project.

Parameters:
project - project.
Returns:
the CodeStyleManager.

hasFields

public static boolean hasFields(com.intellij.psi.PsiClass clazz)
Does the class have any fields?

Parameters:
clazz - the PsiClass.
Returns:
true if the class has fields.

hasImportStatement

public static boolean hasImportStatement(com.intellij.psi.PsiJavaFile javaFile,
                                         java.lang.String importStatement)
Does the javafile have the importstatement.

Parameters:
javaFile - javafile.
importStatement - importstatement.
Returns:
true if the javafile has the importstatement.

addImportStatement

public static void addImportStatement(com.intellij.psi.PsiJavaFile javaFile,
                                      java.lang.String importStatementOnDemand,
                                      com.intellij.psi.PsiElementFactory factory,
                                      com.intellij.psi.codeStyle.CodeStyleManager codeStyleManager)
                               throws com.intellij.util.IncorrectOperationException
Add's an importstatement to the javafile and optimizes the imports afterwards.

Parameters:
javaFile - javafile.
importStatementOnDemand - name of importstatement, must be with a wildcard (etc. java.util.*).
factory - PSI element factory.
codeStyleManager - PSI codestyle to optimize the imports.
Throws:
com.intellij.util.IncorrectOperationException - is thrown if there is an error creating the importstatement.