All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.agent.debug.InstanceScopeNode Maven / Gradle / Ivy


package org.snapscript.agent.debug;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.TypeTraverser;
import org.snapscript.core.Value;
import org.snapscript.core.define.Instance;
import org.snapscript.core.property.Property;

public class InstanceScopeNode implements ScopeNode {
   
   private final TypeTraverser extractor;
   private final ScopeNodeBuilder builder;
   private final List nodes;
   private final Scope scope;
   private final String path;
   private final String name;
   private final int depth;
   
   public InstanceScopeNode(ScopeNodeBuilder builder, Instance scope, String path, String name, int depth) {
      this.extractor = new TypeTraverser();
      this.nodes = new ArrayList();
      this.builder = builder;
      this.scope = scope;
      this.depth = depth;
      this.name = name;
      this.path = path;
   }
   
   @Override
   public int getDepth() {
      return depth;
   }
   
   @Override
   public String getName() {
      return name;
   }
   
   @Override
   public String getPath() {
      return path;
   }

   @Override
   public List getNodes() {
      if(nodes.isEmpty()) {
         State state = scope.getState();
         Iterator names = state.iterator();
         Type type = scope.getType();
         Set types = extractor.findHierarchy(type);
         
         if(names.hasNext() && !types.isEmpty()) {
            Set include = new HashSet();
            
            for(Type base : types) {
               List fields = base.getProperties();
               
               for(Property property : fields) {
                  String name = property.getName();
                  include.add(name);
               }
            }
            while(names.hasNext()) {
               String name = names.next();
               
               if(include.contains(name)) {
                  Value value = state.get(name); 
                  Object object = value.getValue();
                  int modifiers = value.getModifiers();
                  ScopeNode node = builder.createNode(path + "." + name, name, object, modifiers, depth);
                  
                  if(node != null) {
                     nodes.add(node);
                  }
               }
            }
         }
      }
      return nodes;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy