org.snapscript.tree.define.FunctionPropertyGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree.define;
import static org.snapscript.core.ModifierType.ABSTRACT;
import static org.snapscript.core.ModifierType.OVERRIDE;
import static org.snapscript.core.Reserved.PROPERTY_GET;
import static org.snapscript.core.Reserved.PROPERTY_SET;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.snapscript.core.Context;
import org.snapscript.core.Module;
import org.snapscript.core.Type;
import org.snapscript.core.function.AccessorProperty;
import org.snapscript.core.function.Function;
import org.snapscript.core.function.Parameter;
import org.snapscript.core.function.Signature;
import org.snapscript.core.index.PropertyNameExtractor;
import org.snapscript.core.property.Property;
import org.snapscript.core.stack.ThreadStack;
public class FunctionPropertyGenerator {
private static final String[] PREFIXES = { PROPERTY_GET, PROPERTY_SET };
private static final int MODIFIERS = OVERRIDE.mask | ABSTRACT.mask;
private final Set done;
public FunctionPropertyGenerator() {
this.done = new HashSet();
}
public void generate(Type type) throws Exception {
List functions = type.getFunctions();
List properties = type.getProperties();
for(Property property : properties) {
String name = property.getName();
if(name != null) {
done.add(name);
}
}
Module module = type.getModule();
Context context = module.getContext();
ThreadStack stack = context.getStack();
for(Function function : functions) {
Signature signature = function.getSignature();
List names = signature.getParameters();
int modifiers = function.getModifiers();
int count = names.size();
if(count == 0) {
String name = extract(function);
if(done.add(name)) {
FunctionAccessor accessor = new FunctionAccessor(function, stack);
AccessorProperty property = new AccessorProperty(name, type, null, accessor, modifiers & ~MODIFIERS);
properties.add(property);
}
}
}
}
private String extract(Function function) throws Exception {
String name = function.getName();
for(String prefix : PREFIXES) {
String property = PropertyNameExtractor.getProperty(name, prefix);
if(property != null) {
return property;
}
}
return name;
}
}