Class JavaSource

java.lang.Object
com.vaadin.copilot.javarewriter.JavaSource

public class JavaSource extends Object
Represents a Java source file that can be parsed and rewritten using a custom observer and parser configuration. Provides functionality to parse the given Java source code.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JavaSource(File file, String source)
    Constructs a JavaSource with the given Java source code.
    JavaSource(File file, String source, boolean solveJdkClassesBySimpleName)
    Constructs a JavaSource with the given Java source code.
    JavaSource(File file, String source, boolean solveJdkClassesBySimpleName, boolean javaParserLexicalPreservation)
    Constructs a JavaSource with the given Java source code.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.github.javaparser.ast.CompilationUnit
    Parsed compilation unit of the given source from constructor.
    Returns the file associated with this Java source.
    int
    Finds the first modified row in the given source by comparing result and initial source
    Applies changes to the given source and returns the result file content
    Returns the source code.
    boolean
    Checks if there have been any changes based on added, modified, or removed nodes.
    boolean
    Checks if the file is written into disk.
    void
    setWritten(boolean written)
    Updates the flag with the given written value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JavaSource

      public JavaSource(File file, String source)
      Constructs a JavaSource with the given Java source code. The source is parsed without attempting to resolve JDK classes by their simple names.
      Parameters:
      file - the file containing the Java source code
      source - the raw Java source code as a String
    • JavaSource

      public JavaSource(File file, String source, boolean solveJdkClassesBySimpleName)
      Constructs a JavaSource with the given Java source code. Allows specifying whether JDK classes should be resolved by their simple names.
      Parameters:
      file - the file containing the Java source code
      source - the raw Java source code as a String
      solveJdkClassesBySimpleName - true to resolve JDK classes by simple name, false otherwise
    • JavaSource

      public JavaSource(File file, String source, boolean solveJdkClassesBySimpleName, boolean javaParserLexicalPreservation)
      Constructs a JavaSource with the given Java source code. Allows specifying whether JDK classes should be resolved by their simple names.
      Parameters:
      file - the file containing the Java source code
      source - the raw Java source code as a String
      solveJdkClassesBySimpleName - true to resolve JDK classes by simple name, false otherwise
      javaParserLexicalPreservation - true to use the JavaParser feature to preserve indentation, false (default) to use the faster but sometimes broken version in Copilot
  • Method Details

    • getCompilationUnit

      public com.github.javaparser.ast.CompilationUnit getCompilationUnit()
      Parsed compilation unit of the given source from constructor.
      Returns:
      Compilation Unit
    • isChanged

      public boolean isChanged()
      Checks if there have been any changes based on added, modified, or removed nodes. This method returns true if there are any added or modified nodes, or if there are any removed ranges.
      Returns:
      true if there are changes (i.e., added/modified nodes or removed ranges), false if there are no changes.
    • setWritten

      public void setWritten(boolean written)
      Updates the flag with the given written value.
      Parameters:
      written - true if it is saved, false otherwise.
    • isWritten

      public boolean isWritten()
      Checks if the file is written into disk.
      Returns:
      true if it is written, false otherwise.
    • getResult

      public String getResult()
      Applies changes to the given source and returns the result file content
      Returns:
      Result file content after changes are applied
    • getFirstModifiedRow

      public int getFirstModifiedRow()
      Finds the first modified row in the given source by comparing result and initial source
      Returns:
      row number, or -1 if no change is found.
    • getSource

      public String getSource()
      Returns the source code.
      Returns:
      the source code
    • getFile

      public File getFile()
      Returns the file associated with this Java source.
      Returns:
      the file containing the Java source code