Package com.iizigo.java
Class JavaHelper
java.lang.Object
com.iizigo.java.JavaHelper
Helper methods for Java.
- Author:
- Mikael Andersson, Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionstatic final IJavaElement[]
Empty array of Java Elements.static final String
Abstract class.static final String
Cannot instantiate.static final String
Constructor without parameters not public.static final String
Interface, not class.static final String
Local class.static final String
Public constructor without arguments required.static final String
Class not found.static final String
Class does not implement interface 'nnn'.static final String
Class must be public.static final String
Inner class.static final String
Not a class.static final String
Project is closed.static final int
The current maximum complier compliance level.Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionstatic String
canInstantiate
(IType clazz) Checks if a class can be instantiated.static String
checkAnnotation
(IType type, String fqnAnnotation, String param, String ref) Validates that the Annotation is tagging the class.static void
Clears the cache for the current thread.static IBinding[]
createBindings
(IJavaProject javaProject, IJavaElement[] elements) Creates the bindings that are sometimes missing.createClassNodesTree
(ModuleModel model, String implementsInterface, String requiresAnnotation) Creates a tree of classes to list in a tree available for class selection.createClassNodesTree
(ModuleModel model, String implementsInterface, String requiresAnnotation, String currentClass, TreeItemNavigatorPresentation[] rcCurrentClass) Creates a tree of classes to list in a tree available for class selection.static JavaClass
createClassReference
(GProp<?> prop, String className, IClassReferenceListener listener) Gets the reference interface to a JavaClass.static String
createJavaClassSource
(IJavaProject project, String implementsInterface) Creates a Java Class implementing the specified interface.static IType
createJavaClassSourceType
(IJavaProject project, String implementsInterface, boolean openEditorOnFinish) Creates a Java Class implementing the specified interface.createMethodTree
(ModuleModel model, String classFQN, boolean isStatic, String requiresAnnotation) Creates a tree of classes to list in a tree available for class selection.static IMethod
getAnnotatedMethod
(IType type, String method, String fqnAnnotation) Gets the method annotated by a specific annotation.static int
getAnnotationsMatch
(IAnnotation[] annotations, String annotationFQN, String param, String value, boolean allowAll) Verifies the annotations for the matching one.static ASTParser
getASTParser
(IJavaProject javaProject, int level) Gets a new or cached AST parser with resolve bindings, bindings recovery, statement recovery and Java Language Specification level (8 toMAX_COMPILER_COMPLIANCE_LEVEL
).getAvailableMethods
(IType clazz, String instanceAnnotation, String staticAnnotation) Gets the list of methods that can match the specified annotations.getBuildPathProjects
(IJavaProject javaProject) Gets the Java projects that a project includes in its build path.static IClassFile
getClassFile
(IType type) Gets the class file for a type by means of a reflection call to enable support for Neon and Oxygen.static String
Gets the fully qualified class name from a type binding as "pkg.className.inner.another".static IType
getIType
(IAdaptable adaptable) Get theIType
from aIAdaptable
.static IJavaProject
getJavaProject
(IProject project) Gets the Java Project from a Project.static String
getNonDesignerNamedClass
(GProp<?> gp) Gets the class name of a class, attempting to use a non-iiziGo but runtime class name inside com.iizix package instead.Gets all the JavaClasses available in all projects that are parsed.static IJavaElement[]
getSelection
(ISelection selection) Gets the Java Elements from a selection.static CompilationUnit
getSharedAST
(ITypeRoot typeRoot) Method to get a shared AST.static String
getSimpleSignature
(IMethodBinding method, ParsedJavaSource psj) Builds the signature of a method or constructor.static String
getSimpleSignature
(IMethod method) Builds the signature of a method.static IFolder[]
getSourceFolders
(IJavaProject javaProject) Gets the list of the Java Source folders for a Java project.static boolean
gotoJava
(Shell shell, String title, IJavaElement element) Goto Java element definition.static boolean
hasAnnotation
(IMethod method, String fqnAnnotation, String param, String ref) Validates that the Annotation is tagging the method.static boolean
hasAnnotation
(IType type, String fqnAnnotation) Validates that the Annotation is tagging the class.static boolean
hasJavaNature
(IProject project) Checks if a Project is of Java Nature.static boolean
isValidForSystem
(IJavaElement element) Checks if the compilation unit should be processed by the IIZI Module system.static String
listJavaClassSource
(Shell shell, IJavaProject project, String title, String message, String interfaze, String annotation, IJavaElement initialSelection) Displays a selection dialog box of possible Java classes matching implementing the specified interface.static String
Gets the non-fully-qualified name of a fully qualified name.static CompilationUnit
Reconciles the type.static String
selectClassName
(Shell shell, ModuleModel model, String requiredInterface, String requiredAnnotation, String currentClass) Displays a dialog box to select a class name from existing ones.static String
validateClass
(IType type, String interfaze) Validates a class for: the class and (optional) interface are valid, can be instantiated, if specified, the implementation of the interface specified (name check only).static String
validateClassName
(String className) Validates a Class Name.static MethodLookup
validateMethod
(IType type, String method, boolean isStatic, String fqnAnnotation, String param, String value, boolean allowAll) Looks up a method with the specified annotation, and optional parameter and value.static boolean
verifyPresence
(GProp<?> prop, String className) Verifies presence of a Class without changing anything.
Field Details
MAX_COMPILER_COMPLIANCE_LEVEL
public static final int MAX_COMPILER_COMPLIANCE_LEVELThe current maximum complier compliance level.This is a number from 8 to 16, currently 16.
- See Also:
EMPTY_JAVA_ELEMENTS
Empty array of Java Elements.ERR_PROJECT_CLOSED
Project is closed.- See Also:
ERR_CLASS_NOT_FOUND
Class not found.- See Also:
ERR_NOT_CLASS
Not a class.- See Also:
ERR_CLASS_NOT_IMPLEMENTING_INTERFACE
Class does not implement interface 'nnn'.- See Also:
ERR_CLASS_ABSTRACT
Abstract class.- See Also:
ERR_CLASS_LOCAL
Local class.- See Also:
ERR_INNER_CLASS
Inner class.- See Also:
ERR_CLASS_CANNOT_INSTANTIATE
Cannot instantiate.- See Also:
ERR_CLASS_INTERFACE
Interface, not class.- See Also:
ERR_CLASS_NOT_PUBLIC
Class must be public.- See Also:
ERR_CLASS_CONSTRUCTOR_NOT_PUBLIC
Constructor without parameters not public.- See Also:
ERR_CLASS_MISSING_PUBLIC_CONSTRUCTOR
Public constructor without arguments required.- See Also:
Constructor Details
JavaHelper
public JavaHelper()
Method Details
getClassFile
Gets the class file for a type by means of a reflection call to enable support for Neon and Oxygen.- Parameters:
type
- The type.- Returns:
- The class file, or null if not found.
createClassReference
public static JavaClass createClassReference(GProp<?> prop, String className, IClassReferenceListener listener) Gets the reference interface to a JavaClass.- Parameters:
prop
- A property.className
- The fully qualified class name (package and name, in Java form as "com.java.Object").listener
- The listener.- Returns:
- The reference interface.
verifyPresence
Verifies presence of a Class without changing anything.- Parameters:
prop
- The calling property.className
- The fully qualified class name (package and name, in Java form as "com.java.Object").- Returns:
- The presence flag.
getPublicOrStaticClasses
Gets all the JavaClasses available in all projects that are parsed. Matching classes are public and public static inner classes.getAvailableMethods
public static List<String> getAvailableMethods(IType clazz, String instanceAnnotation, String staticAnnotation) Gets the list of methods that can match the specified annotations.- Parameters:
clazz
- The class to search in for theinstanceAnnotation
.instanceAnnotation
- The instance annotation to search the class for, null for none.staticAnnotation
- Static annotation to search classes for static methods.
listJavaClassSource
public static String listJavaClassSource(Shell shell, IJavaProject project, String title, String message, String interfaze, String annotation, IJavaElement initialSelection) Displays a selection dialog box of possible Java classes matching implementing the specified interface.- Parameters:
shell
- The shell.project
- The Java project, null for all projects.title
- Dialog title.message
- Message to display.interfaze
- The interface to implement, null for none.annotation
- Annotation for class, null for none.initialSelection
- The initially selected object, null for none.- Returns:
- The class name created, or null if cancelled.
gotoJava
Goto Java element definition. Failures are reported in a message box with the specified title.- Parameters:
shell
- The shell, if null no message is shown.title
- Message box title in case of error.element
- The Java element, if null this will fail but not cause an error message box.- Returns:
- true for success, false for failure.
createJavaClassSource
Creates a Java Class implementing the specified interface.- Parameters:
project
- The Java project.implementsInterface
- The interface to implement, null for none.- Returns:
- The class name created, or null if cancelled.
createJavaClassSourceType
public static IType createJavaClassSourceType(IJavaProject project, String implementsInterface, boolean openEditorOnFinish) Creates a Java Class implementing the specified interface. The class is reconciled before returning.- Parameters:
project
- The Java project.implementsInterface
- The interface to implement, null for none.openEditorOnFinish
- If set, the wizard will open the created type with the default editor- Returns:
- The class name created, or null if cancelled.
reconcile
Reconciles the type.- Parameters:
type
- The type.- Returns:
- The compilation unit or null if this failed (error is already logged).
validateClassName
Validates a Class Name.- Parameters:
className
- The class name.- Returns:
- The error in validation, or null for success.
getClassFQN
Gets the fully qualified class name from a type binding as "pkg.className.inner.another".canInstantiate
Checks if a class can be instantiated.- Parameters:
clazz
- The class.- Returns:
- null if class could be instantiated, otherwise an error message.
getIType
Get theIType
from aIAdaptable
.- Parameters:
adaptable
- The adaptable instance.- Returns:
- null The primary type, null if unsuccessful.
validateClass
Validates a class for:- the class and (optional) interface are valid,
- can be instantiated,
- if specified, the implementation of the interface specified (name check only).
- Parameters:
type
- The class.interfaze
- The interface to implement, null for none.- Returns:
- The error in validation, or null for success.
getAnnotationsMatch
public static int getAnnotationsMatch(IAnnotation[] annotations, String annotationFQN, String param, String value, boolean allowAll) Verifies the annotations for the matching one. A quick-check is done to verify the presence of an annotation if theparam
is null and the annotation name matches the annotationFQN- Parameters:
annotations
- The annotations array (never null).annotationFQN
- The fully qualified annotation class name.param
- Parameter to validate, null for none.value
- Validation value, null for none.allowAll
- Allows a ref="*" if static.- Returns:
- 0=annotation not found, 1=annotation found, but not the parameter (or param=null), 2=annotation found but not the value (or value=null), 3=full match (value!=null).
hasAnnotation
Validates that the Annotation is tagging the class.- Parameters:
type
- The class.fqnAnnotation
- The fully qualified annotation class name.- Returns:
- true if the annotation is present, false otherwise.
nonFQN
Gets the non-fully-qualified name of a fully qualified name.- Parameters:
fqn
- The fully qualified name.- Returns:
- The string after the last '.' position, or the
fqn
if it didn't contain a '.'.
checkAnnotation
Validates that the Annotation is tagging the class.- Parameters:
type
- The class.fqnAnnotation
- The fully qualified annotation class name.param
- Parameter to validate, null for none.ref
- Reference value, null for none.- Returns:
- null for OK, otherwise an error message.
hasAnnotation
Validates that the Annotation is tagging the method.- Parameters:
method
- The method.fqnAnnotation
- The fully qualified annotation class name.param
- Parameter to validate, null for none.ref
- Reference value, null for none.- Returns:
- true if the annotation is present, false otherwise.
getAnnotatedMethod
Gets the method annotated by a specific annotation.- Parameters:
type
- The class.method
- The method name.fqnAnnotation
- The fully qualified annotation class name, or null if none is required.- Returns:
- The method instance or null if not found.
validateMethod
public static MethodLookup validateMethod(IType type, String method, boolean isStatic, String fqnAnnotation, String param, String value, boolean allowAll) Looks up a method with the specified annotation, and optional parameter and value.- Parameters:
type
- The class.method
- The method name.isStatic
- Method must be static flag.fqnAnnotation
- The fully qualified annotation class name, or null if none is required.param
- Parameter to validate, null for none.value
- Validation value, null for none.allowAll
- Flag to allow a ref "*" as back-reference.- Returns:
- The method lookup instance.
getASTParser
Gets a new or cached AST parser with resolve bindings, bindings recovery, statement recovery and Java Language Specification level (8 toMAX_COMPILER_COMPLIANCE_LEVEL
).- Parameters:
javaProject
- The Java project, used only when "level" is -1, can otherwise benull
.level
- NormallyMAX_COMPILER_COMPLIANCE_LEVEL
for current JLS level, -1 if not known and should be retrieved from the Java Project.- Throws:
IllegalArgumentException
- When request is for a cached AST parser requested from a non-SWT thread.
clearThreadCache
public static void clearThreadCache()Clears the cache for the current thread. This will also take the opportunity to clear the cache of threads that have died.createBindings
Creates the bindings that are sometimes missing. This is unfortunate as it requires a little processing, and you're never really sure that everything is present, such as renamed or deleted resources.- Parameters:
javaProject
- The Java project, or null to retrieve it from the elements.elements
- Elements to get bindings for.
hasJavaNature
Checks if a Project is of Java Nature.- Parameters:
project
- The project to check.- Returns:
true
if the natureJavaCore.NATURE_ID
is present,false
otherwise.- Throws:
CoreException
- If project nature couldn't be retrieved, or if it is not accessible (closed perhaps).
getJavaProject
Gets the Java Project from a Project.- Returns:
- The Java project.
- Throws:
CoreException
- If project nature could'nt be retrieved.
getSimpleSignature
Builds the signature of a method.E.g. "void(java.lang.String,com.iizix.prop.PropCnr)" for a method, or "()" or "(com.iizix.prop.Atom)" for a constructor.
- Parameters:
method
- The method binding.- Returns:
- The signature, or null for failure.
getSimpleSignature
Builds the signature of a method or constructor.E.g. "void(java.lang.String,com.iizix.prop.PropCnr)" for a method, or "()" or "(com.iizix.prop.Atom)" for a constructor.
- Parameters:
method
- The method binding.psj
- The parsed Java Source, perhaps null.- Returns:
- The signature, or null for failure.
getNonDesignerNamedClass
Gets the class name of a class, attempting to use a non-iiziGo but runtime class name inside com.iizix package instead. This is done for properties only.- Parameters:
gp
- The instance of a property.
getSourceFolders
Gets the list of the Java Source folders for a Java project.- Throws:
JavaModelException
- When getting the resolved classpath of the project fails.
getBuildPathProjects
public static ArrayList<IProject> getBuildPathProjects(IJavaProject javaProject) throws JavaModelException Gets the Java projects that a project includes in its build path.- Returns:
- A new list of the projects on the build path.
- Throws:
JavaModelException
isValidForSystem
Checks if the compilation unit should be processed by the IIZI Module system.- Parameters:
element
- The Java element, hopefully an instance of ICompilationUnit.- Returns:
- true if it's primary type is a class, false otherwise.
getSelection
Gets the Java Elements from a selection.- Parameters:
selection
- A IStructuredSelection instance, otherwise the return array is empty.- Returns:
- The adapted IJavaElement's in the selection.
createClassNodesTree
public static TreeItemNavigatorPresentation createClassNodesTree(ModuleModel model, String implementsInterface, String requiresAnnotation) Creates a tree of classes to list in a tree available for class selection. Structure is [root], projects, flat-packages, classes. The TreeItemNavigatorPresentation nodes with classes contains the "attachment" as the IType, so non-null.- Parameters:
model
- The originating module model to list only classes in dependent projects, null to list all.implementsInterface
- Interface to implement, null for none.requiresAnnotation
- Required annotation, null for none.- Returns:
- The root node.
createMethodTree
public static TreeItemNavigatorPresentation createMethodTree(ModuleModel model, String classFQN, boolean isStatic, String requiresAnnotation) Creates a tree of classes to list in a tree available for class selection. Structure is class, methods. The TreeItemNavigatorPresentation nodes with classes contains the "attachment" as the method name, so non-null.- Parameters:
model
- The originating module model to list only classes in dependent projects, null to list all. TODO: all projects are included for now...requiresAnnotation
- Required annotation, null for none.- Returns:
- null If none if found.
selectClassName
public static String selectClassName(Shell shell, ModuleModel model, String requiredInterface, String requiredAnnotation, String currentClass) Displays a dialog box to select a class name from existing ones.- Parameters:
shell
- The shell.model
- The originating module model to list only classes in dependent projects, null to list all.requiredInterface
- Required interface, or null for none.requiredAnnotation
- Required annotation, or null for none.currentClass
- A current class for selection, null for none.- Returns:
- The class name, or null for cancel.