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

org.snapscript.tree.ModifierAccessVerifier Maven / Gradle / Ivy

package org.snapscript.tree;

import java.util.Set;

import org.snapscript.core.Context;
import org.snapscript.core.module.Module;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.type.Type;
import org.snapscript.core.type.TypeExtractor;

public class ModifierAccessVerifier {
   
   public ModifierAccessVerifier() {
      super();
   }

   public boolean isAccessible(Scope scope, Type owner) {
      Type caller = scope.getType();
      
      if(caller != null) {
         return isAccessible(caller, owner);
      }
      return false;
   }
   
   public boolean isAccessible(Type caller, Type owner) {
      if(caller != null && owner != null) {
         if(isSuper(caller, owner)) {
            return true;
         }
         if(isCompatible(caller, owner)) {
            if(isEnclosing(caller, owner)) {
               return true;
            }
            if(isEnclosing(owner, caller)) {
               return true;
            }
         }
      }
      return false;
   }
   
   private boolean isSuper(Type caller, Type owner) {
      if(caller != owner) {
         Module module = caller.getModule();
         Context context = module.getContext();
         TypeExtractor extractor = context.getExtractor();
         Set types = extractor.getTypes(caller); // what is this scope
         
         return types.contains(owner);
      }
      return true;
   }
   
   private boolean isCompatible(Type caller, Type owner) { // same module
      Module actual = caller.getModule();
      Module require = owner.getModule(); 
      
      return actual == require;
   }
   
   private boolean isEnclosing(Type parent, Type child) {
      String outer = parent.getName();
      String inner = child.getName();
      
      if(inner.startsWith(outer)) {
         int length = outer.length();
         int index = inner.indexOf('$', length);
         
         return index == length;
      }
      return false;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy