Package sc.lang.java

Class JavaModel

  • All Implemented Interfaces:
    IChangeable, INameContext, ILanguageModel, ISemanticNode, IUserDataNode, IFileProcessorResult, ILifecycle
    Direct Known Subclasses:
    JSModel, sc.lang.sc.SCModel

    public class JavaModel
    extends JavaSemanticNode
    implements ILanguageModel, INameContext, IChangeable, IUserDataNode
    The semantic node corresponding to a Java file. Look for "JavaModel" in the sc.lang.JavaLanguage grammar to find how this is created from code. It's semantic properties (those set by the parser) are packageDef, imports, and types. It's extended by other languages that can convert to Java (e.g. SCModel, JSModel, Template). This class does a lot. It manages it's dependencies on other files for faster incremental builds. It can update a type index for IDEs to bootstrap type system searches. It supports a basic transformation capability to pre-process the Java code into a different format. It can refresh itself from the file system - updating any changes using the dynamic runtime.
    • Field Detail

      • packageDef

        public Package packageDef
        The package of the Java model if any
      • types

        public SemanticNodeList<TypeDeclaration> types
        The set of types defined in this file. Normally there's just one but Java does let you specify multiple types per file at the top-level.
      • layer

        public transient Layer layer
      • userDataMapped

        public transient boolean userDataMapped
        A flag used in the IDE when the userData has been mapped
      • replacesModel

        public transient JavaModel replacesModel
        During an add layer, this stores the old model - the one, this model replaced so we know how to do the refresh
      • replacedByModel

        public transient JavaModel replacedByModel
        This is set just when we start reloading the new model to the new model. We can't add the new types into the index until they are initialized but in some cases we need to be aware of the new model before then, like in building the templates properly
      • removed

        public transient boolean removed
      • reparsed

        public transient boolean reparsed
        Has this model been reparsed since this flag was last checked
      • disableTypeErrors

        public transient boolean disableTypeErrors
      • inTransform

        public transient boolean inTransform
      • hasErrors

        public transient boolean hasErrors
      • isLayerModel

        public transient boolean isLayerModel
      • temporary

        public transient boolean temporary
        Set to true for models which are not part of the type system - i.e. documentation, etc.
      • prependLayerPackage

        public transient boolean prependLayerPackage
        Set to false to ignore the layer's package in determining the type (e.g. WEB-INF/web.xml)
      • extraFiles

        public transient java.util.List<sc.lang.java.JavaModel.ExtraFile> extraFiles
        Stores contents of any files attached to this model, e.g. the gwt module templates associated with a module type
      • srcFiles

        public transient java.util.List<SrcEntry> srcFiles
      • lastModifiedTime

        public transient long lastModifiedTime
        Set so we can keep do incremental refreshes
      • lastStartedTime

        public transient long lastStartedTime
        Set to the timestamp of the time the model was last started - TODO: remove this? We used to use it to avoid restarting the model unnecessarily
      • lastAccessTime

        public transient long lastAccessTime
        Set to the timestamp of the time the model was last used - to determine when to flush it from the cache for the IDE
      • needsRestart

        public transient boolean needsRestart
        Set to true during the build process if this model needs to stopped when it's reinitialized
      • needsReparse

        public transient boolean needsReparse
        Set to true if we fail to edit this model at some point - used by the IDE to mark models which failed to reparse properly
      • modifiedModel

        public transient JavaModel modifiedModel
      • customResolver

        public transient ICustomResolver customResolver
        If you want to parse and start a model but insert your own name resolver which runs before the normal system's type look, set this property.
      • initPackage

        protected transient boolean initPackage
      • mergeDeclaration

        public transient boolean mergeDeclaration
      • transformedModel

        public transient JavaModel transformedModel
      • transformedInLayer

        public transient Layer transformedInLayer
      • nonTransformedModel

        public transient JavaModel nonTransformedModel
        If this is a transformed model, this points back to the original
      • resultSuffix

        public transient java.lang.String resultSuffix
        For models defined by a template, stores the suffix from the last template in the model. Valid in the transformed model only.
      • initializedInLayer

        public transient Layer initializedInLayer
      • added

        public transient boolean added
        Has this model been added to the type system
      • unsavedModel

        public transient boolean unsavedModel
        Is this a new model which has not yet been saved or a model whose memory representation is more recent than the file system
      • resolveInLayer

        public transient boolean resolveInLayer
      • errorMessages

        public transient java.util.List<ModelError> errorMessages
      • warningMessages

        public transient java.lang.StringBuilder warningMessages
    • Constructor Detail

      • JavaModel

        public JavaModel()
    • Method Detail

      • switchLayers

        public void switchLayers​(Layer l)
      • addExternalReference

        public void addExternalReference​(java.lang.String type)
      • initPackageAndImports

        protected void initPackageAndImports()
      • addTypeDeclaration

        public void addTypeDeclaration​(TypeDeclaration td)
      • getChildTypeName

        public java.lang.String getChildTypeName​(BodyTypeDeclaration bodyTypeDeclaration,
                                                 java.lang.String useTypeName)
      • modelChanged

        public void modelChanged()
        Called after reparse so we can invalidate the member cache and member caches that reference this type explicitly
      • updateAccessTimeForRefs

        public void updateAccessTimeForRefs​(boolean updatePeers)
      • getErrorMessagesAsString

        public java.lang.String getErrorMessagesAsString()
      • getImportedName

        public java.lang.String getImportedName​(java.lang.String name)
      • isWildcardImport

        public boolean isWildcardImport​(java.lang.String name)
      • findMatchingGlobalNames

        public boolean findMatchingGlobalNames​(java.lang.String prefix,
                                               java.util.Set<java.lang.String> candidates,
                                               boolean annotTypes,
                                               int max)
      • findMatchingGlobalNames

        public boolean findMatchingGlobalNames​(java.lang.String prefix,
                                               java.lang.String prefixPkgName,
                                               java.lang.String prefixBaseName,
                                               java.util.Set<java.lang.String> candidates,
                                               boolean annotTypes,
                                               int max)
      • copyImports

        public void copyImports​(JavaModel model)
      • addImport

        public void addImport​(java.lang.String name)
      • changePackage

        public void changePackage​(Package newPackage)
      • getTypeDeclaration

        public TypeDeclaration getTypeDeclaration​(java.lang.String typeName)
        Returns a type declaration defined in this model
      • findTypeDeclaration

        public java.lang.Object findTypeDeclaration​(java.lang.String typeName,
                                                    boolean addExternalReference)
      • findTypeDeclaration

        public java.lang.Object findTypeDeclaration​(java.lang.String typeName,
                                                    boolean addExternalReference,
                                                    boolean srcOnly)
      • getClass

        public java.lang.Object getClass​(java.lang.String className,
                                         boolean useImports,
                                         boolean compiledOnly)
      • getClass

        public java.lang.Object getClass​(java.lang.String className,
                                         boolean useImports,
                                         Layer refLayer,
                                         boolean layerResolve,
                                         boolean forceClass,
                                         boolean compiledOnly)
      • getPackagePrefixDir

        public java.lang.String getPackagePrefixDir()
      • addTypeDeclaration

        public void addTypeDeclaration​(java.lang.String typeName,
                                       TypeDeclaration type)
      • getExternalReferences

        public java.util.Set<java.lang.String> getExternalReferences()
      • addDependentFiles

        public void addDependentFiles​(java.util.List<SrcEntry> ent)
      • getProcessedFileId

        public java.lang.String getProcessedFileId()
        Returns the type name plus the suffix of the generated file for uniqueness
        Specified by:
        getProcessedFileId in interface IFileProcessorResult
      • getPostBuildFileId

        public java.lang.String getPostBuildFileId()
        Description copied from interface: IFileProcessorResult
        Returns the unique name of the file this type generates in the post-build process so we only generate that file once in a given system. This is different than the processedFileId for files which are both types and files, e.g. schtml files. It allows you to override an schtml template in a different layer package. At the type level, we'll maintain two unique types but the generated files will replace each other to make it easier to merge web roots with layers that are in different packages.
        Specified by:
        getPostBuildFileId in interface IFileProcessorResult
      • postBuild

        public void postBuild​(java.lang.String buildDir)
        Called for templates after the build has been completed.
      • removeSrcFiles

        public void removeSrcFiles()
      • cloneSrcFiles

        public java.util.List<SrcEntry> cloneSrcFiles()
      • setSrcFile

        public void setSrcFile​(SrcEntry srcFile)
      • setRelDirPath

        public void setRelDirPath​(java.lang.String relDirPath)
      • getRelDirPath

        public java.lang.String getRelDirPath()
        Returns the directory containing this file in a path format (i.e. "a.b").
        Specified by:
        getRelDirPath in interface ILanguageModel
      • getPathInfo

        public java.lang.String getPathInfo()
      • setLayerTypeDeclaration

        public void setLayerTypeDeclaration​(java.lang.Object td)
        Dummy for the sync process
      • getModelType

        public java.lang.Object getModelType()
        Description copied from interface: ILanguageModel
        Same as getModelTypeDeclaration but if you have a template where the root type is a compiled class this method returns the class
        Specified by:
        getModelType in interface ILanguageModel
      • getImplicitTypeDeclaration

        public TypeDeclaration getImplicitTypeDeclaration()
      • getModelTypeName

        public java.lang.String getModelTypeName()
      • getPreviousDeclaration

        public BodyTypeDeclaration getPreviousDeclaration​(java.lang.String fullClassName,
                                                          boolean includeEnums)
      • initReverseDeps

        public void initReverseDeps()
      • isAnnotationModel

        public boolean isAnnotationModel()
      • changedSinceLayer

        public boolean changedSinceLayer​(Layer initLayer,
                                         Layer buildLayer)
      • transformModel

        public boolean transformModel()
      • isTheTransformedModel

        public boolean isTheTransformedModel()
      • getTransformedModel

        public JavaModel getTransformedModel()
        Returns the transformed model for this model.
      • dependenciesChanged

        public boolean dependenciesChanged()
        Hook to reinitiualize any state after one of your base types gets modified after this type has been processed.
      • reinitialize

        public void reinitialize()
        Description copied from interface: ILanguageModel
        Called when dependent files for this type have changed and this type needs to re-evaluate itself from it's original AST. It might happen if the subclass depends on the code in the base class or if the subclass failed to compile and we need to retry things again.
        Specified by:
        reinitialize in interface ILanguageModel
      • getBreakpointStatements

        public java.util.List<ISrcStatement> getBreakpointStatements​(ISrcStatement srcStatement)
        Returns the list of statements generated from this statement that are used when setting breakpoints on this statement.
      • addExtraFile

        public void addExtraFile​(java.lang.String relFilePath,
                                 java.lang.String fileBody)
      • getProcessedFiles

        public java.util.List<SrcEntry> getProcessedFiles​(Layer buildLayer,
                                                          java.lang.String buildSrcDir,
                                                          boolean generate)
        Returns the set of files generated by this model. This includes the pass through or transformed Java file, any dynamic stubs for the main type or inner types, and any extra files added by extra features defined on this model (e.g. the GWT module file)
        Specified by:
        getProcessedFiles in interface IFileProcessorResult
      • cloneTransformedModel

        public void cloneTransformedModel()
      • prepareModelTypeForTransform

        protected void prepareModelTypeForTransform​(boolean generate,
                                                    TypeDeclaration modelType)
      • getProcessedFileName

        public java.lang.String getProcessedFileName​(java.lang.String buildSrcDir)
      • getClassFileName

        public java.lang.String getClassFileName​(java.lang.String buildDir)
      • saveModel

        public void saveModel()
        Save the model source into its original location. We get the layer's main directory and write the model into the source file at that location
        Specified by:
        saveModel in interface ILanguageModel
      • saveModelTextToFile

        public void saveModelTextToFile​(java.lang.String text)
      • updateModel

        public void updateModel​(JavaModel newModel,
                                ExecutionContext ctx,
                                TypeUpdateMode updateMode,
                                boolean updateInstances,
                                UpdateInstanceInfo updateInfo)
        Reload this model from a saved model in a file. Any changes found either will mark the compiled system as "stale" or will update the running application.
      • completeUpdateModel

        public void completeUpdateModel​(JavaModel newModel,
                                        boolean updateRuntime)
      • updateLayeredModel

        public void updateLayeredModel​(ExecutionContext ctx,
                                       boolean updateInstances,
                                       UpdateInstanceInfo updateInfo)
        Adds a new layer onto an existing model.
      • modifiesModel

        public boolean modifiesModel()
        Returns true if this model modifies a type, instead of defining a new type
      • getTransformedResult

        public java.lang.String getTransformedResult()
      • validateSavedModel

        public void validateSavedModel​(boolean finalGen)
      • resolveName

        public java.lang.Object resolveName​(java.lang.String name,
                                            boolean create,
                                            boolean returnTypes)
        Specified by:
        resolveName in interface INameContext
      • resolveName

        public java.lang.Object resolveName​(java.lang.String name,
                                            boolean create,
                                            boolean addExternalReference,
                                            boolean returnTypes)
        This is called when evaluating an expression and we want to evaluate an identifier expression. It first looks for a local type in this file, then will go resolve a global one from the system.
      • addGlobalObject

        public void addGlobalObject​(java.lang.String name,
                                    java.lang.Object obj)
      • transformPackage

        public boolean transformPackage()
      • addModelMergingComment

        public void addModelMergingComment​(ParentParseNode baseCommentNode)
      • setResultSuffix

        public void setResultSuffix​(java.lang.String suffix)
      • getResultSuffix

        public java.lang.String getResultSuffix()
      • getUnresolvedModifiedModel

        public JavaModel getUnresolvedModifiedModel()
      • isModifyModel

        public boolean isModifyModel()
      • syncAutoImports

        public void syncAutoImports()
        The autoImports get registered by all layered models with the same type. Since we can only resolve imports in the layered structure, during transform we need to aggregate them all in the model so it can find any types that get merged into it
      • enableExtensions

        public boolean enableExtensions()
        Overridden in SCModel to true
      • findMethod

        public java.lang.Object findMethod​(java.lang.String name,
                                           java.util.List<? extends java.lang.Object> params,
                                           java.lang.Object fromChild,
                                           java.lang.Object refType,
                                           boolean staticOnly,
                                           java.lang.Object inferredType)
        Overrides:
        findMethod in class JavaSemanticNode
      • definesMethod

        public java.lang.Object definesMethod​(java.lang.String name,
                                              java.util.List<?> types,
                                              ITypeParamContext ctx,
                                              java.lang.Object refType,
                                              boolean isTransformed,
                                              boolean staticOnly,
                                              java.lang.Object inferredType,
                                              java.util.List<JavaType> methodTypeArgs)
        Overrides:
        definesMethod in class JavaSemanticNode
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • addBindDependency

        public void addBindDependency​(TypeDeclaration toType,
                                      java.lang.String property,
                                      TypeDeclaration fromType,
                                      boolean referenceOnly)
      • addDynMethod

        public void addDynMethod​(TypeDeclaration toType,
                                 java.lang.String methodName,
                                 java.lang.String paramSig,
                                 TypeDeclaration fromType)
      • cleanStaleEntries

        public void cleanStaleEntries​(java.util.HashSet<java.lang.String> changedTypes)
      • getReverseDepsName

        public java.lang.String getReverseDepsName​(java.lang.String buildSrcDir)
      • saveReverseDeps

        public void saveReverseDeps​(java.lang.String buildSrcDir)
      • cleanReverseDeps

        public void cleanReverseDeps​(Layer buildLayer)
      • readReverseDeps

        public boolean readReverseDeps​(Layer buildLayer)
      • getPrevTypeGroupMembers

        public java.util.List<java.lang.String> getPrevTypeGroupMembers​(Layer buildLayer,
                                                                        java.lang.String typeGroupName)
      • setPrevTypeGroupMembers

        public void setPrevTypeGroupMembers​(java.lang.String buildSrcDir,
                                            java.lang.String typeGroupName,
                                            java.util.List<TypeGroupMember> newTypeGroupMembers)
      • setCommandInterpreter

        public void setCommandInterpreter​(AbstractInterpreter interp)
        Associates this model with a command interpreter for resolving the cmd object. Must propagate this up the modified hierarchy since the defineType call gets redirected by replacedByType.
      • resolveModel

        public JavaModel resolveModel()
        Resolves through any deleted copies of the model only. Does not go up the layer stack and return the most specific model for this type.
        Specified by:
        resolveModel in interface ILanguageModel
      • resolveLastModel

        public JavaModel resolveLastModel()
      • addDependentTypes

        public void addDependentTypes​(java.util.Set<java.lang.Object> resultTypes,
                                      JavaSemanticNode.DepTypeCtx mode)
      • setErrorHandler

        public void setErrorHandler​(IMessageHandler errorHandler)
      • reportError

        public void reportError​(java.lang.String error,
                                ISemanticNode source)
      • reportWarning

        public void reportWarning​(java.lang.String error,
                                  ISemanticNode source)
      • clearTransformed

        public void clearTransformed()
      • getModifiedByModel

        public JavaModel getModifiedByModel()
      • refreshModelText

        public void refreshModelText()
      • refreshHTMLModelText

        public void refreshHTMLModelText()
      • refreshGeneratedText

        public void refreshGeneratedText()
      • clearModelText

        public void clearModelText()
      • clearHTMLModelText

        public void clearHTMLModelText()
      • clearGeneratedText

        public void clearGeneratedText()
      • markChanged

        public void markChanged()
        Sends a value changed event using the data binding system. This lets code which edits models add listeners directly on a model and receive notifications when they change.
      • markTransformedChanged

        public void markTransformedChanged()
      • resolve

        public JavaModel resolve()
        After a JavaModel is replaced, this resolves the model which replaced it
      • disposeInstances

        public void disposeInstances()
        Find all instances for all types in this model and dispose of them
      • deepCopy

        public ISemanticNode deepCopy​(int options,
                                      java.util.IdentityHashMap<java.lang.Object,​java.lang.Object> oldNewMap)
        Description copied from interface: ISemanticNode
        Performs a deep copy of the semantic node tree. Subclasses should do their own copy implementation to decide what else to copy. Uses the copy options flags above. The default, with 0, just copies the semantic node information so you get a new tree like's it parsed but without parse node info. Use that mode only if you want to throw away the original source and regenerate it all from scratch. Otherwise, you can copy the parse tree as well. If you want to copy the semantic node's initial resolved state - including it's init/start, etc. and whatever the subclass adds to that copy, use CopyState or CopyNormal - for both parse nodes and state.
        Specified by:
        deepCopy in interface ISemanticNode
        Overrides:
        deepCopy in class SemanticNode
      • setMergeDeclaration

        public void setMergeDeclaration​(boolean val)
      • stop

        public void stop​(boolean stopModified)
      • setNeedsModelText

        public void setNeedsModelText​(boolean val)
      • getNeedsModelText

        public boolean getNeedsModelText()
      • setNeedsHTMLModelText

        public void setNeedsHTMLModelText​(boolean val)
      • getNeedsHTMLModelText

        public boolean getNeedsHTMLModelText()
      • getModelText

        public java.lang.String getModelText()
      • getHTMLModelText

        public java.lang.String getHTMLModelText()
      • setCachedModelText

        public void setCachedModelText​(java.lang.String str)
      • getCachedModelText

        public java.lang.String getCachedModelText()
      • setCachedHTMLModelText

        public void setCachedHTMLModelText​(java.lang.String str)
      • getCachedHTMLModelText

        public java.lang.String getCachedHTMLModelText()
      • setCachedGeneratedSCText

        public void setCachedGeneratedSCText​(java.lang.String str)
      • getCachedGeneratedSCText

        public java.lang.String getCachedGeneratedSCText()
      • setCachedGeneratedClientJavaText

        public void setCachedGeneratedClientJavaText​(java.lang.String str)
      • getCachedGeneratedClientJavaText

        public java.lang.String getCachedGeneratedClientJavaText()
      • setNeedsGeneratedText

        public void setNeedsGeneratedText​(boolean val)
      • getNeedsGeneratedText

        public boolean getNeedsGeneratedText()
      • getGeneratedText

        public java.lang.String getGeneratedText()
      • getGeneratedJSText

        public java.lang.String getGeneratedJSText()
      • getGeneratedSCText

        public java.lang.String getGeneratedSCText()
      • getGeneratedClientJavaText

        public java.lang.String getGeneratedClientJavaText()
      • getExistsInJSRuntime

        public boolean getExistsInJSRuntime()
      • setExistsInJSRuntime

        public void setExistsInJSRuntime​(boolean b)
      • setCachedGeneratedText

        public void setCachedGeneratedText​(java.lang.String str)
      • getCachedGeneratedText

        public java.lang.String getCachedGeneratedText()
      • setCachedGeneratedJSText

        public void setCachedGeneratedJSText​(java.lang.String str)
      • getCachedGeneratedJSText

        public java.lang.String getCachedGeneratedJSText()
      • getDependenciesChanged

        public boolean getDependenciesChanged​(Layer genLayer,
                                              java.util.Set<java.lang.String> changedTypes,
                                              boolean processJava)
      • refreshNode

        public JavaModel refreshNode()
        Description copied from class: JavaSemanticNode
        Needs to be implemented for IDE support on nodes which can be referenced outside of the same file. If the model in which this references lives has been removed (or really replaced by another version of the same model), we need to find the node in the other model that corresponds to this node and return it.
        Specified by:
        refreshNode in interface ISemanticNode
        Overrides:
        refreshNode in class JavaSemanticNode
      • markAsTemporary

        public void markAsTemporary()
        Called for models which are not part of the type system for the runtime (e.g. documentation files, temporary nodes created by the interpreter or those created during the editing process to apply a batch of changes) Sets temporaryType = true on all children types
        Specified by:
        markAsTemporary in interface ILanguageModel
      • sameModel

        public boolean sameModel​(ILanguageModel other)
        Description copied from interface: ILanguageModel
        An optimization - returns true if the other is the exact same model, Returns false if you are not sure.
        Specified by:
        sameModel in interface ILanguageModel
      • displayTypeError

        public boolean displayTypeError​(java.lang.String... args)
        Description copied from class: JavaSemanticNode
        Used for errors involving type resolution. These errors can be disabled during certain operations like transform
        Overrides:
        displayTypeError in class JavaSemanticNode
      • getDisableTypeErrors

        public boolean getDisableTypeErrors()
      • updateTypeName

        public void updateTypeName​(java.lang.String oldTypeName,
                                   java.lang.String newTypeName,
                                   boolean renameFile)
      • fileRenamed

        public void fileRenamed​(SrcEntry oldSrcEnt,
                                SrcEntry newSrcEnt)
        Called when we detect that a source file has changed ffom outside the system. This handles updating the srcFile on this model and updating the indexes in teh layered system that depend on the file name. NOTE: this does not change the type name of the model type which typically must correspond with the type but during the editing process, sometimes these become different so we need to handle the update independently.
      • getComment

        public java.lang.String getComment()
      • getClassComment

        public java.lang.String getClassComment()
      • getLineIndexFile

        public java.io.File getLineIndexFile​(java.lang.String buildSrcDir)
      • updateFileLineIndex

        public void updateFileLineIndex​(java.lang.String transformedResult,
                                        java.lang.String buildSrcDir)
      • readFileLineIndex

        public GenFileLineIndex readFileLineIndex​(java.lang.String buildSrcDir)
      • getGenLinesForSrcStatement

        public java.util.List<java.lang.Integer> getGenLinesForSrcStatement​(ISrcStatement st,
                                                                            java.lang.String buildDir)
      • getSrcStatementForGenLine

        public ISrcStatement getSrcStatementForGenLine​(int lineNum,
                                                       java.lang.String buildDir)
      • generateFileLineIndex

        public GenFileLineIndex generateFileLineIndex​(java.lang.String transformedResult,
                                                      java.lang.String buildSrcDir)
      • getModelDefaultModifier

        public java.lang.Object getModelDefaultModifier()
      • restoreParseNode

        public boolean restoreParseNode()
        Description copied from interface: ILanguageModel
        Models can be parsed once and then serialized and de-serialized much faster. When a model is deserialized, we can restore the parse nodes using this operation. When the model is cached, we can either cache the parse-node tree as well or not. Either way, it's faster to restore the parse-node tree with the existing model than to parse it again from scratch.
        Specified by:
        restoreParseNode in interface ILanguageModel
        Overrides:
        restoreParseNode in class SemanticNode