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

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

Introduction

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

Prototype

JConstructor[] getConstructors();

Source Link

Usage

From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);/*from   ww  w.  jav a2  s .c  o  m*/
    String varName = "t";
    if (t instanceof JPrimitiveType)
        varName = "p";
    int id = nextId();
    typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id);
    pb("Type " + varName + " = new Type();");
    pb(varName + ".name = \"" + t.getErasedType().getQualifiedSourceName() + "\";");
    pb(varName + ".id = " + id + ";");
    pb(varName + ".clazz = " + t.getErasedType().getQualifiedSourceName() + ".class;");
    if (t instanceof JClassType) {
        JClassType c = (JClassType) t;
        if (isVisible(c.getSuperclass()))
            pb(varName + ".superClass = " + c.getSuperclass().getErasedType().getQualifiedSourceName()
                    + ".class;");
        if (c.getFlattenedSupertypeHierarchy() != null) {
            pb("Set<Class> " + varName + "Assignables = new HashSet<Class>();");
            for (JType i : c.getFlattenedSupertypeHierarchy()) {
                if (!isVisible(i))
                    continue;
                pb(varName + "Assignables.add(" + i.getErasedType().getQualifiedSourceName() + ".class);");
            }
            pb(varName + ".assignables = " + varName + "Assignables;");
        }
        if (c.isInterface() != null)
            pb(varName + ".isInterface = true;");
        if (c.isEnum() != null)
            pb(varName + ".isEnum = true;");
        if (c.isArray() != null)
            pb(varName + ".isArray = true;");
        if (c.isMemberType())
            pb(varName + ".isMemberClass = true;");
        pb(varName + ".isStatic = " + c.isStatic() + ";");
        pb(varName + ".isAbstract = " + c.isAbstract() + ";");

        if (c.getFields() != null) {
            pb(varName + ".fields = new Field[] {");
            for (JField f : c.getFields()) {
                String enclosingType = getType(c);
                String fieldType = getType(f.getType());
                int setter = nextId();
                int getter = nextId();
                String elementType = getElementTypes(f);
                String annotations = getAnnotations(f.getDeclaredAnnotations());

                pb("new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", " + f.isFinal()
                        + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", " + f.isProtected() + ", "
                        + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient() + ", " + f.isVolatile()
                        + ", " + getter + ", " + setter + ", " + elementType + ", " + annotations + "), ");

                SetterGetterStub stub = new SetterGetterStub();
                stub.name = f.getName();
                stub.enclosingType = enclosingType;
                stub.type = fieldType;
                stub.isStatic = f.isStatic();
                stub.isFinal = f.isFinal();
                if (enclosingType != null && fieldType != null) {
                    stub.getter = getter;
                    stub.setter = setter;
                }
                setterGetterStubs.add(stub);
            }
            pb("};");
        }

        printMethods(c, varName, "Method", c.getMethods());
        if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) {
            printMethods(c, varName, "Constructor", c.getConstructors());
        } else {
            logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated");
        }

        if (c.isArray() != null) {
            pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";");
        }
        if (c.isEnum() != null) {
            JEnumConstant[] enumConstants = c.isEnum().getEnumConstants();
            if (enumConstants != null) {
                pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];");
                for (int i = 0; i < enumConstants.length; i++) {
                    pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName()
                            + "." + enumConstants[i].getName() + ";");
                }
            }
        }

        Annotation[] annotations = c.getDeclaredAnnotations();
        if (annotations != null && annotations.length > 0) {
            pb(varName + ".annotations = " + getAnnotations(annotations) + ";");
        }
    } else if (t.isAnnotation() != null) {
        pb(varName + ".isAnnotation = true;");
    } else {
        pb(varName + ".isPrimitive = true;");
    }

    pb("types.put(\"" + t.getErasedType().getQualifiedSourceName() + "\", " + varName + ");");
    return buffer.toString();
}

From source file:com.badlogic.gwtref.gen.ReflectionCacheSourceCreator.java

License:Apache License

