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

org.snapscript.tree.define.StaticConstantCollector Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.define;

import static org.snapscript.core.Reserved.TYPE_CLASS;

import java.util.List;
import java.util.Set;

import org.snapscript.core.Context;
import org.snapscript.core.ModifierType;
import org.snapscript.core.module.Module;
import org.snapscript.core.property.Property;
import org.snapscript.core.property.PropertyValue;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.ScopeState;
import org.snapscript.core.type.Type;
import org.snapscript.core.type.TypeExtractor;

public class StaticConstantCollector {

   private final StaticConstantIndexer indexer;
   
   public StaticConstantCollector() {
      this.indexer = new StaticConstantIndexer(TYPE_CLASS);
   }
   
   public void collect(Type type) throws Exception {
      Module module = type.getModule();
      Context context = module.getContext();
      TypeExtractor extractor = context.getExtractor();
      Set types = extractor.getTypes(type); // get hierarchy
      
      if(!types.isEmpty()) {
         Set names = indexer.index(type);
         Scope scope = type.getScope();
         ScopeState state = scope.getState();
   
         for(Type next : types) {
            if(next != type) {
               List properties = next.getProperties();
               
               for(Property property : properties) {
                  String name = property.getName();
                  String alias = property.getAlias();
                  int modifiers = property.getModifiers();
                  
                  if(ModifierType.isStatic(modifiers)) {
                     PropertyValue value = new PropertyValue(property, null, name);
                     
                     if(names.add(alias)) { // ensure only supers are added
                        state.addValue(alias, value);
                     }
                  }
               }
            }
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy