RECODER 0.92

recoder.java
Class CompilationUnit

java.lang.Object
  extended by recoder.java.JavaSourceElement
      extended by recoder.java.JavaProgramElement
          extended by recoder.java.JavaNonTerminalProgramElement
              extended by recoder.java.CompilationUnit
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, TypeDeclarationContainer, NonTerminalProgramElement, ProgramElement, ScopeDefiningElement, SourceElement, TypeScope, ModelElement

public class CompilationUnit
extends JavaNonTerminalProgramElement
implements TypeDeclarationContainer, TypeScope

A node representing a single source file containing TypeDeclarations and an optional PackageSpecificationand an optional set of Imports. In Java, any source file may contain at most one public class type definition.

Author:
AL, AutoDoc
See Also:
Serialized Form

Field Summary
 
Fields inherited from class recoder.java.JavaSourceElement
factory
 
Constructor Summary
  CompilationUnit()
          Compilation unit.
protected CompilationUnit(CompilationUnit proto)
          Compilation unit.
  CompilationUnit(PackageSpecification pkg, ASTList<Import> imports, ASTList<TypeDeclaration> typeDeclarations)
          Compilation unit.
 
Method Summary
 void accept(SourceVisitor v)
          Receive a visitor, for instance a pretty printer.
 void addTypeToScope(ClassType type, java.lang.String name)
           
 CompilationUnit deepClone()
          Deep clone.
 NonTerminalProgramElement getASTParent()
          A compilation unit has no syntactical parent
 ProgramElement getChildAt(int index)
          Returns the child at the specified index in this node's "virtual" child array
 int getChildCount()
          Returns the number of children of this node.
 int getChildPositionCode(ProgramElement child)
          Returns the positional code of the given child, or -1 if there is no such child.
 DataLocation getDataLocation()
          Gets the current data location.
 ASTList<TypeDeclaration> getDeclarations()
          Get declarations.
 SourceElement getFirstElement()
          Finds the source element that occurs first in the source.
 ASTList<Import> getImports()
          Get imports.
 SourceElement getLastElement()
          Finds the source element that occurs last in the source.
 java.lang.String getName()
          Get name of the unit.
 DataLocation getOriginalDataLocation()
          Gets the original data location.
 PackageSpecification getPackageSpecification()
          Get package specification.
 TypeDeclaration getPrimaryTypeDeclaration()
          Gets the primary type declaration of the compilation unit.
 TypeDeclaration getTypeDeclarationAt(int index)
           
 int getTypeDeclarationCount()
          Get the number of type declarations in this container.
 ClassType getTypeInScope(java.lang.String name)
           
 java.util.List<ClassType> getTypesInScope()
           
 boolean isDefinedScope()
          Check if the scope has been set up.
 void makeParentRoleValid()
          Make parent role valid.
 void removeTypeFromScope(java.lang.String name)
           
 boolean replaceChild(ProgramElement p, ProgramElement q)
          Replace a single child in the current node.
 void setDataLocation(DataLocation location)
          Sets the current data location.
 void setDeclarations(ASTList<TypeDeclaration> list)
          Set declarations.
 void setDefinedScope(boolean defined)
          Sets the scope to be defined or undefined.
 void setImports(ASTList<Import> list)
          Set imports.
 void setPackageSpecification(PackageSpecification p)
          Set package specification.
 
Methods inherited from class recoder.java.JavaNonTerminalProgramElement
getIndexOfChild, getRoleOfChild, makeAllParentRolesValid, validateAll
 
Methods inherited from class recoder.java.JavaProgramElement
getComments, getID, setComments, validate
 
Methods inherited from class recoder.java.JavaSourceElement
getEndPosition, getFactory, getRelativePosition, getStartPosition, setEndPosition, setRelativePosition, setStartPosition, toSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompilationUnit

public CompilationUnit()
Compilation unit.


CompilationUnit

public CompilationUnit(PackageSpecification pkg,
                       ASTList<Import> imports,
                       ASTList<TypeDeclaration> typeDeclarations)
Compilation unit.

Parameters:
pkg - a package specification.
imports - an import mutable list.
typeDeclarations - a type declaration mutable list.

CompilationUnit

protected CompilationUnit(CompilationUnit proto)
Compilation unit. Does not copy the data location.

Parameters:
proto - a compilation unit.
Method Detail

deepClone

public CompilationUnit deepClone()
Deep clone.

Specified by:
deepClone in interface SourceElement
Returns:
the object.

makeParentRoleValid

public void makeParentRoleValid()
Make parent role valid.

Specified by:
makeParentRoleValid in interface NonTerminalProgramElement
Overrides:
makeParentRoleValid in class JavaNonTerminalProgramElement

replaceChild

public boolean replaceChild(ProgramElement p,
                            ProgramElement q)
Replace a single child in the current node. The child to replace is matched by identity and hence must be known exactly. The replacement element can be null - in that case, the child is effectively removed. The parent role of the new child is validated, while the parent link of the replaced child is left untouched.

Specified by:
replaceChild in interface NonTerminalProgramElement
Parameters:
p - the old child.
p - the new child.
Returns:
true if a replacement has occured, false otherwise.
Throws:
java.lang.ClassCastException - if the new child cannot take over the role of the old one.

getFirstElement

public SourceElement getFirstElement()
Description copied from class: JavaSourceElement
Finds the source element that occurs first in the source. The default implementation returns this element, which is correct for all terminal program elements, and many non terminals such as statements and prefixed operators.