private String createTypeGenerator(JType t) {
    buffer.setLength(0);//from  ww w.j av a  2  s.  c o  m
    int id = nextTypeId++;
    typeNames2typeIds.put(t.getErasedType().getQualifiedSourceName(), id);
    JClassType c = t.isClass();

    String name = t.getErasedType().getQualifiedSourceName();
    String superClass = null;
    if (c != null && (isVisible(c.getSuperclass())))
        superClass = c.getSuperclass().getErasedType().getQualifiedSourceName() + ".class";
    String assignables = null;
    String interfaces = null;

    if (c != null && c.getFlattenedSupertypeHierarchy() != null) {
        assignables = "new HashSet<Class>(Arrays.asList(";
        boolean used = false;
        for (JType i : c.getFlattenedSupertypeHierarchy()) {
            if (!isVisible(i) || i.equals(t)
                    || "java.lang.Object".equals(i.getErasedType().getQualifiedSourceName()))
                continue;
            if (used)
                assignables += ", ";
            assignables += i.getErasedType().getQualifiedSourceName() + ".class";
            used = true;
        }
        if (used)
            assignables += "))";
        else
            assignables = null;
    }

    if (c == null) {
        // if it's not a class, it may be an interface instead
        c = t.isInterface();
    }

    if (c != null && c.getImplementedInterfaces() != null) {
        interfaces = "new HashSet<Class>(Arrays.asList(";
        boolean used = false;
        for (JType i : c.getImplementedInterfaces()) {
            if (!isVisible(i) || i.equals(t))
                continue;
            if (used)
                interfaces += ", ";
            interfaces += i.getErasedType().getQualifiedSourceName() + ".class";
            used = true;
        }
        if (used)
            interfaces += "))";
        else
            interfaces = null;
    }

    String varName = "c" + id;
    pb("private static Type " + varName + ";");
    pb("private static Type " + varName + "() {");
    pb("if(" + varName + "!=null) return " + varName + ";");
    pb(varName + " = new Type(\"" + name + "\", " + id + ", " + name + ".class, " + superClass + ", "
            + assignables + ", " + interfaces + ");");

    if (c != null) {
        if (c.isEnum() != null)
            pb(varName + ".isEnum = true;");
        if (c.isArray() != null)
            pb(varName + ".isArray = true;");
        if (c.isMemberType())
            pb(varName + ".isMemberClass = true;");
        if (c.isInterface() != null) {
            pb(varName + ".isInterface = true;");
        } else {
            pb(varName + ".isStatic = " + c.isStatic() + ";");
            pb(varName + ".isAbstract = " + c.isAbstract() + ";");
        }

        if (c.getFields() != null && c.getFields().length > 0) {
            pb(varName + ".fields = new Field[] {");
            for (JField f : c.getFields()) {
                String enclosingType = getType(c);
                String fieldType = getType(f.getType());
                int setterGetter = nextSetterGetterId++;
                String elementType = getElementTypes(f);
                String annotations = getAnnotations(f.getDeclaredAnnotations());

                pb("    new Field(\"" + f.getName() + "\", " + enclosingType + ", " + fieldType + ", "
                        + f.isFinal() + ", " + f.isDefaultAccess() + ", " + f.isPrivate() + ", "
                        + f.isProtected() + ", " + f.isPublic() + ", " + f.isStatic() + ", " + f.isTransient()
                        + ", " + f.isVolatile() + ", " + setterGetter + ", " + setterGetter + ", " + elementType
                        + ", " + annotations + "), ");

                SetterGetterStub stub = new SetterGetterStub();
                stub.name = f.getName();
                stub.enclosingType = enclosingType;
                stub.type = fieldType;
                stub.isStatic = f.isStatic();
                stub.isFinal = f.isFinal();
                if (enclosingType != null && fieldType != null) {
                    stub.getter = setterGetter;
                    stub.setter = setterGetter;
                }
                setterGetterStubs.add(stub);
            }
            pb("};");
        }

        createTypeInvokables(c, varName, "Method", c.getMethods());
        if (c.isPublic() && !c.isAbstract() && (c.getEnclosingType() == null || c.isStatic())) {
            createTypeInvokables(c, varName, "Constructor", c.getConstructors());
        } else {
            logger.log(Type.INFO, c.getName() + " can't be instantiated. Constructors not generated");
        }

        if (c.isArray() != null) {
            pb(varName + ".componentType = " + getType(c.isArray().getComponentType()) + ";");
        }
        if (c.isEnum() != null) {
            JEnumConstant[] enumConstants = c.isEnum().getEnumConstants();
            if (enumConstants != null) {
                pb(varName + ".enumConstants = new Object[" + enumConstants.length + "];");
                for (int i = 0; i < enumConstants.length; i++) {
                    pb(varName + ".enumConstants[" + i + "] = " + c.getErasedType().getQualifiedSourceName()
                            + "." + enumConstants[i].getName() + ";");
                }
            }
        }

        Annotation[] annotations = c.getDeclaredAnnotations();
        if (annotations != null && annotations.length > 0) {
            pb(varName + ".annotations = " + getAnnotations(annotations) + ";");
        }
    } else if (t.isAnnotation() != null) {
        pb(varName + ".isAnnotation = true;");
    } else {
        pb(varName + ".isPrimitive = true;");
    }

    pb("return " + varName + ";");
    pb("}");
    return buffer.toString();
}

