Class JavaCodeFragment

java.lang.Object
org.faktorips.codegen.CodeFragment
org.faktorips.codegen.JavaCodeFragment

public class JavaCodeFragment extends CodeFragment
The class represents a Java source code fragment. A source code fragment consists of the source code text and the import statements needed to compile the text.
See Also:
  • Constructor Details

    • JavaCodeFragment

      public JavaCodeFragment()
      Creates a new empty JavaCodeFragment.
    • JavaCodeFragment

      public JavaCodeFragment(boolean indent)
    • JavaCodeFragment

      public JavaCodeFragment(String sourcecode)
      Creates a new JavaCodeFragment with the indicated source code and no import statements.
    • JavaCodeFragment

      public JavaCodeFragment(String sourcecode, ImportDeclaration importDecl)
      Creates a new JavaCodeFragment with the indicated source code and import declaration.
    • JavaCodeFragment

      public JavaCodeFragment(JavaCodeFragment fragment, String packageName)
      Constructs a JavaCodeFragment that has the same source code as the given fragment with all import statements removed that are obsolete because of the indicated package.
    • JavaCodeFragment

      public JavaCodeFragment(JavaCodeFragment fragment)
      Copy constructor.
  • Method Details

    • getImportDeclaration

      public ImportDeclaration getImportDeclaration()
      Returns the import declaration needed to compile the source code.
    • getImportDeclaration

      public ImportDeclaration getImportDeclaration(String packageName)
      Returns the import declaration needed to compile the source code. The returned import declaration does not contain any import statements that refer to the indicated packageName. The method is useful to avoid unnecessary import statements.
    • appendOpenBracket

      public JavaCodeFragment appendOpenBracket()
      Appends '{' to the source code and increases the indentation level.
    • appendCloseBracket

      public JavaCodeFragment appendCloseBracket()
      Appends '}' to the source code and decreases the indentation level.
    • append

      public JavaCodeFragment append(String s)
      Description copied from class: CodeFragment
      Appends the given String to the source code.
      Overrides:
      append in class CodeFragment
    • appendJoined

      public JavaCodeFragment appendJoined(Iterable<?> iterable)
      Description copied from class: CodeFragment
      Appends the given Iterable's content converted to String and separated by ", " to the source code.
      Overrides:
      appendJoined in class CodeFragment
    • appendJoined

      public JavaCodeFragment appendJoined(Object[] array)
      Description copied from class: CodeFragment
      Appends the given array's content converted to String and separated by ", " to the source code.
      Overrides:
      appendJoined in class CodeFragment
    • appendJoined

      public JavaCodeFragment appendJoined(JavaCodeFragment[] javaCodeFragments)
    • appendQuoted

      public JavaCodeFragment appendQuoted(String s)
      Description copied from class: CodeFragment
      Encloses the given String with double quotes (") and appends it to the source code.
      Overrides:
      appendQuoted in class CodeFragment
    • append

      public JavaCodeFragment append(char c)
      Description copied from class: CodeFragment
      Appends the given char to the source code.
      Overrides:
      append in class CodeFragment
    • appendln

      public JavaCodeFragment appendln()
      Description copied from class: CodeFragment
      Appends a line separator to the source code.
      Overrides:
      appendln in class CodeFragment
    • appendln

      public JavaCodeFragment appendln(String s)
      Description copied from class: CodeFragment
      Appends the given String and a line separator to the source code.
      Overrides:
      appendln in class CodeFragment
    • appendlnUnindented

      public JavaCodeFragment appendlnUnindented(String arg)
      Description copied from class: CodeFragment
      Appends the given String as is to the source code without indenting it.
      Overrides:
      appendlnUnindented in class CodeFragment
    • appendln

      public JavaCodeFragment appendln(char c)
      Description copied from class: CodeFragment
      Appends the given char to the source code.
      Overrides:
      appendln in class CodeFragment
    • append

      public JavaCodeFragment append(int i)
      Transform the given int into a String and appends it to the source code.
    • append

      public JavaCodeFragment append(boolean b)
      Transform the given boolean into a String and appends it to the source code.
    • appendClassName

      public JavaCodeFragment appendClassName(Class<?> clazz)
      Appends the unqualified class name to the source code and updates the import declaration (if necessary).
      Throws:
      NullPointerException - if clazz is null.
    • appendInnerClassName

      public JavaCodeFragment appendInnerClassName(Class<?> clazz)
      Appends the unqualified class name of an public inner class to the source code and updates the import declaration (if necessary).
      Throws:
      NullPointerException - if clazz is null.
    • appendInnerClassName

      public JavaCodeFragment appendInnerClassName(String qualifiedClassName)
      Appends the unqualified class name of an public inner class to the source code and updates the import declaration (if necessary).
      Throws:
      NullPointerException - if qualifiedClassName is null.
    • appendClassName

      public JavaCodeFragment appendClassName(String qualifiedClassName)
      Appends the unqualified class name to the source code and updates the import declaration (if necessary). A [] appended at the end of the qualified class name indicates the array of this type. The brackets are added correctly at the end of the class name in the source code.
      Throws:
      NullPointerException - if qualifiedClassName is null.
    • addImport

      public JavaCodeFragment addImport(String qualifiedClassName)
      Adds an import entry to this code fragment.
      Parameters:
      qualifiedClassName - the java class that is added to the import declaration
    • append

      public JavaCodeFragment append(CodeFragment fragment)
      Appends the given fragment to his fragment and indents it properly.
      Overrides:
      append in class CodeFragment
    • append

      public JavaCodeFragment append(JavaCodeFragment fragment)
      Appends the given fragment to his fragment and indents it properly.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class CodeFragment
    • equals

      public boolean equals(Object obj)
      Two fragments are equal if they contain the same source code and have the same import declaration.
      Overrides:
      equals in class CodeFragment
    • toString

      public String toString()
      Returns the CodeFragment as String in "normal" Java style, that means first all import statements, new line, than the source code.
      Overrides:
      toString in class CodeFragment
      See Also: