Example usage for java.lang Class toString

List of usage examples for java.lang Class toString

Introduction

In this page you can find the example usage for java.lang Class toString.

Prototype

public String toString() 

Source Link

Document

Converts the object to a string.

Usage

From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.java

/**
 * Given a JSONObject, unmarhall it to an instance of the given class.
 *
 * @param jsonObj JSON string to unmarshall.
 * @param cls Return an instance of this class. Must be either public class
 *            or private static class. Inner class will not work.
 * @param <T> Same type as cls.//from ww  w  .j  a  v  a 2  s  .c  o m
 * @return An instance of class given by cls.
 * @throws com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.TypeMismatchException
 */
public static <T> T JSONToObj(JSONObject jsonObj, Class<T> cls) {
    T result = null;

    try {
        Constructor<T> constructor = cls.getDeclaredConstructor();
        constructor.setAccessible(true);
        result = (T) constructor.newInstance();
        Iterator<?> i = jsonObj.keys();

        while (i.hasNext()) {
            String k = (String) i.next();
            Object val = jsonObj.get(k);

            try {
                Field field = cls.getField(k);
                Object converted = valToType(val, field.getGenericType());

                if (converted == null) {
                    if (!field.getType().isPrimitive()) {
                        field.set(result, null);
                    } else {
                        throw new TypeMismatchException(
                                String.format("Type %s cannot be set to null.", field.getType()));
                    }
                } else {
                    if (converted instanceof List && field.getType().isAssignableFrom(List.class)) {
                        // Class can define their own favorite
                        // implementation of List. In which case the field
                        // still need to be defined as List, but it can be
                        // initialized with a placeholder instance of any of
                        // the List implementations (eg. ArrayList).
                        Object existing = field.get(result);
                        if (existing != null) {
                            ((List<?>) existing).clear();

                            // Just because I don't want javac to complain
                            // about unsafe operations. So I'm gonna use
                            // more reflection, HA!
                            Method addAll = existing.getClass().getMethod("addAll", Collection.class);
                            addAll.invoke(existing, converted);
                        } else {
                            field.set(result, converted);
                        }
                    } else {
                        field.set(result, converted);
                    }
                }
            } catch (NoSuchFieldException e) {
                // Ignore.
            } catch (IllegalAccessException e) {
                // Ignore.
            } catch (IllegalArgumentException e) {
                // Ignore.
            }
        }
    } catch (JSONException e) {
        throw new ParserException(e);
    } catch (NoSuchMethodException e) {
        throw new ClassInstantiationException("Failed to retrieve constructor for " + cls.toString()
                + ", make sure it's not an inner class.");
    } catch (InstantiationException e) {
        throw new ClassInstantiationException(cls);
    } catch (IllegalAccessException e) {
        throw new ClassInstantiationException(cls);
    } catch (InvocationTargetException e) {
        throw new ClassInstantiationException(cls);
    }

    return result;
}

From source file:org.apache.axis2.jaxws.description.builder.DescriptionBuilderComposite.java

/**
 * Return a composite annotation of the specified type.  If the composite annotation is 
 * null, then the associated class (if not null) will be examined for the appropriate java
 * annotation.  If one is found, it will be used to create a new composite annotation.
 * //from   w  w  w .  ja va2s.  c  om
 * @param compositeAnnotation May be null.  The current composite annotation.  If this is
 * non-null, it will simply be returned.
 * @param compositeAnnotClass The class of the composite annotation.  This is a subclass of
 * the java annotation class.
 * @param javaAnnotationClass The java annotation class.  The associated class will be 
 * reflected on to see if this annotation exists.  If so, it is used to create an instance of
 * the composite annotation class.
 * @return
 */
private Annotation getCompositeAnnotation(Annotation compositeAnnotation, Class compositeAnnotClass,
        Class javaAnnotationClass) {
    Annotation returnAnnotation = compositeAnnotation;
    if (returnAnnotation == null && theCorrespondingClass != null) {
        // Get the annotation from the class and if one exists, construct a composite annot for it
        Annotation annotationFromClass = getAnnotationFromClass(theCorrespondingClass, javaAnnotationClass);
        if (annotationFromClass != null) {
            try {
                Method createAnnot = compositeAnnotClass.getMethod("createFromAnnotation", Annotation.class);
                returnAnnotation = (Annotation) createAnnot.invoke(null, annotationFromClass);
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug("Unable to create composite annotation due to exception."
                            + "  Composite Annotation: " + compositeAnnotation + "; Composite Annot class: "
                            + compositeAnnotClass + "; Java Annot class: " + javaAnnotationClass, e);
                }
                String msg = Messages.getMessage("DescriptionBuilderErr1", compositeAnnotClass.toString(),
                        e.toString());
                throw ExceptionFactory.makeWebServiceException(msg, e);
            }
        }
    }
    return returnAnnotation;
}

From source file:alice.tuprolog.lib.OOLibrary.java

/**
 * Sets the value of the field 'i' with 'what'
 * @param obj_id/* w w  w  . j av a 2s.c  o m*/
 * @param i
 * @param what
 * @return
 * @throws JavaException
 */
public boolean java_array_get_primitive_3(PTerm obj_id, PTerm i, PTerm what) throws JavaException {
    Struct objId = (Struct) obj_id.getTerm();
    Number index = (Number) i.getTerm();
    what = what.getTerm();
    Object obj = null;
    if (!index.isInteger()) {
        throw new JavaException(new IllegalArgumentException(index.toString()));
    }
    try {
        Class<?> cl = null;
        String objName = alice.util.Tools.removeApices(objId.toString());
        obj = currentObjects.get(objName);
        if (obj != null) {
            cl = obj.getClass();
        } else {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }

        if (!cl.isArray()) {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }
        String name = cl.toString();
        switch (name) {
        case "class [I": {
            PTerm value = new Int(Array.getInt(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [D": {
            PTerm value = new alice.tuprolog.Double(Array.getDouble(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [F": {
            PTerm value = new alice.tuprolog.Float(Array.getFloat(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [L": {
            PTerm value = new alice.tuprolog.Long(Array.getLong(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [C": {
            PTerm value = new Struct("" + Array.getChar(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [Z":
            boolean b = Array.getBoolean(obj, index.intValue());
            if (b) {
                if (unify(what, PTerm.TRUE))
                    return true;
                else
                    throw new JavaException(new IllegalArgumentException(what.toString()));
            } else {
                if (unify(what, PTerm.FALSE))
                    return true;
                else
                    throw new JavaException(new IllegalArgumentException(what.toString()));
            }
        case "class [B": {
            PTerm value = new Int(Array.getByte(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        case "class [S": {
            PTerm value = new Int(Array.getInt(obj, index.intValue()));
            if (unify(what, value))
                return true;
            else
                throw new JavaException(new IllegalArgumentException(what.toString()));
        }
        default:
            throw new JavaException(new Exception());
        }
    } catch (Exception ex) {
        // ex.printStackTrace();
        throw new JavaException(ex);
    }

}

From source file:alice.tuprolog.lib.OOLibrary.java

public boolean java_array_set_primitive_3(PTerm obj_id, PTerm i, PTerm what) throws JavaException {
    Struct objId = (Struct) obj_id.getTerm();
    Number index = (Number) i.getTerm();
    what = what.getTerm();/* w  ww .j  a  v a 2  s  .  c  om*/
    Object obj = null;
    if (!index.isInteger()) {
        throw new JavaException(new IllegalArgumentException(index.toString()));
    }
    try {
        Class<?> cl = null;
        String objName = alice.util.Tools.removeApices(objId.toString());
        obj = currentObjects.get(objName);
        if (obj != null) {
            cl = obj.getClass();
        } else {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }

        if (!cl.isArray()) {
            throw new JavaException(new IllegalArgumentException(objId.toString()));
        }
        String name = cl.toString();
        switch (name) {
        case "class [I": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            byte v = (byte) ((Number) what).intValue();
            Array.setInt(obj, index.intValue(), v);
            break;
        }
        case "class [D": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            double v = ((Number) what).doubleValue();
            Array.setDouble(obj, index.intValue(), v);
            break;
        }
        case "class [F": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            float v = ((Number) what).floatValue();
            Array.setFloat(obj, index.intValue(), v);
            break;
        }
        case "class [L": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            long v = ((Number) what).longValue();
            Array.setFloat(obj, index.intValue(), v);
            break;
        }
        case "class [C": {
            String s = what.toString();
            Array.setChar(obj, index.intValue(), s.charAt(0));
            break;
        }
        case "class [Z": {
            String s = what.toString();
            switch (s) {
            case "true":
                Array.setBoolean(obj, index.intValue(), true);
                break;
            case "false":
                Array.setBoolean(obj, index.intValue(), false);
                break;
            default:
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            break;
        }
        case "class [B": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            int v = ((Number) what).intValue();
            Array.setByte(obj, index.intValue(), (byte) v);
            break;
        }
        case "class [S": {
            if (!(what instanceof Number)) {
                throw new JavaException(new IllegalArgumentException(what.toString()));
            }
            short v = (short) ((Number) what).intValue();
            Array.setShort(obj, index.intValue(), v);
            break;
        }
        default:
            throw new JavaException(new Exception());
        }
        return true;
    } catch (Exception ex) {
        throw new JavaException(ex);
    }
}

From source file:org.broadinstitute.sting.commandline.ArgumentTypeDescriptor.java

@Override
public Object parse(ParsingEngine parsingEngine, ArgumentSource source, Type fulltype,
        ArgumentMatches matches) {//from   w  w  w .j  a v a  2 s.  c o  m
    Class type = makeRawTypeIfNecessary(fulltype);
    if (source.isFlag())
        return true;

    ArgumentDefinition defaultDefinition = createDefaultArgumentDefinition(source);
    ArgumentMatchValue value = getArgumentValue(defaultDefinition, matches);
    Object result;
    Tags tags = getArgumentTags(matches);

    // lets go through the types we support
    try {
        if (type.isPrimitive()) {
            Method valueOf = primitiveToWrapperMap.get(type).getMethod("valueOf", String.class);
            if (value == null)
                throw new MissingArgumentValueException(createDefaultArgumentDefinition(source));
            result = valueOf.invoke(null, value.asString().trim());
        } else if (type.isEnum()) {
            Object[] vals = type.getEnumConstants();
            Object defaultEnumeration = null; // as we look at options, record the default option if it exists
            for (Object val : vals) {
                if (String.valueOf(val).equalsIgnoreCase(value == null ? null : value.asString()))
                    return val;
                try {
                    if (type.getField(val.toString()).isAnnotationPresent(EnumerationArgumentDefault.class))
                        defaultEnumeration = val;
                } catch (NoSuchFieldException e) {
                    throw new ReviewedStingException(
                            "parsing " + type.toString() + "doesn't contain the field " + val.toString());
                }
            }
            // if their argument has no value (null), and there's a default, return that default for the enum value
            if (defaultEnumeration != null && value == null)
                result = defaultEnumeration;
            // if their argument has no value and there's no default, throw a missing argument value exception.
            // TODO: Clean this up so that null values never make it to this point.  To fix this, we'll have to clean up the implementation of -U.
            else if (value == null)
                throw new MissingArgumentValueException(createDefaultArgumentDefinition(source));
            else
                throw new UnknownEnumeratedValueException(createDefaultArgumentDefinition(source),
                        value.asString());
        } else if (type.equals(File.class)) {
            result = value == null ? null : value.asFile();
        } else {
            Constructor ctor = type.getConstructor(String.class);
            result = ctor.newInstance(value == null ? null : value.asString());
        }
    } catch (UserException e) {
        throw e;
    } catch (InvocationTargetException e) {
        throw new UserException.CommandLineException(String.format(
                "Failed to parse value %s for argument %s.  This is most commonly caused by providing an incorrect data type (e.g. a double when an int is required)",
                value, source.field.getName()));
    } catch (Exception e) {
        throw new DynamicClassResolutionException(String.class, e);
    }

    // TODO FIXME!

    // WARNING: Side effect!
    parsingEngine.addTags(result, tags);

    return result;
}

From source file:edu.ksu.cis.indus.tools.slicer.SlicerConfiguration.java

/**
 * Sets up the nature of ready dependence.
 * /*w  w w  .  j  a va2s .c  o m*/
 * @param nature of ready dependence.
 */
private void setupNatureOfReadyDep(final Comparable<?> nature) {
    final Class<? extends ReadyDAv1> _clazz = getReadyDAClass(nature);

    try {
        final Set<IDependencyAnalysis<?, ?, ?, ?, ?, ?>> _temp = new HashSet<IDependencyAnalysis<?, ?, ?, ?, ?, ?>>();

        if (SliceType.FORWARD_SLICE.equals(getSliceType())) {
            _temp.add((IDependencyAnalysis) _clazz.getMethod("getForwardReadyDA", (Class[]) null).invoke(null,
                    (Object[]) null));
        } else if (SliceType.BACKWARD_SLICE.equals(getSliceType())) {
            _temp.add((IDependencyAnalysis) _clazz.getMethod("getBackwardReadyDA", (Class[]) null).invoke(null,
                    (Object[]) null));
        } else if (SliceType.COMPLETE_SLICE.equals(getSliceType())) {
            _temp.add((IDependencyAnalysis) _clazz.getMethod("getBackwardReadyDA", (Class[]) null).invoke(null,
                    (Object[]) null));
            _temp.add((IDependencyAnalysis) _clazz.getMethod("getForwardReadyDA", (Class[]) null).invoke(null,
                    (Object[]) null));
        } else {
            final String _msg = "Illegal slice type :" + _clazz.toString() + " : " + getSliceType();
            LOGGER.error("setupNatureOfReadyDep" + "() -  : " + _msg);
            throw new IllegalStateException(_msg);
        }
        id2dependencyAnalyses.put(IDependencyAnalysis.DependenceSort.READY_DA, _temp);
    } catch (final NoSuchMethodException _e) {
        final String _msg = "Dependence analysis does not provide getForwardReadyDA() and/or getBackwardReadyDA() :"
                + _clazz;
        LOGGER.error("setupNatureOfReadyDep() -  : " + _msg);

        final RuntimeException _runtimeException = new RuntimeException(_msg);
        _runtimeException.initCause(_e);
        throw _runtimeException;
    } catch (final IllegalArgumentException _e) {
        final String _msg = "Dependence analysis does not provide static zero-parameter versions of "
                + "getForwardReadyDA() and/or getBackwardReadyDA() : " + _clazz;
        LOGGER.error("setupNatureOfReadyDep() -  : " + _msg);
        throw _e;
    } catch (final SecurityException _e) {
        final String _msg = "Insufficient permission to access specified ready dependence analysis class : "
                + _clazz;
        LOGGER.error("setupNatureOfReadyDep() -  : " + _msg);

        final RuntimeException _runtimeException = new RuntimeException(_msg);
        _runtimeException.initCause(_e);
        throw _runtimeException;
    } catch (final IllegalAccessException _e) {
        final String _msg = "Dependence analysis does not provide publicly accessible versions of  "
                + "getForwardReadyDA() and/or getBackwardReadyDA() : " + _clazz;
        LOGGER.error("setupNatureOfReadyDep() -  : " + _msg);

        final RuntimeException _runtimeException = new RuntimeException(_msg);
        _runtimeException.initCause(_e);
        throw _runtimeException;
    } catch (final InvocationTargetException _e) {
        final String _msg = "getForwardReadyDA() and/or getBackwardReadyDA() threw an exception : " + _clazz;
        LOGGER.error("setupNatureOfReadyDep() -  : " + _msg);

        final RuntimeException _runtimeException = new RuntimeException(_msg);
        _runtimeException.initCause(_e);
        throw _runtimeException;
    }
}

From source file:org.sakaiproject.kernel.webapp.JaxRsApplicationListener.java

@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    Registry<String, JaxRsSingletonProvider> singletonRegistry = getSingletonRegistry();
    Registry<String, JaxRsPrototypeProvider> prototypeRegistry = getPrototypeRegistry();
    String appClass = event.getServletContext().getInitParameter(Application.class.getName());
    Application app;/*from w  w  w. j  av  a2s  .com*/
    try {
        Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(appClass.trim());
        app = (Application) clazz.newInstance();
    } catch (Exception e) {
        LOG.warn("Unable to instantiate JAX-RS Application " + appClass);
        e.printStackTrace();
        return;
    }
    for (final Object object : app.getSingletons()) {
        if (!(object instanceof Documentable)) {
            throw new IllegalStateException(object + " must implement " + Documentable.class);
        }
        JaxRsSingletonProvider provider = new JaxRsSingletonProvider() {
            // The Application object is out of our hands, so we can't
            // properly constrain the classes it returns. Hence, cast it.
            private Documentable documentable = (Documentable) object;

            public Documentable getJaxRsSingleton() {
                return documentable;
            }

            public String getKey() {
                return documentable.getClass().getName();
            }

            public int getPriority() {
                return 0;
            }

            @Override
            public String toString() {
                return "Provider for: " + object.toString();
            }
        };
        jaxRsSingletonProviders.add(provider);
        singletonRegistry.add(provider);
        LOG.info("Added " + provider.getJaxRsSingleton() + " to JAX-RS registry " + singletonRegistry);
    }
    for (final Class<?> clazz : app.getClasses()) {
        if (!(Documentable.class.isAssignableFrom(clazz))) {
            throw new IllegalStateException(clazz + " must implement " + Documentable.class);
        }

        JaxRsPrototypeProvider provider = new JaxRsPrototypeProvider() {
            // The Application object is out of our hands, so we can't
            // properly constrain the classes it returns. Hence, cast it.
            private Class<? extends Documentable> documentable = (Class<? extends Documentable>) clazz;

            public Class<? extends Documentable> getJaxRsPrototype() {
                return documentable;
            }

            public String getKey() {
                return documentable.getClass().getName();
            }

            public int getPriority() {
                return 0;
            }

            @Override
            public String toString() {
                return "Provider for: " + clazz.toString();
            }
        };
        jaxRsPrototypeProviders.add(provider);
        prototypeRegistry.add(provider);
        LOG.info("Added " + provider.getJaxRsPrototype() + " to JAX-RS registry " + prototypeRegistry);

    }
}

From source file:nl.knaw.dans.common.lang.search.bean.AbstractSearchBeanFactory.java

@SuppressWarnings("unchecked")
public Object createSearchBean(String type, Document document)
        throws SearchBeanFactoryException, SearchBeanException {
    Class<?> sbClass = null;
    Object searchBean = null;/*w  ww.  j a  va  2s.c om*/
    sbClass = getTypeMap().get(type);
    if (sbClass == null)
        throw new UnknownSearchBeanTypeException(type);

    Map<String, java.lang.reflect.Field> fieldsMap = null;
    try {
        fieldsMap = getFieldsMap(sbClass);
        searchBean = sbClass.newInstance();
    } catch (Exception e) {
        throw new SearchBeanException(e);
    }

    // check if the primary key is in the document
    Index index = SearchBeanUtil.getDefaultIndex(sbClass);
    if (index != null) {
        if (document.getFieldByName(index.getPrimaryKey()) == null)
            throw new PrimaryKeyMissingException("Primary key not found in document. " + document.toString());
    }

    // convert fields
    for (Map.Entry<String, java.lang.reflect.Field> searchField : fieldsMap.entrySet()) {
        Field docField = document.getFieldByName(searchField.getKey());
        java.lang.reflect.Field classField = searchField.getValue();
        if (docField != null) {
            String propName = classField.getName();
            String setMethodName = "set" + StringUtils.capitalize(propName);
            try {
                Method setMethod = sbClass.getMethod(setMethodName, new Class[] { classField.getType() });
                Class fieldType = classField.getType();
                Object value = docField.getValue();

                SearchField sbField = classField.getAnnotation(SearchField.class);
                Class<? extends SearchFieldConverter<?>> converterClass = sbField.converter();

                // convert to basic types
                boolean basicConversionFailed = false;
                if (!ClassUtil.instanceOf(value, fieldType)) {
                    // Don't change the type of value if we have a non-default converter
                    // instead, let the converter do it's work
                    //if (ClassUtil.classImplements(fieldType, Collection.class) )
                    if (ClassUtil.classImplements(fieldType, Collection.class)
                            && converterClass.equals(DefaultSearchFieldConverter.class)) {
                        ArrayList listValue = new ArrayList(1);
                        listValue.add(value);
                        value = listValue;
                    } else if (fieldType.equals(String.class)) {
                        value = value.toString();
                    } else if (fieldType.equals(DateTime.class) && value.getClass().equals(Date.class)) {
                        value = new DateTime((Date) value);
                    } else if (fieldType.equals(DateTime.class) && value.getClass().equals(String.class)) {
                        value = new DateTime(value.toString());
                    } else if (ClassUtil.instanceOf(fieldType, Enum.class)) {
                        value = Enum.valueOf(fieldType, value.toString());
                    } else if (fieldType.equals(int.class) && value.getClass().equals(Integer.class)) {
                        value = ((Integer) value).intValue();
                    } else
                        // if basic conversion fails, the converter might still
                        // save the day
                        basicConversionFailed = true;
                }

                if (!converterClass.equals(DefaultSearchFieldConverter.class)) {
                    SearchFieldConverter<?> converter = converterClass.newInstance();
                    value = converter.fromFieldValue(value);
                } else {
                    if (basicConversionFailed)
                        // converter did not save the day
                        throw new DocumentReturnedInvalidTypeException(
                                "expected " + fieldType.toString() + " but got " + value.getClass().toString());
                }

                setMethod.invoke(searchBean, value);
            } catch (Exception e) {
                throw new SearchBeanException(e);
            }
        } else {
            SearchField sbField = classField.getAnnotation(SearchField.class);
            if (sbField.required())
                throw new MissingRequiredFieldException(sbField.name());
        }
    }

    return searchBean;
}

From source file:org.apache.hadoop.hive.ql.parse.ExtractTmpSemanticAnalyzer.java

private Operator createFileSinkOperator(QB qb, Operator<? extends OperatorDesc> input)
        throws SemanticException {

    String tmpName = RSToTmpName.get(input.getChildOperators().get(0));
    String dest = "insclause-" + tmpName;

    ASTNode tab = new ASTNode(new CommonToken(HiveParser.TOK_TAB, "TOK_TAB"));
    ASTNode tabname = new ASTNode(new CommonToken(HiveParser.TOK_TABNAME, "TOK_TABNAME"));
    ASTNode crtTmpTab = new ASTNode(new CommonToken(HiveParser.Identifier, tmpName));
    tab.addChild(tabname);/*  w  ww .  jav  a  2  s .  c  o m*/
    tabname.addChild(crtTmpTab);
    crtTmpTab.setParent(tabname);
    tabname.setParent(tab);
    tableSpec ts = new tableSpec(db, conf, tab);

    RowResolver inputRR = opParseCtx.get(input).getRowResolver();
    QBMetaData qbm = qb.getMetaData();

    if (ts.tableHandle.isView()) {
        throw new SemanticException(ErrorMsg.DML_AGAINST_VIEW.getMsg());
    }

    Class<?> outputFormatClass = ts.tableHandle.getOutputFormatClass();
    if (!HiveOutputFormat.class.isAssignableFrom(outputFormatClass)) {
        throw new SemanticException(ErrorMsg.INVALID_OUTPUT_FORMAT_TYPE.getMsg(ast,
                "The class is " + outputFormatClass.toString()));
    }

    // tableSpec ts is got from the query (user specified),
    // which means the user didn't specify partitions in their query,
    // but whether the table itself is partitioned is not know.
    if (ts.specType != SpecType.STATIC_PARTITION) {
        // This is a table or dynamic partition
        qb.getMetaData().setDestForAlias(dest, ts.tableHandle);
        // has dynamic as well as static partitions
        if (ts.partSpec != null && ts.partSpec.size() > 0) {
            qb.getMetaData().setPartSpecForAlias(dest, ts.partSpec);
        }
    } else {
        // This is a partition
        qb.getMetaData().setDestForAlias(dest, ts.partHandle);
    }
    if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVESTATSAUTOGATHER)) {
        // Set that variable to automatically collect stats during the MapReduce job
        qb.getParseInfo().setIsInsertToTable(true);
        // Add the table spec for the destination table.
        qb.getParseInfo().addTableSpec(ts.tableName.toLowerCase(), ts);
    }

    Table dest_tab = null; // destination table if any
    Partition dest_part = null;// destination partition if any
    String queryTmpdir = null; // the intermediate destination directory
    Path dest_path = null; // the final destination directory
    TableDesc table_desc = null;
    int currentTableId = 0;
    boolean isLocal = false;
    SortBucketRSCtx rsCtx = new SortBucketRSCtx();
    DynamicPartitionCtx dpCtx = null;
    LoadTableDesc ltd = null;
    boolean holdDDLTime = checkHoldDDLTime(qb);
    ListBucketingCtx lbCtx = null;

    dest_tab = ts.tableHandle;
    // Is the user trying to insert into a external tables
    if ((!conf.getBoolVar(HiveConf.ConfVars.HIVE_INSERT_INTO_EXTERNAL_TABLES))
            && (dest_tab.getTableType().equals(TableType.EXTERNAL_TABLE))) {
        throw new SemanticException(ErrorMsg.INSERT_EXTERNAL_TABLE.getMsg(dest_tab.getTableName()));
    }

    Map<String, String> partSpec = ts.partSpec;
    dest_path = dest_tab.getPath();

    // check for partition
    List<FieldSchema> parts = dest_tab.getPartitionKeys();
    if (parts != null && parts.size() > 0) { // table is partitioned
        if (partSpec == null || partSpec.size() == 0) { // user did NOT specify partition
            throw new SemanticException(generateErrorMessage(qb.getParseInfo().getDestForClause(dest),
                    ErrorMsg.NEED_PARTITION_ERROR.getMsg()));
        }
        // the HOLD_DDLTIIME hint should not be used with dynamic partition since the
        // newly generated partitions should always update their DDLTIME
        if (holdDDLTime) {
            throw new SemanticException(generateErrorMessage(qb.getParseInfo().getDestForClause(dest),
                    ErrorMsg.HOLD_DDLTIME_ON_NONEXIST_PARTITIONS.getMsg()));
        }
        dpCtx = qbm.getDPCtx(dest);
        if (dpCtx == null) {
            Utilities.validatePartSpec(dest_tab, partSpec);
            dpCtx = new DynamicPartitionCtx(dest_tab, partSpec,
                    conf.getVar(HiveConf.ConfVars.DEFAULTPARTITIONNAME),
                    conf.getIntVar(HiveConf.ConfVars.DYNAMICPARTITIONMAXPARTSPERNODE));
            qbm.setDPCtx(dest, dpCtx);
        }

        if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.DYNAMICPARTITIONING)) { // allow DP
            // turn on hive.task.progress to update # of partitions created to the JT
            HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVEJOBPROGRESS, true);

        } else { // QBMetaData.DEST_PARTITION capture the all-SP case
            throw new SemanticException(generateErrorMessage(qb.getParseInfo().getDestForClause(dest),
                    ErrorMsg.DYNAMIC_PARTITION_DISABLED.getMsg()));
        }
        if (dpCtx.getSPPath() != null) {
            dest_path = new Path(dest_tab.getPath(), dpCtx.getSPPath());
        }
        if ((dest_tab.getNumBuckets() > 0) && (conf.getBoolVar(HiveConf.ConfVars.HIVEENFORCEBUCKETING))) {
            dpCtx.setNumBuckets(dest_tab.getNumBuckets());
        }
    }

    boolean isNonNativeTable = dest_tab.isNonNative();
    //boolean isNonNativeTable = false;
    if (isNonNativeTable) {
        queryTmpdir = dest_path.toUri().getPath();
    } else {
        queryTmpdir = ctx.getExternalTmpFileURI(dest_path.toUri());
    }
    if (dpCtx != null) {
        // set the root of the temporay path where dynamic partition columns will populate
        dpCtx.setRootPath(queryTmpdir);
    }
    // this table_desc does not contain the partitioning columns
    table_desc = Utilities.getTableDesc(dest_tab);

    // Add sorting/bucketing if needed
    input = genBucketingSortingDest(dest, input, qb, table_desc, dest_tab, rsCtx);

    idToTableNameMap.put(String.valueOf(destTableId), dest_tab.getTableName());
    currentTableId = destTableId;
    destTableId++;

    lbCtx = constructListBucketingCtx(dest_tab.getSkewedColNames(), dest_tab.getSkewedColValues(),
            dest_tab.getSkewedColValueLocationMaps(), dest_tab.isStoredAsSubDirectories(), conf);

    // Create the work for moving the table
    // NOTE: specify Dynamic partitions in dest_tab for WriteEntity
    if (!isNonNativeTable) {
        ltd = new LoadTableDesc(queryTmpdir, ctx.getExternalTmpFileURI(dest_path.toUri()), table_desc, dpCtx);
        ltd.setReplace(!qb.getParseInfo().isInsertIntoTable(dest_tab.getDbName(), dest_tab.getTableName()));
        ltd.setLbCtx(lbCtx);

        if (holdDDLTime) {
            LOG.info("this query will not update transient_lastDdlTime!");
            ltd.setHoldDDLTime(true);
        }
        loadTableWork.add(ltd);
    }

    WriteEntity output = null;

    // Here only register the whole table for post-exec hook if no DP present
    // in the case of DP, we will register WriteEntity in MoveTask when the
    // list of dynamically created partitions are known.
    if ((dpCtx == null || dpCtx.getNumDPCols() == 0)) {
        output = new WriteEntity(dest_tab);
        if (!outputs.add(output)) {
            throw new SemanticException(
                    ErrorMsg.OUTPUT_SPECIFIED_MULTIPLE_TIMES.getMsg(dest_tab.getTableName()));
        }
    }
    if ((dpCtx != null) && (dpCtx.getNumDPCols() >= 0)) {
        // No static partition specified
        if (dpCtx.getNumSPCols() == 0) {
            output = new WriteEntity(dest_tab, false);
            outputs.add(output);
        }
        // part of the partition specified
        // Create a DummyPartition in this case. Since, the metastore does not store partial
        // partitions currently, we need to store dummy partitions
        else {
            try {
                String ppath = dpCtx.getSPPath();
                ppath = ppath.substring(0, ppath.length() - 1);
                DummyPartition p = new DummyPartition(dest_tab,
                        dest_tab.getDbName() + "@" + dest_tab.getTableName() + "@" + ppath, partSpec);
                output = new WriteEntity(p, false);
                outputs.add(output);
            } catch (HiveException e) {
                throw new SemanticException(e.getMessage(), e);
            }
        }
    }

    ctx.getLoadTableOutputMap().put(ltd, output);

    input = genConversionSelectOperator(dest, qb, input, table_desc, dpCtx);
    inputRR = opParseCtx.get(input).getRowResolver();

    ArrayList<ColumnInfo> vecCol = new ArrayList<ColumnInfo>();

    try {
        StructObjectInspector rowObjectInspector = (StructObjectInspector) table_desc.getDeserializer()
                .getObjectInspector();
        List<? extends StructField> fields = rowObjectInspector.getAllStructFieldRefs();
        for (int i = 0; i < fields.size(); i++) {
            vecCol.add(new ColumnInfo(fields.get(i).getFieldName(),
                    TypeInfoUtils.getTypeInfoFromObjectInspector(fields.get(i).getFieldObjectInspector()), "",
                    false));
        }
    } catch (Exception e) {
        throw new SemanticException(e.getMessage(), e);
    }

    RowSchema fsRS = new RowSchema(vecCol);

    // The output files of a FileSink can be merged if they are either not being written to a table
    // or are being written to a table which is either not bucketed or enforce bucketing is not set
    // and table the table is either not sorted or enforce sorting is not set
    boolean canBeMerged = (dest_tab == null
            || !((dest_tab.getNumBuckets() > 0 && conf.getBoolVar(HiveConf.ConfVars.HIVEENFORCEBUCKETING))
                    || (dest_tab.getSortCols() != null && dest_tab.getSortCols().size() > 0
                            && conf.getBoolVar(HiveConf.ConfVars.HIVEENFORCESORTING))));

    FileSinkDesc fileSinkDesc = new FileSinkDesc(queryTmpdir, table_desc,
            conf.getBoolVar(HiveConf.ConfVars.COMPRESSRESULT), currentTableId, rsCtx.isMultiFileSpray(),
            canBeMerged, rsCtx.getNumFiles(), rsCtx.getTotalFiles(), rsCtx.getPartnCols(), dpCtx);

    /* Set List Bucketing context. */
    if (lbCtx != null) {
        lbCtx.processRowSkewedIndex(fsRS);
        lbCtx.calculateSkewedValueSubDirList();
    }
    fileSinkDesc.setLbCtx(lbCtx);

    // set it in plan instead of runtime in FileSinkOperator
    fileSinkDesc.setStatsCollectRawDataSize(
            HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_STATS_COLLECT_RAWDATASIZE));

    // set the stats publishing/aggregating key prefix
    // the same as directory name. The directory name
    // can be changed in the optimizer but the key should not be changed
    // it should be the same as the MoveWork's sourceDir.
    fileSinkDesc.setStatsAggPrefix(fileSinkDesc.getDirName());

    if (dest_part != null) {
        try {
            String staticSpec = Warehouse.makePartPath(dest_part.getSpec());
            fileSinkDesc.setStaticSpec(staticSpec);
        } catch (MetaException e) {
            throw new SemanticException(e);
        }
    } else if (dpCtx != null) {
        fileSinkDesc.setStaticSpec(dpCtx.getSPPath());
    }

    Operator ret = putOpInsertMap(OperatorFactory.getAndMakeChild(fileSinkDesc, fsRS, input), inputRR);

    if (ltd != null && SessionState.get() != null) {
        SessionState.get().getLineageState().mapDirToFop(ltd.getSourceDir(), (FileSinkOperator) ret);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Created FileSink Plan for clause: " + dest + "dest_path: " + dest_path + " row schema: "
                + inputRR.toString());
    }

    fsopToTable.put((FileSinkOperator) ret, dest_tab);
    return ret;

}

From source file:org.broadinstitute.sting.WalkerTest.java

/**
 * execute the test, given the following:
 * @param name     the name of the test//  ww w .j  a  va 2  s  .  co m
 * @param args     the argument list
 * @param expectedException the expected exception or null
 */
private void executeTest(String name, String args, Class expectedException) {
    CommandLineGATK instance = new CommandLineGATK();
    String[] command = Utils.escapeExpressions(args);

    // run the executable
    boolean gotAnException = false;
    try {
        final String now = new SimpleDateFormat("HH:mm:ss").format(new Date());
        final String cmdline = Utils.join(" ", command);
        System.out.println(String.format("[%s] Executing test %s with GATK arguments: %s", now, name, cmdline));
        // also write the command line to the HTML log for convenient follow-up
        // do the replaceAll so paths become relative to the current
        BaseTest.log(cmdline.replaceAll(publicTestDirRoot, "").replaceAll(privateTestDirRoot, ""));
        CommandLineExecutable.start(instance, command);
    } catch (Exception e) {
        gotAnException = true;
        if (expectedException != null) {
            // we expect an exception
            //System.out.println(String.format("Wanted exception %s, saw %s", expectedException, e.getClass()));
            if (expectedException.isInstance(e)) {
                // it's the type we expected
                //System.out.println(String.format("  => %s PASSED", name));
            } else {
                final String message = String.format(
                        "Test %s expected exception %s but instead got %s with error message %s", name,
                        expectedException, e.getClass(), e.getMessage());
                if (e.getCause() != null) {
                    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    final PrintStream ps = new PrintStream(baos);
                    e.getCause().printStackTrace(ps);
                    BaseTest.log(message);
                    BaseTest.log(baos.toString());
                }
                Assert.fail(message);
            }
        } else {
            // we didn't expect an exception but we got one :-(
            throw new RuntimeException(e);
        }
    }

    // catch failures from the integration test
    if (expectedException != null) {
        if (!gotAnException)
            // we expected an exception but didn't see it
            Assert.fail(String.format("Test %s expected exception %s but none was thrown", name,
                    expectedException.toString()));
    } else {
        if (CommandLineExecutable.result != 0) {
            throw new RuntimeException("Error running the GATK with arguments: " + args);
        }
    }
}