From source file:com.github.ludorival.dao.gwt.rebind.EntityManagerGenerator.java

License:Apache License

private boolean isInstantiable(JClassType type, TreeLogger logger) {
    JConstructor[] constructors = type.getConstructors();
    if (constructors == null || constructors.length == 0)
        return true;
    JConstructor constructor = type.findConstructor(new JType[0]);
    if (constructor == null)
        return false;
    if (constructor.isPublic())
        return true;

    return false;
}

From source file:com.github.nmorel.gwtjackson.rebind.bean.BeanProcessor.java

License:Apache License

/**
 * Look for the method to create a new instance of the bean. If none are found or the bean is abstract or an interface, we considered it
 * as non instantiable./*from  www .  ja  va2  s .  c  om*/
 *
 * @param typeOracle the oracle
 * @param logger logger
 * @param beanType type to look for constructor
 * @param builder current bean builder
 */
private static void determineInstanceCreator(RebindConfiguration configuration, JacksonTypeOracle typeOracle,
        TreeLogger logger, JClassType beanType, BeanInfoBuilder builder) {
    if (isObjectOrSerializable(beanType)) {
        return;
    }

    Optional<JClassType> mixinClass = configuration.getMixInAnnotations(beanType);

    List<JClassType> accessors = new ArrayList<JClassType>();
    if (mixinClass.isPresent()) {
        accessors.add(mixinClass.get());
    }
    accessors.add(beanType);

    // Look for a builder class
    Optional<Annotation> jsonDeserialize = CreatorUtils
            .getAnnotation("com.fasterxml.jackson.databind.annotation.JsonDeserialize", accessors);
    if (jsonDeserialize.isPresent()) {
        Optional<JClassType> builderClass = typeOracle.getClassFromJsonDeserializeAnnotation(logger,
                jsonDeserialize.get(), "builder");
        if (builderClass.isPresent()) {
            builder.setBuilder(builderClass.get());
            return;
        }
    }

    // we search for @JsonCreator annotation
    JConstructor creatorDefaultConstructor = null;
    JConstructor creatorConstructor = null;

    // we keep the list containing the mixin creator and the real creator
    List<? extends JAbstractMethod> creators = Collections.emptyList();

    if (null == beanType.isInterface() && !beanType.isAbstract()) {
        for (JConstructor constructor : beanType.getConstructors()) {
            if (constructor.getParameters().length == 0) {
                creatorDefaultConstructor = constructor;
                continue;
            }

            // A constructor is considered as a creator if
            // - he is annotated with JsonCreator and
            //   * all its parameters are annotated with JsonProperty
            //   * or it has only one parameter
            // - or all its parameters are annotated with JsonProperty

            List<JConstructor> constructors = new ArrayList<JConstructor>();
            if (mixinClass.isPresent() && null == mixinClass.get().isInterface()) {
                JConstructor mixinConstructor = mixinClass.get()
                        .findConstructor(constructor.getParameterTypes());
                if (null != mixinConstructor) {
                    constructors.add(mixinConstructor);
                }
            }
            constructors.add(constructor);

            Optional<JsonIgnore> jsonIgnore = getAnnotation(JsonIgnore.class, constructors);
            if (jsonIgnore.isPresent() && jsonIgnore.get().value()) {
                continue;
            }

            boolean isAllParametersAnnotatedWithJsonProperty = isAllParametersAnnotatedWith(constructors.get(0),
                    JsonProperty.class);
            if ((isAnnotationPresent(JsonCreator.class, constructors)
                    && ((isAllParametersAnnotatedWithJsonProperty)
                            || (constructor.getParameters().length == 1)))
                    || isAllParametersAnnotatedWithJsonProperty) {
                creatorConstructor = constructor;
                creators = constructors;
                break;
            }
        }
    }

    JMethod creatorFactory = null;
    if (null == creatorConstructor) {
        // searching for factory method
        for (JMethod method : beanType.getMethods()) {
            if (method.isStatic()) {

                List<JMethod> methods = new ArrayList<JMethod>();
                if (mixinClass.isPresent() && null == mixinClass.get().isInterface()) {
                    JMethod mixinMethod = mixinClass.get().findMethod(method.getName(),
                            method.getParameterTypes());
                    if (null != mixinMethod && mixinMethod.isStatic()) {
                        methods.add(mixinMethod);
                    }
                }
                methods.add(method);

                Optional<JsonIgnore> jsonIgnore = getAnnotation(JsonIgnore.class, methods);
                if (jsonIgnore.isPresent() && jsonIgnore.get().value()) {
                    continue;
                }

                if (isAnnotationPresent(JsonCreator.class, methods) && (method.getParameters().length == 1
                        || isAllParametersAnnotatedWith(methods.get(0), JsonProperty.class))) {
                    creatorFactory = method;
                    creators = methods;
                    break;
                }
            }
        }
    }

    final Optional<JAbstractMethod> creatorMethod;
    boolean defaultConstructor = false;

    if (null != creatorConstructor) {
        creatorMethod = Optional.<JAbstractMethod>of(creatorConstructor);
    } else if (null != creatorFactory) {
        creatorMethod = Optional.<JAbstractMethod>of(creatorFactory);
    } else if (null != creatorDefaultConstructor) {
        defaultConstructor = true;
        creatorMethod = Optional.<JAbstractMethod>of(creatorDefaultConstructor);
    } else {
        creatorMethod = Optional.absent();
    }

    builder.setCreatorMethod(creatorMethod);
    builder.setCreatorDefaultConstructor(defaultConstructor);

    if (creatorMethod.isPresent() && !defaultConstructor) {
        if (creatorMethod.get().getParameters().length == 1
                && !isAllParametersAnnotatedWith(creators.get(0), JsonProperty.class)) {
            // delegation constructor
            builder.setCreatorDelegation(true);
            builder.setCreatorParameters(ImmutableMap.of(BeanJsonDeserializerCreator.DELEGATION_PARAM_NAME,
                    creatorMethod.get().getParameters()[0]));
        } else {
            // we want the property name define in the mixin and the parameter defined in the real creator method
            ImmutableMap.Builder<String, JParameter> creatorParameters = ImmutableMap.builder();
            for (int i = 0; i < creatorMethod.get().getParameters().length; i++) {
                creatorParameters.put(
                        creators.get(0).getParameters()[i].getAnnotation(JsonProperty.class).value(),
                        creators.get(creators.size() - 1).getParameters()[i]);
            }
            builder.setCreatorParameters(creatorParameters.build());
        }
    }
}

