Package com.iizigo.java
Class JavaHelper
java.lang.Object
com.iizigo.java.JavaHelper
Helper methods for Java.
- Author:
- Mikael Andersson, Christopher Mindus
- Field SummaryFieldsModifier and TypeFieldDescription- static final IJavaElement[]Empty array of Java Elements.- static final StringAbstract class.- static final StringCannot instantiate.- static final StringConstructor without parameters not public.- static final StringInterface, not class.- static final StringLocal class.- static final StringPublic constructor without arguments required.- static final StringClass not found.- static final StringClass does not implement interface 'nnn'.- static final StringClass must be public.- static final StringInner class.- static final StringNot a class.- static final StringProject is closed.- static final intThe current maximum complier compliance level.
- Constructor SummaryConstructors
- Method SummaryModifier and TypeMethodDescription- static 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 voidClears 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 to- MAX_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 StringGets the fully qualified class name from a type binding as "pkg.className.inner.another".- static IType- getIType- (IAdaptable adaptable) Get the- ITypefrom a- IAdaptable.- 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 StringGets the non-fully-qualified name of a fully qualified name.- static CompilationUnitReconciles 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_LEVELpublic 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_ELEMENTSEmpty array of Java Elements.
- ERR_PROJECT_CLOSEDProject is closed.- See Also:
 
- ERR_CLASS_NOT_FOUNDClass not found.- See Also:
 
- ERR_NOT_CLASSNot a class.- See Also:
 
- ERR_CLASS_NOT_IMPLEMENTING_INTERFACEClass does not implement interface 'nnn'.- See Also:
 
- ERR_CLASS_ABSTRACTAbstract class.- See Also:
 
- ERR_CLASS_LOCALLocal class.- See Also:
 
- ERR_INNER_CLASSInner class.- See Also:
 
- ERR_CLASS_CANNOT_INSTANTIATECannot instantiate.- See Also:
 
- ERR_CLASS_INTERFACEInterface, not class.- See Also:
 
- ERR_CLASS_NOT_PUBLICClass must be public.- See Also:
 
- ERR_CLASS_CONSTRUCTOR_NOT_PUBLICConstructor without parameters not public.- See Also:
 
- ERR_CLASS_MISSING_PUBLIC_CONSTRUCTORPublic constructor without arguments required.- See Also:
 
 
- Constructor Details- JavaHelperpublic JavaHelper()
 
- Method Details- getClassFileGets 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.
 
- createClassReferencepublic 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.
 
- verifyPresenceVerifies 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.
 
- getPublicOrStaticClassesGets all the JavaClasses available in all projects that are parsed. Matching classes are public and public static inner classes.
- getAvailableMethodspublic 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 the- instanceAnnotation.
- instanceAnnotation- The instance annotation to search the class for, null for none.
- staticAnnotation- Static annotation to search classes for static methods.
 
- listJavaClassSourcepublic 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.
 
- gotoJavaGoto 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.
 
- createJavaClassSourceCreates 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.
 
- createJavaClassSourceTypepublic 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.
 
- reconcileReconciles the type.- Parameters:
- type- The type.
- Returns:
- The compilation unit or null if this failed (error is already logged).
 
- validateClassNameValidates a Class Name.- Parameters:
- className- The class name.
- Returns:
- The error in validation, or null for success.
 
- getClassFQNGets the fully qualified class name from a type binding as "pkg.className.inner.another".
- canInstantiateChecks if a class can be instantiated.- Parameters:
- clazz- The class.
- Returns:
- null if class could be instantiated, otherwise an error message.
 
- getITypeGet the- ITypefrom a- IAdaptable.- Parameters:
- adaptable- The adaptable instance.
- Returns:
- null The primary type, null if unsuccessful.
 
- validateClassValidates 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.
 
- getAnnotationsMatchpublic 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 the- paramis 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).
 
- hasAnnotationValidates 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.
 
- nonFQNGets 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 fqnif it didn't contain a '.'.
 
- checkAnnotationValidates 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.
 
- hasAnnotationValidates 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.
 
- getAnnotatedMethodGets 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.
 
- validateMethodpublic 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.
 
- getASTParserGets a new or cached AST parser with resolve bindings, bindings recovery, statement recovery and Java Language Specification level (8 to- MAX_COMPILER_COMPLIANCE_LEVEL).- Parameters:
- javaProject- The Java project, used only when "level" is -1, can otherwise be- null.
- level- Normally- MAX_COMPILER_COMPLIANCE_LEVELfor 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.
 
- clearThreadCachepublic 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.
- createBindingsCreates 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.
 
- hasJavaNatureChecks if a Project is of Java Nature.- Parameters:
- project- The project to check.
- Returns:
- trueif the nature- JavaCore.NATURE_IDis present,- falseotherwise.
- Throws:
- CoreException- If project nature couldn't be retrieved, or if it is not accessible (closed perhaps).
 
- getJavaProjectGets the Java Project from a Project.- Returns:
- The Java project.
- Throws:
- CoreException- If project nature could'nt be retrieved.
 
- getSimpleSignatureBuilds 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.
 
- getSimpleSignatureBuilds 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.
 
- getNonDesignerNamedClassGets 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.
 
- getSourceFoldersGets the list of the Java Source folders for a Java project.- Throws:
- JavaModelException- When getting the resolved classpath of the project fails.
 
- getBuildPathProjectspublic 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
 
- isValidForSystemChecks 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.
 
- getSelectionGets 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.
 
- createClassNodesTreepublic 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.
 
- createMethodTreepublic 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.
 
- selectClassNamepublic 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.