Class JavaHelper

java.lang.Object
com.iizigo.java.JavaHelper

public class JavaHelper extends Object
Helper methods for Java.
Author:
Mikael Andersson, Christopher Mindus
  • Field Details

    • MAX_COMPILER_COMPLIANCE_LEVEL

      public static final int MAX_COMPILER_COMPLIANCE_LEVEL
      The current maximum complier compliance level.

      This is a number from 8 to 16, currently 16.

      See Also:
    • EMPTY_JAVA_ELEMENTS

      public static final IJavaElement[] EMPTY_JAVA_ELEMENTS
      Empty array of Java Elements.
    • ERR_PROJECT_CLOSED

      public static final String ERR_PROJECT_CLOSED
      Project is closed.
      See Also:
    • ERR_CLASS_NOT_FOUND

      public static final String ERR_CLASS_NOT_FOUND
      Class not found.
      See Also:
    • ERR_NOT_CLASS

      public static final String ERR_NOT_CLASS
      Not a class.
      See Also:
    • ERR_CLASS_NOT_IMPLEMENTING_INTERFACE

      public static final String ERR_CLASS_NOT_IMPLEMENTING_INTERFACE
      Class does not implement interface 'nnn'.
      See Also:
    • ERR_CLASS_ABSTRACT

      public static final String ERR_CLASS_ABSTRACT
      Abstract class.
      See Also:
    • ERR_CLASS_LOCAL

      public static final String ERR_CLASS_LOCAL
      Local class.
      See Also:
    • ERR_INNER_CLASS

      public static final String ERR_INNER_CLASS
      Inner class.
      See Also:
    • ERR_CLASS_CANNOT_INSTANTIATE

      public static final String ERR_CLASS_CANNOT_INSTANTIATE
      Cannot instantiate.
      See Also:
    • ERR_CLASS_INTERFACE

      public static final String ERR_CLASS_INTERFACE
      Interface, not class.
      See Also:
    • ERR_CLASS_NOT_PUBLIC

      public static final String ERR_CLASS_NOT_PUBLIC
      Class must be public.
      See Also:
    • ERR_CLASS_CONSTRUCTOR_NOT_PUBLIC

      public static final String ERR_CLASS_CONSTRUCTOR_NOT_PUBLIC
      Constructor without parameters not public.
      See Also:
    • ERR_CLASS_MISSING_PUBLIC_CONSTRUCTOR

      public static final String ERR_CLASS_MISSING_PUBLIC_CONSTRUCTOR
      Public constructor without arguments required.
      See Also:
  • Constructor Details

    • JavaHelper

      public JavaHelper()
  • Method Details

    • getClassFile

      public 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.
      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

      public static boolean verifyPresence(GProp<?> prop, String className)
      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

      public static List<JavaClass> 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 the instanceAnnotation.
      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

      public static boolean gotoJava(Shell shell, String title, IJavaElement element)
      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

      public static String createJavaClassSource(IJavaProject project, String implementsInterface)
      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

      public static CompilationUnit reconcile(IType type)
      Reconciles the type.
      Parameters:
      type - The type.
      Returns:
      The compilation unit or null if this failed (error is already logged).
    • validateClassName

      public static String validateClassName(String className)
      Validates a Class Name.
      Parameters:
      className - The class name.
      Returns:
      The error in validation, or null for success.
    • getClassFQN

      public static String getClassFQN(ITypeBinding tb)
      Gets the fully qualified class name from a type binding as "pkg.className.inner.another".
    • canInstantiate

      public static String canInstantiate(IType clazz)
      Checks if a class can be instantiated.
      Parameters:
      clazz - The class.
      Returns:
      null if class could be instantiated, otherwise an error message.
    • getIType

      public static IType getIType(IAdaptable adaptable)
      Get the IType from a IAdaptable.
      Parameters:
      adaptable - The adaptable instance.
      Returns:
      null The primary type, null if unsuccessful.
    • validateClass

      public static String validateClass(IType type, String interfaze)
      Validates a class for:
      1. the class and (optional) interface are valid,
      2. can be instantiated,
      3. 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 the param 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

      public static boolean hasAnnotation(IType type, String fqnAnnotation)
      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

      public static String nonFQN(String fqn)
      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

      public static String checkAnnotation(IType type, String fqnAnnotation, String param, String ref)
      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

      public static boolean hasAnnotation(IMethod method, String fqnAnnotation, String param, String ref)
      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

      public static IMethod getAnnotatedMethod(IType type, String method, String fqnAnnotation)
      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.
    • getSharedAST

      public static CompilationUnit getSharedAST(ITypeRoot typeRoot)
      Method to get a shared AST.
      Parameters:
      typeRoot - The type root, i.e. the top class.
      Returns:
      The compilation unit.
    • getASTParser

      public 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).
      Parameters:
      javaProject - The Java project, used only when "level" is -1, can otherwise be null.
      level - Normally MAX_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

      public static IBinding[] createBindings(IJavaProject javaProject, IJavaElement[] elements)
      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

      public static boolean hasJavaNature(IProject project) throws CoreException
      Checks if a Project is of Java Nature.
      Parameters:
      project - The project to check.
      Returns:
      true if the nature JavaCore.NATURE_ID is present, false otherwise.
      Throws:
      CoreException - If project nature couldn't be retrieved, or if it is not accessible (closed perhaps).
    • getJavaProject

      public static IJavaProject getJavaProject(IProject project) throws CoreException
      Gets the Java Project from a Project.
      Returns:
      The Java project.
      Throws:
      CoreException - If project nature could'nt be retrieved.
    • getSimpleSignature

      public static String getSimpleSignature(IMethod method)
      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

      public static String getSimpleSignature(IMethodBinding method, ParsedJavaSource psj)
      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

      public 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. This is done for properties only.
      Parameters:
      gp - The instance of a property.
    • getSourceFolders

      public static IFolder[] getSourceFolders(IJavaProject javaProject) throws JavaModelException
      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

      public static boolean isValidForSystem(IJavaElement element)
      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

      public static IJavaElement[] getSelection(ISelection selection)
      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.
    • createClassNodesTree

      public static TreeItemNavigatorPresentation 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. 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.
      currentClass - Class to find, null for none.
      rcCurrentClass - Returned matching class found in [0], 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.