Example usage for java.lang Enum getClass

List of usage examples for java.lang Enum getClass

Introduction

In this page you can find the example usage for java.lang Enum getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.jhkt.playgroundArena.db.nosql.mongodb.beans.AbstractDocument.java

public final BasicDBObject resolveToBasicDBObject() {

    BasicDBObject bDBObject = new BasicDBObject();
    try {//from  w w w  .  j  ava  2s.  co m
        Class<? extends AbstractDocument> currClass = getClass();
        while (currClass != null) {

            for (Method method : currClass.getMethods()) {
                IDocumentKeyValue dkv = method.getAnnotation(IDocumentKeyValue.class);
                String mName = method.getName();
                if (dkv != null && mName.startsWith(JPAConstants.GETTER_PREFIX)) {

                    try {
                        Object returnValue = method.invoke(this, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                        char[] propertyChars = mName.substring(3).toCharArray();
                        String property = String.valueOf(propertyChars[0]).toLowerCase()
                                + String.valueOf(propertyChars, 1, propertyChars.length - 1);

                        if (returnValue == null) {
                            continue;
                        }

                        if (returnValue instanceof AbstractDocument) {

                            Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(returnValue,
                                    JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                            bDBObject.put(property, subReturnValue);

                        } else if (returnValue instanceof Enum) {

                            Enum<?> enumClass = Enum.class.cast(returnValue);
                            BasicDBObject enumObject = new BasicDBObject();

                            enumObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    enumClass.getClass().getName());
                            enumObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), enumClass.name());

                            bDBObject.put(property, enumObject);

                        } else if (returnValue instanceof Collection) {

                            Collection<?> collectionContent = (Collection<?>) returnValue;
                            BasicDBObject collectionObject = new BasicDBObject();
                            collectionObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    collectionContent.getClass().getName());

                            BasicDBList bDBList = new BasicDBList();
                            if (collectionContent.iterator().next() instanceof AbstractDocument) {
                                for (Object content : collectionContent) {
                                    if (content instanceof AbstractDocument) {
                                        Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD
                                                .invoke(returnValue, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);
                                        bDBList.add(subReturnValue);
                                    }
                                }
                            } else {
                                bDBList.addAll(collectionContent);
                            }

                            collectionObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), bDBList);
                            bDBObject.put(property, collectionObject);

                        } else if (returnValue instanceof Map) {

                            Map<?, ?> mapContent = (Map<?, ?>) returnValue;
                            BasicDBObject mapObject = new BasicDBObject();
                            mapObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(),
                                    mapContent.getClass().getName());

                            Set<?> keys = mapContent.keySet();
                            if (keys.iterator().next() instanceof AbstractDocument) {

                                Map<Object, Object> convertedMap = new HashMap<Object, Object>();
                                for (Object key : keys) {
                                    Object value = mapContent.get(key);
                                    Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(value,
                                            JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST);

                                    convertedMap.put(key, subReturnValue);
                                }

                                mapContent = convertedMap;
                            }

                            mapObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), mapContent);
                            bDBObject.put(property, mapObject);

                        } else {
                            bDBObject.put(property, returnValue);
                        }

                    } catch (Exception e) {

                    }

                }
            }

            currClass = currClass.getSuperclass().asSubclass(AbstractDocument.class);
        }

    } catch (ClassCastException castException) {

    }

    bDBObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), getClass().getName());
    _log.info("BdBObject " + bDBObject);
    return bDBObject;
}

From source file:com.evolveum.midpoint.prism.marshaller.BeanMarshaller.java

private XNode marshalEnum(Enum enumValue, SerializationContext ctx) {
    Class<? extends Enum> enumClass = enumValue.getClass();
    String enumStringValue = inspector.findEnumFieldValue(enumClass, enumValue.toString());
    if (StringUtils.isEmpty(enumStringValue)) {
        enumStringValue = enumValue.toString();
    }//from   www . j  a v a 2  s  . c o  m
    QName fieldTypeName = inspector.findTypeName(null, enumClass, DEFAULT_PLACEHOLDER);
    return createPrimitiveXNode(enumStringValue, fieldTypeName, false);

}