From source file:com.github.nmorel.gwtjackson.rebind.RebindConfiguration.java

License:Apache License

/**
 * Search a static method or constructor to instantiate the mapper and return a {@link String} calling it.
 *///from w  ww .j  ava 2 s  . c om
private MapperInstance getInstance(JType mappedType, JClassType classType, boolean isSerializers) {
    int nbParam = 0;
    if (null != mappedType.isGenericType() && (!isSerializers || !typeOracle.isEnumSupertype(mappedType))) {
        nbParam = mappedType.isGenericType().getTypeParameters().length;
    }

    // we first look at static method
    for (JMethod method : classType.getMethods()) {
        // method must be public static, return the instance type and take no parameters
        if (method.isStatic() && null != method.getReturnType().isClassOrInterface()
                && classType.isAssignableTo(method.getReturnType().isClassOrInterface())
                && method.getParameters().length == nbParam && method.isPublic()) {
            MapperType[] parameters = getParameters(mappedType, method, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, method, parameters);
        }
    }

    // then we search the default constructor
    for (JConstructor constructor : classType.getConstructors()) {
        if (constructor.isPublic() && constructor.getParameters().length == nbParam) {
            MapperType[] parameters = getParameters(mappedType, constructor, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, constructor, parameters);
        }
    }

    logger.log(Type.WARN, "Cannot instantiate the custom serializer/deserializer "
            + classType.getQualifiedSourceName() + ". It will be ignored");
    return null;
}

From source file:com.github.nmorel.gwtjackson.rebind.RebindConfiguration.java

License:Apache License

/**
 * Search a static method or constructor to instantiate the key mapper and return a {@link String} calling it.
 *///from   w ww.  ja v a  2 s . c o m
private MapperInstance getKeyInstance(JType mappedType, JClassType classType, boolean isSerializers) {
    int nbParam = 0;
    if (!isSerializers && typeOracle.isEnumSupertype(mappedType)) {
        nbParam = 1;
    }

    // we first look at static method
    for (JMethod method : classType.getMethods()) {
        // method must be public static, return the instance type and take no parameters
        if (method.isStatic() && null != method.getReturnType().isClassOrInterface()
                && classType.isAssignableTo(method.getReturnType().isClassOrInterface())
                && method.getParameters().length == nbParam && method.isPublic()) {
            MapperType[] parameters = getParameters(mappedType, method, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, method, parameters);
        }
    }

    // then we search the default constructor
    for (JConstructor constructor : classType.getConstructors()) {
        if (constructor.isPublic() && constructor.getParameters().length == nbParam) {
            MapperType[] parameters = getParameters(mappedType, constructor, isSerializers);
            if (null == parameters) {
                continue;
            }

            return new MapperInstance(classType, constructor, parameters);
        }
    }

    logger.log(Type.WARN, "Cannot instantiate the custom key serializer/deserializer "
            + classType.getQualifiedSourceName() + ". It will be ignored");
    return null;
}

