Example usage for com.google.gwt.dev.asm Type getObjectType

List of usage examples for com.google.gwt.dev.asm Type getObjectType

Introduction

In this page you can find the example usage for com.google.gwt.dev.asm Type getObjectType.

Prototype

public static Type getObjectType(final String internalName) 

Source Link

Document

Returns the Java type corresponding to the given internal name.

Usage

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

/**
 * Determine if the specified type implements a proxy interface and apply the
 * appropriate validations. This can be used as a general-purpose entry method
 * when writing unit tests.//  w w w . ja va 2 s. c  om
 * 
 * @param binaryName the binary name (e.g. {@link Class#getName()}) of the
 *          EntityProxy or ValueProxy subtype
 */
public void validateProxy(String binaryName) {
    /*
     * Don't call fastFail() here or the proxy may not be validated, since
     * validateXProxy delegates to validateProxy() which would re-check.
     */
    Type proxyType = Type.getObjectType(BinaryName.toInternalName(binaryName));
    if (isAssignable(parentLogger, entityProxyIntf, proxyType)) {
        validateEntityProxy(binaryName);
    } else if (isAssignable(parentLogger, valueProxyIntf, proxyType)) {
        validateValueProxy(binaryName);
    } else {
        parentLogger.poison("%s is neither an %s nor a %s", print(proxyType), print(entityProxyIntf),
                print(valueProxyIntf));
    }
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

/**
 * This method checks a RequestContext interface against its peer domain
 * domain object to determine if the server code would be able to process a
 * request using the the methods defined in the RequestContext interface. It
 * does not perform any checks as to whether or not the RequestContext could
 * actually be generated by the Generator.
 * <p>/*from  ww w.  j  ava2s  . co m*/
 * This method may be called repeatedly on a single instance of the validator.
 * Doing so will amortize type calculation costs.
 * <p>
 * Checks implemented:
 * <ul>
 * <li> <code>binaryName</code> implements RequestContext</li>
 * <li><code>binaryName</code> has a {@link Service} or {@link ServiceName}
 * annotation</li>
 * <li>All service methods in the RequestContext can be mapped onto an
 * equivalent domain method (unless validation is skipped for the method)</li>
 * <li>All referenced EntityProxy types are valid</li>
 * </ul>
 * 
 * @param binaryName the binary name (e.g. {@link Class#getName()}) of the
 *          RequestContext subtype
 * @see #validateEntityProxy(String)
 */
public void validateRequestContext(String binaryName) {
    if (fastFail(binaryName)) {
        return;
    }

    Type requestContextType = Type.getObjectType(BinaryName.toInternalName(binaryName));
    final ErrorContext logger = parentLogger.setType(requestContextType);

    // Quick sanity check for calling code
    if (!isAssignable(logger, requestContextIntf, requestContextType)) {
        logger.poison("%s is not a %s", print(requestContextType), RequestContext.class.getSimpleName());
        return;
    }

    Type domainServiceType = getDomainType(logger, requestContextType, false);
    if (domainServiceType == errorType) {
        logger.poison("The type %s must be annotated with a @%s or @%s annotation",
                BinaryName.toSourceName(binaryName), Service.class.getSimpleName(),
                ServiceName.class.getSimpleName());
        return;
    }

    for (RFMethod method : getMethodsInHierarchy(logger, requestContextType)) {
        // Ignore methods in RequestContext itself
        if (findCompatibleMethod(logger, requestContextIntf, method, false, true, true) != null) {
            continue;
        }

        // Check the client method against the domain
        Method found = checkClientMethodInDomain(logger, method, domainServiceType,
                !clientToLocatorMap.containsKey(requestContextType));
        if (found != null) {
            OperationKey key = new OperationKey(binaryName, method.getName(), method.getDescriptor());
            OperationData data = new OperationData.Builder().setClientMethodDescriptor(method.getDescriptor())
                    .setDomainMethodDescriptor(found.getDescriptor()).setMethodName(method.getName())
                    .setRequestContext(requestContextType.getClassName()).build();
            operationData.put(key, data);
        }
        maybeCheckReferredProxies(logger, method);
    }

    maybeCheckExtraTypes(logger, requestContextType);
    checkUnresolvedKeyTypes(logger);
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

/**
 * This method checks a RequestFactory interface.
 * <p>//from   ww w. j  a v a2s .c om
 * This method may be called repeatedly on a single instance of the validator.
 * Doing so will amortize type calculation costs. It does not perform any
 * checks as to whether or not the RequestContext could actually be generated
 * by the Generator.
 * <p>
 * Checks implemented:
 * <ul>
 * <li> <code>binaryName</code> implements RequestFactory</li>
 * <li>All referenced RequestContext types are valid</li>
 * </ul>
 * 
 * @param binaryName the binary name (e.g. {@link Class#getName()}) of the
 *          RequestContext subtype
 * @see #validateRequestContext(String)
 */
public void validateRequestFactory(String binaryName) {
    if (fastFail(binaryName)) {
        return;
    }

    Type requestFactoryType = Type.getObjectType(BinaryName.toInternalName(binaryName));
    ErrorContext logger = parentLogger.setType(requestFactoryType);

    // Quick sanity check for calling code
    if (!isAssignable(logger, Type.getType(RequestFactory.class), requestFactoryType)) {
        logger.poison("%s is not a %s", print(requestFactoryType), RequestFactory.class.getSimpleName());
        return;
    }

    // Validate each RequestContext method in the RF
    for (Method contextMethod : getMethodsInHierarchy(logger, requestFactoryType)) {
        Type returnType = contextMethod.getReturnType();
        if (isAssignable(logger, requestContextIntf, returnType)) {
            validateRequestContext(returnType.getClassName());
        }
    }

    maybeCheckExtraTypes(logger, requestFactoryType);
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

/**
 * Convert the type used in a client-side EntityProxy or RequestContext
 * declaration to the equivalent domain type. Value types and supported
 * collections are a pass-through. EntityProxy types will be resolved to their
 * domain object type. RequestContext types will be resolved to their service
 * object./* w w w .  j a va2s.  com*/
 */
private Type getDomainType(ErrorContext logger, Type clientType, boolean requireMapping) {
    Type domainType = clientToDomainType.get(clientType);
    if (domainType != null) {
        return domainType;
    }
    if (isValueType(logger, clientType) || isCollectionType(logger, clientType)) {
        domainType = clientType;
    } else if (entityProxyIntf.equals(clientType) || valueProxyIntf.equals(clientType)) {
        domainType = objectType;
    } else {
        logger = logger.setType(clientType);
        DomainMapper pv = new DomainMapper(logger);
        visit(logger, clientType.getInternalName(), pv);
        if (pv.getDomainInternalName() == null) {
            if (requireMapping) {
                logger.poison("%s has no mapping to a domain type (e.g. @%s or @%s)", print(clientType),
                        ProxyFor.class.getSimpleName(), Service.class.getSimpleName());
            }
            domainType = errorType;
        } else {
            domainType = Type.getObjectType(pv.getDomainInternalName());
        }
        if (pv.getLocatorInternalName() != null) {
            Type locatorType = Type.getObjectType(pv.getLocatorInternalName());
            clientToLocatorMap.put(clientType, locatorType);
        }
    }
    addToDomainMap(logger, domainType, clientType);
    if (domainType != errorType) {
        maybeCheckProxyType(logger, clientType);
    }
    return domainType;
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

/**
 * Examines a generic RequestContext method declaration and determines the
 * expected domain return type. This implementation is limited in that it will
 * not attempt to resolve type bounds since that would essentially require
 * implementing TypeOracle. In the case where the type bound cannot be
 * resolved, this method will return Object's type.
 *//*  ww  w  .j a va2 s.c  om*/
private Type getReturnType(ErrorContext logger, RFMethod method) {
    logger = logger.setMethod(method);
    final String[] returnType = { objectType.getInternalName() };
    String signature = method.getSignature();

    final int expectedCount;
    if (method.getReturnType().equals(instanceRequestIntf)) {
        expectedCount = 2;
    } else if (method.getReturnType().equals(requestIntf)) {
        expectedCount = 1;
    } else {
        logger.spam("Punting on " + signature);
        return Type.getObjectType(returnType[0]);
    }

    // TODO(bobv): If a class-based TypeOracle is built, use that instead
    new SignatureReader(signature).accept(new SignatureAdapter() {
        @Override
        public SignatureVisitor visitReturnType() {
            return new SignatureAdapter() {
                int count;

                @Override
                public SignatureVisitor visitTypeArgument(char wildcard) {
                    if (++count == expectedCount) {
                        return new SignatureAdapter() {
                            @Override
                            public void visitClassType(String name) {
                                returnType[0] = name;
                            }
                        };
                    }
                    return super.visitTypeArgument(wildcard);
                }
            };
        }
    });

    logger.spam("Extracted " + returnType[0]);
    return Type.getObjectType(returnType[0]);
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryInterfaceValidator.java

License:Apache License

private void validateProxy(String binaryName, Type expectedType, boolean requireId) {
    if (fastFail(binaryName)) {
        return;//from   w  w w  .  ja  v a2 s  .c o  m
    }

    Type proxyType = Type.getObjectType(BinaryName.toInternalName(binaryName));
    typeTokens.put(OperationKey.hash(binaryName), proxyType);
    ErrorContext logger = parentLogger.setType(proxyType);

    // Quick sanity check for calling code
    if (!isAssignable(logger, expectedType, proxyType)) {
        parentLogger.poison("%s is not a %s", print(proxyType), print(expectedType));
        return;
    }

    // Check supertypes first
    for (Type supertype : getSupertypes(logger, proxyType)) {
        if (shouldAttemptProxyValidation(logger, supertype)) {
            maybeCheckProxyType(logger, supertype);
        }
    }

    // Find the domain type
    Type domainType = getDomainType(logger, proxyType, false);
    if (domainType == errorType) {
        logger.poison("The type %s must be annotated with a @%s or @%s annotation",
                BinaryName.toSourceName(binaryName), ProxyFor.class.getSimpleName(),
                ProxyForName.class.getSimpleName());
        return;
    }

    // Check for getId() and getVersion() in domain if no locator is specified
    if (requireId) {
        Type locatorType = clientToLocatorMap.get(proxyType);
        if (locatorType == null) {
            checkIdAndVersion(logger, domainType);
        }
    }

    // Collect all methods in the client proxy type
    Set<RFMethod> clientPropertyMethods = getMethodsInHierarchy(logger, proxyType);

    // Find the equivalent domain getter/setter method
    for (RFMethod clientPropertyMethod : clientPropertyMethods) {
        // Ignore stableId(). Can't use descriptor due to overrides
        if ("stableId".equals(clientPropertyMethod.getName())
                && clientPropertyMethod.getArgumentTypes().length == 0) {
            continue;
        }
        checkPropertyMethod(logger, clientPropertyMethod, domainType);
        maybeCheckReferredProxies(logger, clientPropertyMethod);
    }
    maybeCheckExtraTypes(logger, proxyType);
}

From source file:com.google.web.bindery.requestfactory.server.RequestFactoryJarExtractor.java

License:Apache License

/**
 * Process the type represented by the name, possibly returning a rebased
 * name./*from  www . ja  va2 s  . c o  m*/
 */
private String processInternalName(String sourceType, String internalName) {
    if (internalName == null) {
        return null;
    }
    return processType(sourceType, Type.getObjectType(internalName)).getInternalName();
}