From source file:foam.lib.json.Outputter.java

public void outputEnum(Enum<?> value) {
    //    outputNumber(value.ordinal());

    writer_.append("{");
    writer_.append(beforeKey_());//from   www . j ava2  s. c  om
    writer_.append("class");
    writer_.append(afterKey_());
    writer_.append(":");
    outputString(value.getClass().getName());
    writer_.append(",");
    writer_.append(beforeKey_());
    writer_.append("ordinal");
    writer_.append(afterKey_());
    writer_.append(":");
    outputNumber(value.ordinal());
    writer_.append("}");
}

From source file:com.haulmont.cuba.core.sys.AbstractMessages.java

@Override
public String getMessage(Enum caller, Locale locale) {
    checkNotNullArgument(caller, "Enum parameter 'caller' is null");

    String className = caller.getClass().getName();
    int i = className.lastIndexOf('.');
    if (i > -1)
        className = className.substring(i + 1);
    // If enum has inner subclasses, its class name ends with "$1", "$2", ... suffixes. Cut them off.
    Matcher matcher = enumSubclassPattern.matcher(className);
    if (matcher.find()) {
        className = className.substring(0, matcher.start());
    }/* ww w . j ava 2s  . co m*/

    return getMessage(getPackName(caller.getClass()), className + "." + caller.name(), locale);
}

From source file:cn.annoreg.mc.s11n.SerializationManager.java

