use of com.oracle.truffle.api.object.DynamicObjectFactory in project graal by oracle.
the class BuildArgumentsTest method testCreate.
@Test
public void testCreate() {
final DynamicObjectFactory factory = LAYOUT.createPrepareArgumentsShape(14);
final DynamicObject object = factory.newInstance(LAYOUT.build(1, 2));
Assert.assertEquals(14, LAYOUT.getShapeProperty(object));
Assert.assertEquals(1, LAYOUT.getA(object));
Assert.assertEquals(2, LAYOUT.getB(object));
}
use of com.oracle.truffle.api.object.DynamicObjectFactory in project graal by oracle.
the class LayoutParser method parseGetter.
private void parseGetter(ExecutableElement methodElement) {
if (methodElement.getParameters().size() != 1) {
processor.reportError(methodElement, "@Layout getter methods must have just one parameter");
}
final VariableElement parameter = methodElement.getParameters().get(0);
final TypeMirror parameterType = parameter.asType();
final String parameterName = parameter.getSimpleName().toString();
final boolean isShapeGetter;
final boolean isObjectTypeGetter;
final String expectedParameterName;
if (isSameType(parameterType, DynamicObject.class)) {
isShapeGetter = false;
isObjectTypeGetter = false;
expectedParameterName = "object";
} else if (isSameType(parameterType, DynamicObjectFactory.class)) {
isShapeGetter = true;
isObjectTypeGetter = false;
expectedParameterName = "factory";
} else if (isSameType(parameterType, ObjectType.class)) {
isShapeGetter = false;
isObjectTypeGetter = true;
expectedParameterName = "objectType";
} else {
isShapeGetter = false;
isObjectTypeGetter = false;
expectedParameterName = null;
processor.reportError(methodElement, "@Layout getter methods must have a parameter of type DynamicObject or, for shape properties, DynamicObjectFactory or ObjectType");
}
if (expectedParameterName != null && !matches(parameterName, expectedParameterName)) {
processor.reportError(methodElement, "@Layout getter method should have a parameter named %s", expectedParameterName);
}
final String propertyName = NameUtils.titleToCamel(methodElement.getSimpleName().toString().substring("get".length()));
final PropertyBuilder property = getProperty(propertyName);
if (isShapeGetter) {
property.setHasShapeGetter(true);
} else if (isObjectTypeGetter) {
property.setHasObjectTypeGetter(true);
} else {
property.setHasGetter(true);
}
setPropertyType(methodElement, property, methodElement.getReturnType());
}
use of com.oracle.truffle.api.object.DynamicObjectFactory in project graal by oracle.
the class LayoutParser method parseSetter.
private void parseSetter(ExecutableElement methodElement) {
if (methodElement.getParameters().size() != 2) {
processor.reportError(methodElement, "@Layout guard methods must have two parameters");
}
final VariableElement parameter = methodElement.getParameters().get(0);
final TypeMirror parameterType = parameter.asType();
final String parameterName = parameter.getSimpleName().toString();
final boolean isShapeSetter;
final String expectedParameterName;
if (isSameType(parameterType, DynamicObject.class)) {
isShapeSetter = false;
expectedParameterName = "object";
} else if (isSameType(parameterType, DynamicObjectFactory.class)) {
isShapeSetter = true;
expectedParameterName = "factory";
} else {
isShapeSetter = false;
expectedParameterName = null;
processor.reportError(methodElement, "@Layout setter methods must have a first parameter of type DynamicObject or, for shape properties, DynamicObjectFactory");
}
if (expectedParameterName != null && !matches(parameterName, expectedParameterName)) {
processor.reportError(methodElement, "@Layout getter method should have a first parameter named %s", expectedParameterName);
}
final VariableElement secondParameter = methodElement.getParameters().get(1);
final String secondParameterName = secondParameter.getSimpleName().toString();
if (!matches(secondParameterName, "value")) {
processor.reportError(methodElement, "@Layout getter method should have a second parameter named value");
}
final boolean isUnsafeSetter = methodElement.getSimpleName().toString().endsWith("Unsafe");
String propertyName = NameUtils.titleToCamel(methodElement.getSimpleName().toString().substring("set".length()));
if (isUnsafeSetter) {
propertyName = propertyName.substring(0, propertyName.length() - "Unsafe".length());
}
final PropertyBuilder property = getProperty(propertyName);
if (isShapeSetter) {
property.setHasShapeSetter(true);
} else {
if (isUnsafeSetter) {
property.setHasUnsafeSetter(isUnsafeSetter);
} else {
property.setHasSetter(true);
}
}
setPropertyType(methodElement, property, methodElement.getParameters().get(1).asType());
}
use of com.oracle.truffle.api.object.DynamicObjectFactory in project graal by oracle.
the class DynamicObjectFactoryTest method testFactory.
@Test
public void testFactory() {
Shape shape = rootShape.defineProperty("x", 1, 0).defineProperty("y", 2, 0);
DynamicObjectFactory factory = shape.createFactory();
try {
factory.newInstance();
Assert.fail();
} catch (AssertionError e) {
Assert.assertEquals("0 arguments given but the factory takes 2: x, y", e.getMessage());
}
try {
factory.newInstance("only one argument");
Assert.fail();
} catch (AssertionError e) {
Assert.assertEquals("1 arguments given but the factory takes 2: x, y", e.getMessage());
}
DynamicObject object = factory.newInstance(3, 4);
Assert.assertEquals(3, object.get("x"));
Assert.assertEquals(4, object.get("y"));
try {
factory.newInstance(1, 2, 3);
Assert.fail();
} catch (AssertionError e) {
Assert.assertEquals("3 arguments given but the factory takes 2: x, y", e.getMessage());
}
}
use of com.oracle.truffle.api.object.DynamicObjectFactory in project graal by oracle.
the class LayoutParser method parseConstructor.
private void parseConstructor(ExecutableElement methodElement) {
hasCreate = true;
checkCreateAndBuilder(methodElement);
List<? extends VariableElement> parameters = methodElement.getParameters();
if (hasShapeProperties) {
if (parameters.isEmpty()) {
processor.reportError(methodElement, "If an @Layout has shape properties the constructor must have parameters");
}
final VariableElement firstParameter = parameters.get(0);
final String firstParameterName = firstParameter.getSimpleName().toString();
if (!matches(firstParameterName, "factory")) {
processor.reportError(firstParameter, "If an @Layout has shape properties, the first parameter of the constructor must be called factory (was %s)", firstParameter.getSimpleName());
}
if (!isSameType(firstParameter.asType(), DynamicObjectFactory.class)) {
processor.reportError(firstParameter, "If an @Layout has shape properties, the first parameter of the constructor must be of type DynamicObjectFactory (was %s)", firstParameter.asType());
}
parameters = parameters.subList(1, parameters.size());
}
addConstructorProperties(methodElement, parameters);
}
Aggregations