Search in sources :

Example 11 with TypeInfo

use of com.google.template.soy.jbcsrc.restricted.TypeInfo in project closure-templates by google.

the class ProtoUtils method getExtensionField.

/**
 * Returns the {@link FieldRef} for the generated {@link Extension} field.
 */
private static FieldRef getExtensionField(FieldDescriptor descriptor) {
    Preconditions.checkArgument(descriptor.isExtension(), "%s is not an extension", descriptor);
    String extensionFieldName = getFieldName(descriptor, false);
    if (descriptor.getExtensionScope() != null) {
        TypeInfo owner = messageRuntimeType(descriptor.getExtensionScope());
        return FieldRef.createPublicStaticField(owner, extensionFieldName, EXTENSION_TYPE);
    }
    // else we have a 'top level extension'
    String containingClass = JavaQualifiedNames.getPackage(descriptor.getFile()) + "." + JavaQualifiedNames.getOuterClassname(descriptor.getFile());
    return FieldRef.createPublicStaticField(TypeInfo.create(containingClass), extensionFieldName, EXTENSION_TYPE);
}
Also used : ByteString(com.google.protobuf.ByteString) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 12 with TypeInfo

use of com.google.template.soy.jbcsrc.restricted.TypeInfo in project closure-templates by google.

the class ProtoUtils method getPutMethod.

/**
 * Returns the {@link MethodRef} for the generated put method for proto map.
 */
private static MethodRef getPutMethod(FieldDescriptor descriptor) {
    Preconditions.checkState(descriptor.isMapField());
    List<FieldDescriptor> mapFields = descriptor.getMessageType().getFields();
    TypeInfo builder = builderRuntimeType(descriptor.getContainingType());
    return MethodRef.createInstanceMethod(builder, new Method("put" + getFieldName(descriptor, true), builder.type(), new Type[] { getRuntimeType(mapFields.get(0)), getRuntimeType(mapFields.get(1)) }));
}
Also used : ProtoUtils.hasJsType(com.google.template.soy.internal.proto.ProtoUtils.hasJsType) Type(org.objectweb.asm.Type) JSType(com.google.protobuf.DescriptorProtos.FieldOptions.JSType) SanitizedType(com.google.template.soy.types.SanitizedType) SoyProtoType(com.google.template.soy.types.SoyProtoType) SoyRuntimeType(com.google.template.soy.jbcsrc.restricted.SoyRuntimeType) ProtoUtils.getJsType(com.google.template.soy.internal.proto.ProtoUtils.getJsType) MapType(com.google.template.soy.types.MapType) SoyType(com.google.template.soy.types.SoyType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) ListType(com.google.template.soy.types.ListType) Method(org.objectweb.asm.commons.Method) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 13 with TypeInfo

use of com.google.template.soy.jbcsrc.restricted.TypeInfo in project closure-templates by google.

the class InnerClasses method registerInnerClassWithGeneratedName.

/**
 * Register the name (or a simpl mangling of it) as an inner class with the given access
 * modifiers.
 *
 * @return A {@link TypeInfo} with the full (possibly mangled) class name
 */
public TypeInfo registerInnerClassWithGeneratedName(String simpleName, int accessModifiers) {
    simpleName = classNames.generateName(simpleName);
    TypeInfo innerClass = outer.innerClass(simpleName);
    innerClassesAccessModifiers.put(innerClass, accessModifiers);
    return innerClass;
}
Also used : TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 14 with TypeInfo

use of com.google.template.soy.jbcsrc.restricted.TypeInfo in project closure-templates by google.

the class InnerClasses method registerInnerClass.

/**
 * Register the given name as an inner class with the given access modifiers.
 *
 * @return A {@link TypeInfo} with the full class name
 */
public TypeInfo registerInnerClass(String simpleName, int accessModifiers) {
    classNames.claimName(simpleName);
    TypeInfo innerClass = outer.innerClass(simpleName);
    innerClassesAccessModifiers.put(innerClass, accessModifiers);
    return innerClass;
}
Also used : TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo)

Example 15 with TypeInfo

use of com.google.template.soy.jbcsrc.restricted.TypeInfo in project closure-templates by google.

the class MemoryClassLoaderTest method testDelegation.

@Test
public void testDelegation() throws Exception {
    // We want to make sure that for generated types, our classloader doesn't delegate to its parent
    // loader
    ClassData parentClass = ExpressionTester.createClass(BooleanInvoker.class, FALSE);
    ClassData childClass = ExpressionTester.createClass(BooleanInvoker.class, TRUE);
    // sanity check,  they have the same fully qualified class name
    assertThat(parentClass.type()).isEqualTo(childClass.type());
    TypeInfo type = childClass.type();
    MemoryClassLoader parentLoader = new MemoryClassLoader(ImmutableList.of(parentClass));
    MemoryClassLoader childLoader = new MemoryClassLoader(parentLoader, ImmutableList.of(childClass));
    BooleanInvoker fromParent = parentLoader.loadClass(type.className()).asSubclass(BooleanInvoker.class).getConstructor().newInstance();
    assertThat(fromParent.invoke()).isFalse();
    BooleanInvoker fromChild = childLoader.loadClass(type.className()).asSubclass(BooleanInvoker.class).getConstructor().newInstance();
    assertThat(fromChild.invoke()).isTrue();
    assertThat(fromChild.getClass()).isNotEqualTo(fromParent.getClass());
}
Also used : BooleanInvoker(com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker) TypeInfo(com.google.template.soy.jbcsrc.restricted.TypeInfo) Test(org.junit.Test)

Aggregations

TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)15 Method (org.objectweb.asm.commons.Method)6 SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)4 ByteString (com.google.protobuf.ByteString)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement.returnExpression (com.google.template.soy.jbcsrc.restricted.Statement.returnExpression)2 JSType (com.google.protobuf.DescriptorProtos.FieldOptions.JSType)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 JavaType (com.google.protobuf.Descriptors.FieldDescriptor.JavaType)1 ProtoUtils.getJsType (com.google.template.soy.internal.proto.ProtoUtils.getJsType)1 ProtoUtils.hasJsType (com.google.template.soy.internal.proto.ProtoUtils.hasJsType)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)1 SoyRuntimeType (com.google.template.soy.jbcsrc.restricted.SoyRuntimeType)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 BooleanInvoker (com.google.template.soy.jbcsrc.restricted.testing.ExpressionTester.BooleanInvoker)1 ListType (com.google.template.soy.types.ListType)1 MapType (com.google.template.soy.types.MapType)1 SanitizedType (com.google.template.soy.types.SanitizedType)1