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

org.snapscript.studio.agent.debug.ObjectScopeNode Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.debug;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.snapscript.core.type.index.ModifierConverter;
import org.snapscript.studio.agent.debug.ScopeNode;
import org.snapscript.studio.agent.debug.ScopeNodeBuilder;

public class ObjectScopeNode implements ScopeNode {
   
   private final ModifierConverter converter;
   private final ScopeNodeBuilder builder;
   private final List nodes;
   private final Object object;
   private final String alias;
   private final String path;
   private final String name;
   private final int depth;
   
   public ObjectScopeNode(ScopeNodeBuilder builder, Object object, String path, String name, String alias, int depth) {
      this.converter = new ModifierConverter();
      this.nodes = new ArrayList();
      this.builder = builder;
      this.object = object;
      this.depth = depth;
      this.alias = alias;
      this.path = path;
      this.name = name;
   }
   
   @Override
   public int getDepth() {
      return depth;
   }
   
   @Override
   public String getName() {
      return name;
   }
   
   @Override
   public String getAlias() {
      return alias;
   }
   
   @Override
   public String getPath() {
      return path;
   }

   @Override
   public List getNodes() {
      if(nodes.isEmpty()) {
         Class type = object.getClass();
         Map fields = getFields(type);
         Set names = fields.keySet();
         
         if(!names.isEmpty()) {
            for(String name : names) {
               try {
                  Field field = fields.get(name);
                  Object value = field.get(object);
                  int modifiers = converter.convert(field);
                  ScopeNode node = builder.createNode(path + "." + name, name, name, value, modifiers, depth);
                  
                  if(node != null) {
                     nodes.add(node);
                  }
               } catch(Exception e) {
                  e.printStackTrace();
               }
            }
         }
      }
      return nodes;
   }
   
   private Map getFields(Class type) {
      Map accessors = new HashMap();
      
      while(type != Object.class) {
         Field[] fields = type.getDeclaredFields();
         
         for(int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String name = field.getName();
            int modifiers = field.getModifiers();
            
            if(!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
               field.setAccessible(true);
               accessors.put(name, field);
            }
         }
         type = type.getSuperclass();
      }
      return accessors;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy