Class JavaSourceFileProcessor<PROP extends GProp<?>>

  • Direct Known Subclasses:
    TestProcessor, TypeFileProcessor

    public abstract class JavaSourceFileProcessor<PROP extends GProp<?>>
    extends java.lang.Object
    Abstract class for parsing a Java source file to perform syntax-check and reference validation of Java source code to the iizi property tree as well as refactoring of the Java source code when properties are changed.
    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected PROP container
      The container created from the call to createPropertyContainer or after XML is loaded.
      protected ParsedJavaSource javaSource
      The ParsedJavaSource reference, set after call to the initialize method, do not use prior to that, it is null.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract PROP createPropertyContainer​(ParsedJavaSource javaSource)
      Creates the property container for the data required by the processor when no saved XML is present at load-time.
      abstract void dispose()
      Called when the Java source is disposed of.
      protected void getAvailableMethods​(java.util.ArrayList<java.lang.String> list, org.eclipse.jdt.core.IType clazz, java.lang.String instanceAnnotation, java.lang.String staticAnnotation)
      Gets the list of methods that can match the specified annotations.
      IJavaSourceParser<PROP> getEngine()
      Returns the Engine instance that created this instance.
      void initialize​(ParsedJavaSource javaSource, GProp<?> container)
      Framework call to initialize, calls onInitialize after having saved the javaSource and container parameters in the class members of the same names.
      void internalRestoreContainerClone​(GProp<?> container)
      Called when a cloned property container has been restored because a Java source file has been edited and then closed without saving.
      boolean isRefactorRequired()
      Checks if this parser needs to refactor something.
      boolean isReparseRequired()
      Checks if reparsing is required at startup.
      abstract void onInitialize()
      Called when the property container is created, either from a call to createPropertyContainer or has been loaded from XML, in which case it could be initiated for specialized data for the processor, e.g.
      void onJavaSourceMoved​(org.eclipse.core.resources.IFile oldFile, org.eclipse.core.resources.IFile newFile, java.lang.String oldPKG, java.lang.String newPKG)
      Called when a Java source has been moved or renamed.
      void onJavaSourceRemoved​(org.eclipse.core.resources.IFile file)
      Called when a Java source has been removed.
      void onRefactorCommit​(org.eclipse.text.edits.MultiTextEdit fileChangeRootEdit)
      Called when the refactoring on each source is to be performed.
      void onRefactoringChange​(java.lang.String classFQN, java.lang.String oldFQN, java.lang.String newFQN)
      Called from Refactoring Rename for classes or methods.
      void onRestoreContainerClone()
      Called when a cloned property container has been restored because a Java source file has been edited and then closed without saving.
      void onRestoredState​(org.eclipse.jdt.core.ICompilationUnit icu, org.eclipse.core.runtime.IProgressMonitor monitor)
      Called to inform the processor that saved parser state is used from a previous parse operation.
      void parse​(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit)
      Called to parse the Java source at load and when the Java compiler runs, using an AST compilation unit and a flag indicating if the compilation unit contains annotations.
      void reconcile​(org.eclipse.jdt.core.compiler.ReconcileContext context, org.eclipse.jdt.core.dom.CompilationUnit cu)
      Called by the Java compilation participant let the processor handle the reconciled working copy.
      java.lang.String validateClass​(org.eclipse.jdt.core.IType type, java.lang.String implementsInterface, java.lang.String requiredAnnotation, java.lang.String annotationParam, java.lang.String annotationValue)
      Validates the class for the interface and annotation.
      protected MethodLookup validateMethod​(org.eclipse.jdt.core.IType type, java.lang.String method, boolean isStatic, java.lang.String requiredAnnotation, java.lang.String annotationParam, java.lang.String annotationValue, boolean allowAll)
      Validates the method for the annotation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • javaSource

        protected ParsedJavaSource javaSource
        The ParsedJavaSource reference, set after call to the initialize method, do not use prior to that, it is null.
      • container

        protected PROP extends GProp<?> container
        The container created from the call to createPropertyContainer or after XML is loaded.
    • Constructor Detail

      • JavaSourceFileProcessor

        public JavaSourceFileProcessor​(IJavaSourceParser<PROP> engine)
        Default constructor.
    • Method Detail

      • dispose

        public abstract void dispose()
        Called when the Java source is disposed of.
      • getEngine

        public final IJavaSourceParser<PROP> getEngine()
        Returns the Engine instance that created this instance.
      • createPropertyContainer

        public abstract PROP createPropertyContainer​(ParsedJavaSource javaSource)
        Creates the property container for the data required by the processor when no saved XML is present at load-time. The container should NOT be added to any other property container, it should remain orphan, and it is NOT required to have an Atom name, i.e. it can be unnamed.
        Parameters:
        javaSource - The Java source.
        Returns:
        The container instance, or null if non is required.
      • initialize

        public final void initialize​(ParsedJavaSource javaSource,
                                     GProp<?> container)
        Framework call to initialize, calls onInitialize after having saved the javaSource and container parameters in the class members of the same names.
        Parameters:
        javaSource - The Java source.
        container - The property container created.
      • onInitialize

        public abstract void onInitialize()
        Called when the property container is created, either from a call to createPropertyContainer or has been loaded from XML, in which case it could be initiated for specialized data for the processor, e.g. setting up instance references to the children properties, reference to the processor itself.
      • onJavaSourceMoved

        public void onJavaSourceMoved​(org.eclipse.core.resources.IFile oldFile,
                                      org.eclipse.core.resources.IFile newFile,
                                      java.lang.String oldPKG,
                                      java.lang.String newPKG)
        Called when a Java source has been moved or renamed. The getSourceFile() method in JavaSource is already moved or renamed when this method is called.

        Subclasses can override this method to perform reference updates.

        Parameters:
        oldFile - The old file name.
        newFile - The new file name.
        oldPKG - The old package name of the main class.
        newPKG - The new package name of the main class.
      • onJavaSourceRemoved

        public void onJavaSourceRemoved​(org.eclipse.core.resources.IFile file)
        Called when a Java source has been removed.

        Subclasses can override this method to perform reference updates.

        Parameters:
        file - The file.
      • onRefactoringChange

        public void onRefactoringChange​(java.lang.String classFQN,
                                        java.lang.String oldFQN,
                                        java.lang.String newFQN)
        Called from Refactoring Rename for classes or methods. The "FQN" is for classes including packages, and enclosed classes are separated by '.' and not '$'. For methods, it's the name followed by ',' and "returnType(param1,..,paramN)" as JavaHelper.getSignature gives.
        Parameters:
        classFQN - The class fully qualified name, null for classes (only for methods).
        oldFQN - The old fully qualified name or "oldName,signature".
        newFQN - The new fully qualified name or "newName,signature".
      • isReparseRequired

        public boolean isReparseRequired()
        Checks if reparsing is required at startup.
        Returns:
        false by default, override to process and return true as required.
      • onRestoredState

        public void onRestoredState​(org.eclipse.jdt.core.ICompilationUnit icu,
                                    org.eclipse.core.runtime.IProgressMonitor monitor)
                             throws org.eclipse.core.runtime.CoreException
        Called to inform the processor that saved parser state is used from a previous parse operation. This data is available in the property container.

        Subclasses should override this method to handle the event.

        Parameters:
        icu - The compilation unit.
        monitor - Progress monitor.
        Throws:
        org.eclipse.core.runtime.CoreException - If the parser failed to restore the state.
      • parse

        public void parse​(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit)
                   throws org.eclipse.core.runtime.CoreException
        Called to parse the Java source at load and when the Java compiler runs, using an AST compilation unit and a flag indicating if the compilation unit contains annotations.
        Parameters:
        compilationUnit - The compilation unit.
        Throws:
        org.eclipse.core.runtime.CoreException - For errors with e.g. removing markers.
      • reconcile

        public void reconcile​(org.eclipse.jdt.core.compiler.ReconcileContext context,
                              org.eclipse.jdt.core.dom.CompilationUnit cu)
        Called by the Java compilation participant let the processor handle the reconciled working copy.
        Parameters:
        context - The reconcile context to act on.
      • internalRestoreContainerClone

        public final void internalRestoreContainerClone​(GProp<?> container)
        Called when a cloned property container has been restored because a Java source file has been edited and then closed without saving. This is done in order to revert the reconciled changes that may apply after the original parsing.
      • onRestoreContainerClone

        public void onRestoreContainerClone()
        Called when a cloned property container has been restored because a Java source file has been edited and then closed without saving. This is done in order to revert the reconciled changes that may apply after the original parsing.
      • isRefactorRequired

        public boolean isRefactorRequired()
        Checks if this parser needs to refactor something.
        Returns:
        true to let the framework perform a grouped refactoring when the RefactorCommit event is received and processed by it.
      • onRefactorCommit

        public void onRefactorCommit​(org.eclipse.text.edits.MultiTextEdit fileChangeRootEdit)
        Called when the refactoring on each source is to be performed.

        When this method is called, refactoring the code for a plain text file using positions would look like:

           ReplaceEdit edit=new ReplaceEdit(offset,length,string);
           fileChangeRootEdit.addChild(edit);
         
        Parameters:
        fileChangeRootEdit - The root edit of the Java file change.
      • validateClass

        public java.lang.String validateClass​(org.eclipse.jdt.core.IType type,
                                              java.lang.String implementsInterface,
                                              java.lang.String requiredAnnotation,
                                              java.lang.String annotationParam,
                                              java.lang.String annotationValue)
        Validates the class for the interface and annotation.
        Parameters:
        implementsInterface - The interface required, or null for none.
        requiredAnnotation - The required annotation, or null for none.
        annotationParam - Parameter member value pair to verify the value with for back-referencing, null for none.
        annotationValue - The value to match for back-referencing, null for none.
        Returns:
        null for OK, otherwise an error message, or NOT_PROCESSED.
      • validateMethod

        protected MethodLookup validateMethod​(org.eclipse.jdt.core.IType type,
                                              java.lang.String method,
                                              boolean isStatic,
                                              java.lang.String requiredAnnotation,
                                              java.lang.String annotationParam,
                                              java.lang.String annotationValue,
                                              boolean allowAll)
        Validates the method for the annotation.
        Parameters:
        type - The class.
        method - The method name.
        isStatic - Method must be static flag.
        requiredAnnotation - The required annotation, or null for none.
        annotationParam - Parameter member value pair to verify the value with for back-referencing, null for none.
        annotationValue - The value to match for back-referencing, null for none.
        allowAll - Flag to allow a ref "*" as back-reference.
        Returns:
        The IMethod matching for OK, otherwise a String error message, or NOT_PROCESSED.
      • getAvailableMethods

        protected void getAvailableMethods​(java.util.ArrayList<java.lang.String> list,
                                           org.eclipse.jdt.core.IType clazz,
                                           java.lang.String instanceAnnotation,
                                           java.lang.String staticAnnotation)
        Gets the list of methods that can match the specified annotations.
        Parameters:
        list - List filled in with info upon return.
        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.