Specified by:
getFirstElement in interface SourceElement
Overrides:
getFirstElement in class JavaSourceElement
Returns:
the first source element in the syntactical representation of this element, may be equals to this element.
See Also:
JavaSourceElement.toSource(), JavaSourceElement.getStartPosition()

getLastElement

public SourceElement getLastElement()
Description copied from class: JavaSourceElement
Finds the source element that occurs last in the source. The default implementation returns this element, which is correct for all terminal program elements, and many non terminals such as statements and prefixed operators.

Specified by:
getLastElement in interface SourceElement
Overrides:
getLastElement in class JavaSourceElement
Returns:
the last source element in the syntactical representation of this element, may be equals to this element.
See Also:
JavaSourceElement.toSource(), JavaSourceElement.getEndPosition()

getName

public java.lang.String getName()
Get name of the unit. The name is empty if no data location is set; otherwise, the name of the current data location is returned.

Returns:
the name of this compilation unit.
See Also:
getDataLocation()

getASTParent

public NonTerminalProgramElement getASTParent()
A compilation unit has no syntactical parent

Specified by:
getASTParent in interface ProgramElement
Returns:
the parent node in the syntax tree.

getChildCount

public int getChildCount()
Returns the number of children of this node.

Specified by:
getChildCount in interface NonTerminalProgramElement
Returns:
an int giving the number of children of this node

getChildAt

public ProgramElement getChildAt(int index)
Returns the child at the specified index in this node's "virtual" child array

Specified by:
getChildAt in interface NonTerminalProgramElement
Parameters:
index - an index into this node's "virtual" child array
Returns:
the program element at the given position
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is out of bounds

getChildPositionCode

public int getChildPositionCode(ProgramElement child)
Description copied from interface: NonTerminalProgramElement
Returns the positional code of the given child, or -1 if there is no such child. The result contains an encoding of the relative position of the child as well as the role it has been playing in this parent element. This information is required internally for proper undo of transformations and is to be delivered to the detached method of the ChangeHistory.

Specified by:
getChildPositionCode in interface NonTerminalProgramElement
Parameters:
child - the exact child to look for.
Returns:
the positional code of the given child, or -1.
See Also:
ChangeHistory.detached(recoder.java.ProgramElement, recoder.java.NonTerminalProgramElement, int)

getDataLocation

public DataLocation getDataLocation()
Gets the current data location.

Returns:
the data location.

setDataLocation

public void setDataLocation(DataLocation location)
Sets the current data location. If the data location has been null , the location also becomes the new original location.

Parameters:
location - a data location.

getOriginalDataLocation

public DataLocation getOriginalDataLocation()
Gets the original data location.

Returns:
the original data location.

getImports

public ASTList<Import> getImports()
Get imports.

Returns:
the import mutable list.

setImports

public void setImports(ASTList<Import> list)
Set imports.

Parameters:
list - an import mutable list.

getPackageSpecification

public PackageSpecification getPackageSpecification()
Get package specification.

Returns:
the package specification.

setPackageSpecification

public void setPackageSpecification(PackageSpecification p)
Set package specification.

Parameters:
p - a package specification.

getTypeDeclarationCount

public int getTypeDeclarationCount()
Get the number of type declarations in this container.

Specified by:
getTypeDeclarationCount in interface TypeDeclarationContainer
Returns:
the number of type declarations.

getTypeDeclarationAt

public TypeDeclaration getTypeDeclarationAt(int index)
Specified by:
getTypeDeclarationAt in interface TypeDeclarationContainer

getDeclarations

public ASTList<TypeDeclaration> getDeclarations()
Get declarations.

Returns:
the type declaration mutable list.

setDeclarations

public void setDeclarations(ASTList<TypeDeclaration> list)
Set declarations.

Parameters:
list - a type declaration mutable list.

getPrimaryTypeDeclaration

public TypeDeclaration getPrimaryTypeDeclaration()
Gets the primary type declaration of the compilation unit. The primary declaration is the first declaration of the unit, or the single public declaration. If there is no unambiguous primary declaration, this method returns null.


isDefinedScope

public boolean isDefinedScope()
Description copied from interface: ScopeDefiningElement
Check if the scope has been set up.

Specified by:
isDefinedScope in interface ScopeDefiningElement

setDefinedScope

public void setDefinedScope(boolean defined)
Description copied from interface: ScopeDefiningElement
Sets the scope to be defined or undefined. If set to defined, the scope cache becomes initialized. If set to undefined, the scope cache becomes erased.

Specified by:
setDefinedScope in interface ScopeDefiningElement

getTypesInScope

public java.util.List<ClassType> getTypesInScope()
Specified by:
getTypesInScope in interface TypeScope

getTypeInScope

public ClassType getTypeInScope(java.lang.String name)
Specified by:
getTypeInScope in interface TypeScope

addTypeToScope

public void addTypeToScope(ClassType type,
                           java.lang.String name)
Specified by:
addTypeToScope in interface TypeScope

removeTypeFromScope

public void removeTypeFromScope(java.lang.String name)
Specified by:
removeTypeFromScope in interface TypeScope

accept

public void accept(SourceVisitor v)
Description copied from interface: SourceElement
Receive a visitor, for instance a pretty printer.

Specified by:
accept in interface SourceElement
Parameters:
v - a source visitor.

RECODER 0.92