Example usage for com.google.gwt.core.ext.typeinfo JClassType isFinal

List of usage examples for com.google.gwt.core.ext.typeinfo JClassType isFinal

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.typeinfo JClassType isFinal.

Prototype

boolean isFinal();

Source Link

Usage

From source file:com.sencha.gxt.core.rebind.AbstractCreator.java

License:sencha.com license

/**
 * Builds up the basics of the factory that will generate source. Should be
 * overridden to add imports./*  www .  j av a 2s  . c  om*/
 * 
 * @param factory the factory to configure
 */
protected void configureFactory(ClassSourceFileComposerFactory factory) throws UnableToCompleteException {
    JClassType t = getSupertype();
    if (t.isInterface() != null) {
        factory.addImplementedInterface(getSupertype().getParameterizedQualifiedSourceName());
    } else {
        if (t.isClass() == null) {
            logger.log(Type.ERROR,
                    "Cannot create a subtype of a non-class and non-interface type: " + t.getName());
            throw new UnableToCompleteException();
        }
        if (t.isFinal()) {
            logger.log(Type.ERROR, "Cannot create a subtype of a final class");
            throw new UnableToCompleteException();
        }
        factory.setSuperclass(t.getQualifiedSourceName());
    }
}

From source file:fr.onevu.gwt.uibinder.rebind.JClassTypeAdapter.java

License:Apache License

/**
 * Creates a mock GWT class type for the given Java class.
 *
 * @param clazz the java class//from w w  w .  j  a  va2s  .c  om
 * @return the gwt class
 */
public JClassType adaptJavaClass(final Class<?> clazz) {
    if (clazz.isPrimitive()) {
        throw new RuntimeException("Only classes can be passed to adaptJavaClass");
    }

    // First try the cache (also avoids infinite recursion if a type references
    // itself).
    JClassType type = adaptedClasses.get(clazz);
    if (type != null) {
        return type;
    }

    // Create and put in the cache
    type = createMock(JClassType.class);
    final JClassType finalType = type;
    adaptedClasses.put(clazz, type);

    // Adds behaviour for annotations and generics
    addAnnotationBehaviour(clazz, type);

    // TODO(rdamazio): Add generics behaviour

    // Add behaviour for getting methods
    expect(type.getMethods()).andStubAnswer(new IAnswer<JMethod[]>() {
        public JMethod[] answer() throws Throwable {
            // TODO(rdamazio): Check behaviour for parent methods
            Method[] realMethods = clazz.getDeclaredMethods();
            JMethod[] methods = new JMethod[realMethods.length];
            for (int i = 0; i < realMethods.length; i++) {
                methods[i] = adaptMethod(realMethods[i], finalType);
            }
            return methods;
        }
    });

    // Add behaviour for getting constructors
    expect(type.getConstructors()).andStubAnswer(new IAnswer<JConstructor[]>() {
        public JConstructor[] answer() throws Throwable {
            Constructor<?>[] realConstructors = clazz.getDeclaredConstructors();
            JConstructor[] constructors = new JConstructor[realConstructors.length];
            for (int i = 0; i < realConstructors.length; i++) {
                constructors[i] = adaptConstructor(realConstructors[i], finalType);
            }
            return constructors;
        }
    });

    // Add behaviour for getting fields
    expect(type.getFields()).andStubAnswer(new IAnswer<JField[]>() {
        public JField[] answer() throws Throwable {
            Field[] realFields = clazz.getDeclaredFields();
            JField[] fields = new JField[realFields.length];
            for (int i = 0; i < realFields.length; i++) {
                fields[i] = adaptField(realFields[i], finalType);
            }
            return fields;
        }
    });

    // Add behaviour for getting names
    expect(type.getName()).andStubReturn(clazz.getName());
    expect(type.getQualifiedSourceName()).andStubReturn(clazz.getCanonicalName());
    expect(type.getSimpleSourceName()).andStubReturn(clazz.getSimpleName());

    // Add modifier behaviour
    int modifiers = clazz.getModifiers();
    expect(type.isAbstract()).andStubReturn(Modifier.isAbstract(modifiers));
    expect(type.isFinal()).andStubReturn(Modifier.isFinal(modifiers));
    expect(type.isPublic()).andStubReturn(Modifier.isPublic(modifiers));
    expect(type.isProtected()).andStubReturn(Modifier.isProtected(modifiers));
    expect(type.isPrivate()).andStubReturn(Modifier.isPrivate(modifiers));

    // Add conversion behaviours
    expect(type.isArray()).andStubReturn(null);
    expect(type.isEnum()).andStubReturn(null);
    expect(type.isPrimitive()).andStubReturn(null);
    expect(type.isClassOrInterface()).andStubReturn(type);
    if (clazz.isInterface()) {
        expect(type.isClass()).andStubReturn(null);
        expect(type.isInterface()).andStubReturn(type);
    } else {
        expect(type.isClass()).andStubReturn(type);
        expect(type.isInterface()).andStubReturn(null);
    }
    expect(type.getEnclosingType()).andStubAnswer(new IAnswer<JClassType>() {
        public JClassType answer() throws Throwable {
            Class<?> enclosingClass = clazz.getEnclosingClass();
            if (enclosingClass == null) {
                return null;
            }

            return adaptJavaClass(enclosingClass);
        }
    });
    expect(type.getSuperclass()).andStubAnswer(new IAnswer<JClassType>() {
        public JClassType answer() throws Throwable {
            Class<?> superclass = clazz.getSuperclass();
            if (superclass == null) {
                return null;
            }

            return adaptJavaClass(superclass);
        }
    });

    // TODO(rdamazio): Mock out other methods as needed
    // TODO(rdamazio): Figure out what to do with reflections that GWT allows
    //                 but Java doesn't

    EasyMock.replay(type);
    return type;
}