Class DeclarationElement

java.lang.Object
org.bluezoo.gumdrop.servlet.jsp.DeclarationElement
All Implemented Interfaces:
JSPElement

public class DeclarationElement extends Object implements JSPElement
Represents a JSP declaration element: <%! java declarations %>.

Declarations contain Java code that becomes class-level members of the generated servlet. This includes instance variables, methods, and inner classes. Declarations are executed when the servlet is instantiated, not during request processing.

Example declarations:

 <%!
   private int counter = 0;
   
   private String formatDate(Date date) {
     return new SimpleDateFormat("yyyy-MM-dd").format(date);
   }
   
   static {
     System.out.println("Servlet class loaded");
   }
 %>
 

Note: Instance variables declared in JSP declarations are shared across all requests to the servlet, so thread safety must be considered.

Author:
Chris Burdess
  • Constructor Details

    • DeclarationElement

      public DeclarationElement(String declaration, int lineNumber, int columnNumber)
      Creates a new declaration element.
      Parameters:
      declaration - the Java declaration code
      lineNumber - the line number where this element begins (1-based)
      columnNumber - the column number where this element begins (1-based)
  • Method Details

    • getType

      public JSPElement.Type getType()
      Description copied from interface: JSPElement
      Gets the type of this JSP element.
      Specified by:
      getType in interface JSPElement
      Returns:
      the element type
    • getLineNumber

      public int getLineNumber()
      Description copied from interface: JSPElement
      Gets the line number where this element begins in the source JSP file.
      Specified by:
      getLineNumber in interface JSPElement
      Returns:
      the line number (1-based), or -1 if not available
    • getColumnNumber

      public int getColumnNumber()
      Description copied from interface: JSPElement
      Gets the column number where this element begins in the source JSP file.
      Specified by:
      getColumnNumber in interface JSPElement
      Returns:
      the column number (1-based), or -1 if not available
    • getDeclaration

      public String getDeclaration()
      Gets the Java declaration code contained in this element.
      Returns:
      the Java declaration code (never null)
    • isEmpty

      public boolean isEmpty()
      Checks if this declaration contains only whitespace or comments.
      Returns:
      true if the declaration is effectively empty
    • containsMethods

      public boolean containsMethods()
      Checks if this declaration appears to contain method definitions. This is a heuristic check for code organization purposes.
      Returns:
      true if the declaration likely contains methods
    • containsFields

      public boolean containsFields()
      Checks if this declaration appears to contain field definitions. This is a heuristic check for code organization purposes.
      Returns:
      true if the declaration likely contains fields
    • accept

      public void accept(JSPElementVisitor visitor) throws Exception
      Description copied from interface: JSPElement
      Accepts a visitor for processing this element. This enables the visitor pattern for code generation and other tree traversal operations.
      Specified by:
      accept in interface JSPElement
      Parameters:
      visitor - the visitor to accept
      Throws:
      Exception - if the visitor encounters an error
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object