From source file:com.google.gwt.testing.easygwtmock.rebind.MocksGenerator.java

License:Apache License

/**
 * Generates a mock class for {@code interfaceToMock}.
 *///from   w  w  w .j  a  v  a  2 s  .  com
String generateMock(JClassType typeToMock) throws UnableToCompleteException {

    JPackage interfacePackage = typeToMock.getPackage();
    String packageName = interfacePackage == null ? "" : interfacePackage.getName();
    String newClassName = typeToMock.getName().replace(".", "_") + "Mock";

    // GenericType<Integer> has to generate a different mock implementation than
    // GenericType<String>, that's what we check and do here
    if (typeToMock.isParameterized() != null) {
        StringBuilder typeList = new StringBuilder();
        for (JClassType genericArg : typeToMock.isParameterized().getTypeArgs()) {
            typeList.append(genericArg.getParameterizedQualifiedSourceName());
        }
        newClassName += Integer.toHexString(typeList.toString().hashCode());
    }

    String fullNewClassName = packageName + "." + newClassName;

    PrintWriter printWriter = this.context.tryCreate(this.logger, packageName, newClassName);
    if (printWriter == null) {
        // We generated this before.
        return fullNewClassName;
    }

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, newClassName);
    composer.addImport(MocksControlBase.class.getCanonicalName());
    composer.addImport(Method.class.getCanonicalName());
    composer.addImport(Call.class.getCanonicalName());
    composer.addImport(UndeclaredThrowableException.class.getCanonicalName());
    if (typeToMock.isInterface() != null) {
        composer.addImplementedInterface(typeToMock.getParameterizedQualifiedSourceName());
    } else {
        composer.setSuperclass(typeToMock.getParameterizedQualifiedSourceName());
    }

    SourceWriter sourceWriter = composer.createSourceWriter(this.context, printWriter);
    sourceWriter.println();

    JMethod[] overridableMethods = typeToMock.getOverridableMethods();

    List<JMethod> methodsToMock = new ArrayList<JMethod>();
    Set<String> needsDefaultImplementation = new HashSet<String>();
    for (JMethod method : overridableMethods) {
        if (isSpecialMethodOfObject(method)) {
            needsDefaultImplementation.add(method.getName());
        } else if (method.getParameterTypes().length == 0 && method.getName().equals("getClass")) {
            // ignore, Bug 5026788 in GWT
        } else {
            methodsToMock.add(method);
        }
    }

    printFields(sourceWriter, methodsToMock);
    printConstructors(sourceWriter, newClassName, typeToMock.getConstructors());
    printMockMethods(sourceWriter, methodsToMock, newClassName);
    printDefaultMethods(sourceWriter, typeToMock, needsDefaultImplementation);

    sourceWriter.commit(this.logger);

    return fullNewClassName;
}

From source file:com.gwtent.gen.GenUtils.java

License:Apache License

public static boolean hasPublicDefaultConstructor(JClassType classType) {
    for (JConstructor constructor : classType.getConstructors()) {
        if ((constructor.getParameters().length == 0) && constructor.isPublic())
            return true;
    }/*from w w  w.  ja  va  2s.co  m*/

    return false;
}

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 va  2s  .  c o m
 * @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;
}

From source file:fr.onevu.gwt.uibinder.rebind.model.OwnerFieldClass.java

License:Apache License

/**
 * Finds the constructor annotated with @UiConcontructor if there is one, and
 * puts it in the {@link #uiConstructor} field.
 * /*from  w  w w . j  a v  a2 s .  c om*/
 * @param fieldType
 *          the type of the field
 */
private void findUiConstructor(JClassType fieldType) throws UnableToCompleteException {
    for (JConstructor ctor : fieldType.getConstructors()) {
        if (ctor.getAnnotation(UiConstructor.class) != null) {
            if (uiConstructor != null) {
                logger.die(
                        fieldType.getName() + " has more than one constructor annotated with @UiConstructor");
            }
            uiConstructor = ctor;
        }
    }
}