private void initInternalSerializers() {
    //First part: java internal class.
    {/*from w w w .j  a v a  2  s.com*/
        InstanceSerializer ser = new InstanceSerializer<Enum>() {
            @Override
            public Enum readInstance(NBTBase nbt) throws Exception {
                NBTTagCompound tag = (NBTTagCompound) nbt;
                try {
                    Class enumClass = Class.forName(tag.getString("class"));
                    Object[] objs = (Object[]) enumClass.getMethod("values").invoke(null);

                    return (Enum) objs[tag.getInteger("ordinal")];
                } catch (Exception e) {
                    ARModContainer.log.error("Failed in enum deserialization. Class: {}.",
                            tag.getString("class"));
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            public NBTBase writeInstance(Enum obj) throws Exception {
                NBTTagCompound ret = new NBTTagCompound();
                ret.setString("class", obj.getClass().getName());
                ret.setByte("ordinal", (byte) ((Enum) obj).ordinal());
                return ret;
            }
        };
        setInstanceSerializerFor(Enum.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Byte>() {
            @Override
            public Byte readData(NBTBase nbt, Byte obj) throws Exception {
                return ((NBTTagByte) nbt).func_150290_f();
            }

            @Override
            public NBTBase writeData(Byte obj) throws Exception {
                return new NBTTagByte(obj);
            }
        };
        setDataSerializerFor(Byte.TYPE, ser);
        setDataSerializerFor(Byte.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Byte[]>() {
            @Override
            public Byte[] readData(NBTBase nbt, Byte[] obj) throws Exception {
                return ArrayUtils.toObject(((NBTTagByteArray) nbt).func_150292_c());
            }

            @Override
            public NBTBase writeData(Byte[] obj) throws Exception {
                return new NBTTagByteArray(ArrayUtils.toPrimitive(obj));
            }
        };
        setDataSerializerFor(Byte[].class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<byte[]>() {
            @Override
            public byte[] readData(NBTBase nbt, byte[] obj) throws Exception {
                return ((NBTTagByteArray) nbt).func_150292_c();
            }

            @Override
            public NBTBase writeData(byte[] obj) throws Exception {
                return new NBTTagByteArray(obj);
            }
        };
        setDataSerializerFor(byte[].class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Double>() {
            @Override
            public Double readData(NBTBase nbt, Double obj) throws Exception {
                return ((NBTTagDouble) nbt).func_150286_g();
            }

            @Override
            public NBTBase writeData(Double obj) throws Exception {
                return new NBTTagDouble(obj);
            }
        };
        setDataSerializerFor(Double.TYPE, ser);
        setDataSerializerFor(Double.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Float>() {
            @Override
            public Float readData(NBTBase nbt, Float obj) throws Exception {
                return ((NBTTagFloat) nbt).func_150288_h();
            }

            @Override
            public NBTBase writeData(Float obj) throws Exception {
                return new NBTTagFloat(obj);
            }
        };
        setDataSerializerFor(Float.TYPE, ser);
        setDataSerializerFor(Float.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Integer>() {
            @Override
            public Integer readData(NBTBase nbt, Integer obj) throws Exception {
                return ((NBTTagInt) nbt).func_150287_d();
            }

            @Override
            public NBTBase writeData(Integer obj) throws Exception {
                return new NBTTagInt(obj);
            }
        };
        setDataSerializerFor(Integer.TYPE, ser);
        setDataSerializerFor(Integer.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Integer[]>() {
            @Override
            public Integer[] readData(NBTBase nbt, Integer[] obj) throws Exception {
                return ArrayUtils.toObject(((NBTTagIntArray) nbt).func_150302_c());
            }

            @Override
            public NBTBase writeData(Integer[] obj) throws Exception {
                return new NBTTagIntArray(ArrayUtils.toPrimitive(obj));
            }
        };
        setDataSerializerFor(Integer[].class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<int[]>() {
            @Override
            public int[] readData(NBTBase nbt, int[] obj) throws Exception {
                return ((NBTTagIntArray) nbt).func_150302_c();
            }

            @Override
            public NBTBase writeData(int[] obj) throws Exception {
                return new NBTTagIntArray(obj);
            }
        };
        setDataSerializerFor(int[].class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Long>() {
            @Override
            public Long readData(NBTBase nbt, Long obj) throws Exception {
                return ((NBTTagLong) nbt).func_150291_c();
            }

            @Override
            public NBTBase writeData(Long obj) throws Exception {
                return new NBTTagLong(obj);
            }
        };
        setDataSerializerFor(Long.TYPE, ser);
        setDataSerializerFor(Long.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Short>() {
            @Override
            public Short readData(NBTBase nbt, Short obj) throws Exception {
                return ((NBTTagShort) nbt).func_150289_e();
            }

            @Override
            public NBTBase writeData(Short obj) throws Exception {
                return new NBTTagShort(obj);
            }
        };
        setDataSerializerFor(Short.TYPE, ser);
        setDataSerializerFor(Short.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<String>() {
            @Override
            public String readData(NBTBase nbt, String obj) throws Exception {
                return ((NBTTagString) nbt).func_150285_a_();
            }

            @Override
            public NBTBase writeData(String obj) throws Exception {
                return new NBTTagString(obj);
            }
        };
        setDataSerializerFor(String.class, ser);
    }
    {
        //TODO: Maybe there is a more data-friendly method?
        DataSerializer ser = new DataSerializer<Boolean>() {
            @Override
            public Boolean readData(NBTBase nbt, Boolean obj) throws Exception {
                return ((NBTTagCompound) nbt).getBoolean("v");
            }

            @Override
            public NBTBase writeData(Boolean obj) throws Exception {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setBoolean("v", obj);
                return tag;
            }
        };
        setDataSerializerFor(Boolean.class, ser);
        setDataSerializerFor(Boolean.TYPE, ser);
    }

    //Second part: Minecraft objects.
    {
        DataSerializer ser = new DataSerializer<NBTTagCompound>() {
            @Override
            public NBTTagCompound readData(NBTBase nbt, NBTTagCompound obj) throws Exception {
                return (NBTTagCompound) nbt;
            }

            @Override
            public NBTBase writeData(NBTTagCompound obj) throws Exception {
                return obj;
            }
        };
        setDataSerializerFor(NBTTagCompound.class, ser);
    }
    {
        InstanceSerializer ser = new InstanceSerializer<Entity>() {
            @Override
            public Entity readInstance(NBTBase nbt) throws Exception {
                int[] ids = ((NBTTagIntArray) nbt).func_150302_c();
                World world = SideHelper.getWorld(ids[0]);
                if (world != null) {
                    return world.getEntityByID(ids[1]);
                }
                return null;
            }

            @Override
            public NBTBase writeInstance(Entity obj) throws Exception {
                return new NBTTagIntArray(new int[] { obj.dimension, obj.getEntityId() });
            }
        };
        setInstanceSerializerFor(Entity.class, ser);
    }
    {
        InstanceSerializer ser = new InstanceSerializer<TileEntity>() {
            @Override
            public TileEntity readInstance(NBTBase nbt) throws Exception {
                int[] ids = ((NBTTagIntArray) nbt).func_150302_c();
                World world = SideHelper.getWorld(ids[0]);
                if (world != null) {
                    return world.getTileEntity(ids[1], ids[2], ids[3]);
                }
                return null;
            }

            @Override
            public NBTBase writeInstance(TileEntity obj) throws Exception {
                return new NBTTagIntArray(new int[] { obj.getWorldObj().provider.dimensionId, obj.xCoord,
                        obj.yCoord, obj.zCoord });
            }
        };
        setInstanceSerializerFor(TileEntity.class, ser);
    }
    {
        //TODO this implementation can not be used to serialize player's inventory container.
        InstanceSerializer ser = new InstanceSerializer<Container>() {
            @Override
            public Container readInstance(NBTBase nbt) throws Exception {
                int[] ids = ((NBTTagIntArray) nbt).func_150302_c();
                World world = SideHelper.getWorld(ids[0]);
                if (world != null) {
                    Entity entity = world.getEntityByID(ids[1]);
                    if (entity instanceof EntityPlayer) {
                        return SideHelper.getPlayerContainer((EntityPlayer) entity, ids[2]);
                    }
                }
                return SideHelper.getPlayerContainer(null, ids[2]);
            }

            @Override
            public NBTBase writeInstance(Container obj) throws Exception {
                EntityPlayer player = SideHelper.getThePlayer();
                if (player != null) {
                    //This is on client. The server needs player to get the Container.
                    return new NBTTagIntArray(new int[] { player.worldObj.provider.dimensionId,
                            player.getEntityId(), obj.windowId });
                } else {
                    //This is on server. The client doesn't need player (just use thePlayer), use MAX_VALUE here.
                    return new NBTTagIntArray(new int[] { Integer.MAX_VALUE, 0, obj.windowId });
                }
            }
        };
        setInstanceSerializerFor(Container.class, ser);
    }
    {
        InstanceSerializer ser = new InstanceSerializer<World>() {

            @Override
            public World readInstance(NBTBase nbt) throws Exception {
                return SideHelper.getWorld(((NBTTagInt) nbt).func_150287_d());
            }

            @Override
            public NBTBase writeInstance(World obj) throws Exception {
                return new NBTTagInt(obj.provider.dimensionId);
            }

        };
        setInstanceSerializerFor(World.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<ItemStack>() {
            @Override
            public ItemStack readData(NBTBase nbt, ItemStack obj) throws Exception {
                if (obj == null) {
                    return ItemStack.loadItemStackFromNBT((NBTTagCompound) nbt);
                } else {
                    obj.readFromNBT((NBTTagCompound) nbt);
                    return obj;
                }
            }

            @Override
            public NBTBase writeData(ItemStack obj) throws Exception {
                NBTTagCompound nbt = new NBTTagCompound();
                obj.writeToNBT(nbt);
                return nbt;
            }
        };
        setDataSerializerFor(ItemStack.class, ser);
    }
    {
        DataSerializer ser = new DataSerializer<Vec3>() {
            @Override
            public Vec3 readData(NBTBase nbt, Vec3 obj) throws Exception {
                NBTTagCompound tag = (NBTTagCompound) nbt;
                return Vec3.createVectorHelper(tag.getFloat("x"), tag.getFloat("y"), tag.getFloat("z"));
            }

            @Override
            public NBTBase writeData(Vec3 obj) throws Exception {
                NBTTagCompound nbt = new NBTTagCompound();
                nbt.setFloat("x", (float) obj.xCoord);
                nbt.setFloat("y", (float) obj.yCoord);
                nbt.setFloat("z", (float) obj.zCoord);
                return nbt;
            }
        };
        setDataSerializerFor(Vec3.class, ser);
    }
    //network part
    {
        DataSerializer ser = new DataSerializer<NetworkTerminal>() {
            @Override
            public NetworkTerminal readData(NBTBase nbt, NetworkTerminal obj) throws Exception {
                return NetworkTerminal.fromNBT(nbt);
            }

            @Override
            public NBTBase writeData(NetworkTerminal obj) throws Exception {
                return obj.toNBT();
            }
        };
        setDataSerializerFor(NetworkTerminal.class, ser);
    }
    {
        Future.FutureSerializer ser = new Future.FutureSerializer();
        setDataSerializerFor(Future.class, ser);
        setInstanceSerializerFor(Future.class, ser);
    }
    //misc
    {
        DataSerializer ser = new DataSerializer<BitSet>() {

            @Override
            public BitSet readData(NBTBase nbt, BitSet obj) throws Exception {
                NBTTagCompound tag = (NBTTagCompound) nbt;
                BitSet ret = BitSet.valueOf(tag.getByteArray("l"));
                return ret;
            }

            @Override
            public NBTBase writeData(BitSet obj) throws Exception {
                NBTTagCompound tag = new NBTTagCompound();
                byte[] barray = obj.toByteArray();
                tag.setByteArray("l", barray);
                return tag;
            }

        };

        setDataSerializerFor(BitSet.class, ser);
    }
}

From source file:com.evolveum.midpoint.repo.sql.query.restriction.ItemRestriction.java

private Enum getRepoEnumValue(Enum schemaValue, Class repoType) throws QueryException {
    if (schemaValue == null) {
        return null;
    }//from   w w w  . java  2 s . c o m

    if (SchemaEnum.class.isAssignableFrom(repoType)) {
        return (Enum) RUtil.getRepoEnumValue(schemaValue, repoType);
    }

    Object[] constants = repoType.getEnumConstants();
    for (Object constant : constants) {
        Enum e = (Enum) constant;
        if (e.name().equals(schemaValue.name())) {
            return e;
        }
    }

    throw new QueryException(
            "Unknown enum value '" + schemaValue + "', which is type of '" + schemaValue.getClass() + "'.");
}

From source file:com.complexible.pinto.RDFMapper.java

private IRI enumToURI(final Enum theEnum) {
    try {/*from www.  j a v  a 2s  . c  o m*/
        final Iri aAnnotation = theEnum.getClass().getField(theEnum.name()).getAnnotation(Iri.class);

        if (aAnnotation != null) {
            return iri(aAnnotation.value());
        } else {
            return mValueFactory.createIRI(mDefaultNamespace, theEnum.name());
        }
    } catch (NoSuchFieldException e) {
        throw new AssertionError();
    }
}

From source file:com.flexive.shared.FxSharedUtils.java

/**
 * Returns the localized label for the given enum value. The enum translations are
 * stored in FxSharedMessages.properties and are standardized as
 * {@code FQCN.value},/*  www  . java 2  s .  c  o m*/
 * e.g. {@code com.flexive.shared.search.query.ValueComparator.LIKE}.
 *
 * @param value the enum value to be translated
 * @param args  optional arguments to be replaced in the localized messages
 * @return the localized label for the given enum value
 */
public static FxString getEnumLabel(Enum<?> value, Object... args) {
    final Class<? extends Enum> valueClass = value.getClass();
    final String clsName;
    if (valueClass.getEnclosingClass() != null && Enum.class.isAssignableFrom(valueClass.getEnclosingClass())) {
        // don't include anonymous inner class definitions often used by enums in class name
        clsName = valueClass.getEnclosingClass().getName();
    } else {
        clsName = valueClass.getName();
    }
    return getMessage(SHARED_BUNDLE, clsName + "." + value.name(), args);
}

From source file:mondrian.olap.Util.java

/**
 * Returns an exception indicating that we didn't expect to find this value
 * here./*from ww  w . j av  a2  s . com*/
 *
 * @param value Value
 */
public static RuntimeException unexpected(Enum value) {
    return Util.newInternal("Was not expecting value '" + value + "' for enumeration '"
            + value.getClass().getName() + "' in this context");
}