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 compiler compliance level: the latest Java Language Specification level that the JDT of this Eclipse supports (AST.getJLSLatest()).

      It used to be fixed at AST.JLS15 ("until 16 or better is available in Tycho"), so every AST and binding resolution of iiziGo parsed Java 16+ syntax (pattern matching for instanceof, records, switch patterns) at level 15, in recovery mode.

    • MODULE_PROJECT_EXECUTION_ENVIRONMENT

      public static final String MODULE_PROJECT_EXECUTION_ENVIRONMENT
      The execution environment a Module project created by the quick-start wizard is pinned to: Java SE 25, the Java level of the IIZI runtime. The project does not follow the workspace default JRE.
      See Also:
    • MODULE_PROJECT_COMPLIANCE

      public static final String MODULE_PROJECT_COMPLIANCE
      The compiler compliance a Module project created by the quick-start wizard is pinned to: Java 25.
      See Also:
    • EMPTY_JAVA_ELEMENTS

      public static final org.eclipse.jdt.core.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 org.eclipse.jdt.core.IClassFile getClassFile(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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, org.eclipse.jdt.core.IJavaProject project, String title, String message, String interfaze, String annotation, org.eclipse.jdt.core.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, org.eclipse.jdt.core.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(org.eclipse.jdt.core.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 org.eclipse.jdt.core.IType createJavaClassSourceType(org.eclipse.jdt.core.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 org.eclipse.jdt.core.dom.CompilationUnit reconcile(org.eclipse.jdt.core.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(org.eclipse.jdt.core.dom.ITypeBinding tb)
      Gets the fully qualified class name from a type binding as "pkg.className.inner.another".
    • canInstantiate

      public static String canInstantiate(org.eclipse.jdt.core.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 org.eclipse.jdt.core.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(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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 org.eclipse.jdt.core.IMethod getAnnotatedMethod(org.eclipse.jdt.core.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(org.eclipse.jdt.core.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 org.eclipse.jdt.core.dom.CompilationUnit getSharedAST(org.eclipse.jdt.core.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 org.eclipse.jdt.core.dom.ASTParser getASTParser(org.eclipse.jdt.core.IJavaProject javaProject, int level)
      Creates a new AST parser with resolve bindings, bindings recovery, statement recovery and Java Language Specification level (8 to MAX_COMPILER_COMPLIANCE_LEVEL).

      A parser is created for every call: ASTParser resets its settings after each createAST, createASTs and createBindings, so the per-thread cache this method used to keep only kept the level and lost the recovery flags from the second use on.

      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.
    • createBindings

      public static org.eclipse.jdt.core.dom.IBinding[] createBindings(org.eclipse.jdt.core.IJavaProject javaProject, org.eclipse.jdt.core.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 org.eclipse.jdt.core.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.
    • newModuleProjectJREContainerEntry

      public static org.eclipse.jdt.core.IClasspathEntry newModuleProjectJREContainerEntry()
      Creates the JRE container entry of a new Module project: the MODULE_PROJECT_EXECUTION_ENVIRONMENT execution environment, so that the project does not follow the workspace default JRE. Falls back to the workspace default JRE container if this Eclipse does not know the execution environment.
      Returns:
      The classpath entry.
    • setModuleProjectCompliance

      public static void setModuleProjectCompliance(org.eclipse.jdt.core.IJavaProject javaProject)
      Pins the compiler compliance of a new Module project to MODULE_PROJECT_COMPLIANCE (compliance, source and target), so that the project does not follow the workspace compliance.
      Parameters:
      javaProject - The Java project.
    • getSimpleSignature

      public static String getSimpleSignature(org.eclipse.jdt.core.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(org.eclipse.jdt.core.dom.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(org.eclipse.jdt.core.IJavaProject javaProject) throws org.eclipse.jdt.core.JavaModelException
      Gets the list of the Java Source folders for a Java project.
      Throws:
      org.eclipse.jdt.core.JavaModelException - When getting the resolved classpath of the project fails.
    • getBuildPathProjects

      public static ArrayList<IProject> getBuildPathProjects(org.eclipse.jdt.core.IJavaProject javaProject) throws org.eclipse.jdt.core.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:
      org.eclipse.jdt.core.JavaModelException
    • isValidForSystem

      public static boolean isValidForSystem(org.eclipse.jdt.core.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 org.eclipse.jdt.core.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.