Example usage for java.lang InstantiationException InstantiationException

List of usage examples for java.lang InstantiationException InstantiationException

Introduction

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

Prototype

public InstantiationException() 

Source Link

Document

Constructs an InstantiationException with no detail message.

Usage

From source file:org.spout.api.protocol.CodecLookupService.java

/**
 * Binds a codec by adding entries for it to the tables.
 * TODO: if a dynamic opcode is registered then a static opcode tries to register, reassign dynamic.
 * TODO: if a static opcode is registered then a static opcode tries to register, throw exception
 *
 * @param clazz The codec's class./* w ww .  j a  v a  2s. c o  m*/
 * @param <T> The type of message.
 * @param <C> The type of codec.
 * @throws InstantiationException if the codec could not be instantiated.
 * @throws IllegalAccessException if the codec could not be instantiated due to an access violation.
 */
@SuppressWarnings("unchecked")
protected <T extends Message, C extends MessageCodec<T>> C bind(Class<C> clazz)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    boolean dynamicId = false;
    Constructor<C> constructor;
    try {
        constructor = clazz.getConstructor();
    } catch (NoSuchMethodException e) {
        try {
            constructor = clazz.getConstructor(int.class);
            dynamicId = true;
        } catch (NoSuchMethodException e1) {
            throw (InstantiationException) new InstantiationException().initCause(e1);
        }
    }
    if (dynamicPacketMap.getKeys().contains(clazz.getName())) {
        //Already bound, return codec
        return (C) find(dynamicPacketMap.register(clazz.getName()));
    }
    final C codec;
    if (dynamicId) {
        int id;
        do {
            id = nextId.getAndIncrement();
        } while (find(id) != null);
        codec = constructor.newInstance(id);
        codec.setDynamic(true);
    } else {
        //Codec is static
        codec = constructor.newInstance();
        final MessageCodec<?> prevCodec = find(codec.getOpcode());
        if (prevCodec != null) {
            throw new IllegalStateException("Trying to bind a static opcode where one already exists. Static: "
                    + clazz.getSimpleName() + " Other: " + prevCodec.getClass().getSimpleName());
        }
    }
    register(codec);
    dynamicPacketMap.register(clazz.getName(), codec.getOpcode());
    return codec;
}

From source file:egovframework.rte.itl.webservice.service.impl.MessageConverterImpl.java

public Object convertToTypedObject(Object source, Type type)
        throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InstantiationException {
    LOG.debug("convertToTypedObject(source = " + source + ", type = " + type + ")");

    if (type instanceof PrimitiveType) {
        LOG.debug("Type is a Primitive Type");
        return source;
    } else if (type instanceof ListType) {
        LOG.debug("Type is a List Type");

        ListType listType = (ListType) type;
        Object[] components = (Object[]) source;

        List<Object> list = new ArrayList<Object>();

        for (Object component : components) {
            list.add(convertToTypedObject(component, listType.getElementType()));
        }// ww w  .  j a  va  2  s  .  c om
        return list;
    } else if (type instanceof RecordType) {
        LOG.debug("Type is a Record(Map) Type");

        RecordType recordType = (RecordType) type;

        Class<?> recordClass = classLoader.loadClass(recordType);
        Map<String, Object> map = new HashMap<String, Object>();

        for (Entry<String, Type> entry : recordType.getFieldTypes().entrySet()) {
            Object fieldValue = recordClass.getField(entry.getKey()).get(source);
            map.put(entry.getKey(), convertToTypedObject(fieldValue, entry.getValue()));
        }
        return map;
    }
    LOG.error("Type is invalid");
    throw new InstantiationException();
}

From source file:org.kuali.kpme.tklm.leave.accrual.bucket.KPMEAccrualCategoryBucket.java

public void initialize(PrincipalHRAttributesBo currentPrincipalCalendar) throws InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    leaveBalances = new LinkedHashMap<String, List<LeaveBalance>>();
    principalCalendar = currentPrincipalCalendar;
    asOfDate = LocalDate.now();//from  w ww .  j a  v a 2  s  . c  om
    List<AccrualCategory> accrualCategories = HrServiceLocator.getAccrualCategoryService()
            .getActiveAccrualCategoriesForLeavePlan(currentPrincipalCalendar.getLeavePlan(), asOfDate);
    for (AccrualCategory accrualCategory : accrualCategories) {
        List<LeaveBalance> leaveBalances = new ArrayList<LeaveBalance>();
        for (Class<LeaveBalance> leaveBalanceClazz : baseBalanceList) {
            Constructor<?>[] constructors = leaveBalanceClazz.getConstructors();
            //does this array contain default constructors as well??
            Object[] args = new Object[2];
            args[0] = accrualCategory;
            args[1] = principalCalendar;
            Constructor<?> constructor = constructors[0];
            LeaveBalance myLeaveBalance = (LeaveBalance) constructor.newInstance(args);
            leaveBalances.add(myLeaveBalance);
        }
        this.leaveBalances.put(accrualCategory.getLmAccrualCategoryId(), leaveBalances);
    }
    //This list *could* contain leave blocks for accrual categories that have been deactivated, those from another leave plan, etc
    List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksSinceCarryOver(
            currentPrincipalCalendar.getPrincipalId(), LmServiceLocator.getLeaveBlockService()
                    .getLastCarryOverBlocks(currentPrincipalCalendar.getPrincipalId(), asOfDate),
            LocalDate.now().plusDays(365), true);

    //These maps could also contain such accrual categories within their keyset.
    Map<String, LeaveBlock> carryOverBlocks = LmServiceLocator.getLeaveBlockService()
            .getLastCarryOverBlocks(currentPrincipalCalendar.getPrincipalId(), asOfDate);
    Map<String, List<LeaveBlock>> accrualCategoryMappedLeaveBlocks = mapLeaveBlocksByAccrualCategory(
            leaveBlocks);
    //merge carryOverBlock map with accrualCategoryMappedLeaveBlocks.
    for (Entry<String, LeaveBlock> entry : carryOverBlocks.entrySet()) {
        if (accrualCategoryMappedLeaveBlocks.containsKey(entry.getKey()))
            accrualCategoryMappedLeaveBlocks.get(entry.getKey()).add(entry.getValue());
        else {
            List<LeaveBlock> carryOverList = new ArrayList<LeaveBlock>();
            carryOverList.add(entry.getValue());
            accrualCategoryMappedLeaveBlocks.put(entry.getKey(), carryOverList);
        }
    }
    //add leave blocks, accrual category by accrual category, to bucket.
    for (Entry<String, List<LeaveBlock>> entry : accrualCategoryMappedLeaveBlocks.entrySet()) {
        for (LeaveBlock leaveBlock : entry.getValue()) {
            try {
                addLeaveBlock(leaveBlock);
            } catch (KPMEBalanceException e) {
                InstantiationException ie = new InstantiationException();
                ie.setStackTrace(e.getStackTrace());
                throw ie;
            }
        }
    }
}

From source file:org.jtransfo.internal.ConverterHelperTest.java

@Test
public void testGetDeclaredTypeConverterIe() throws Exception {
    MappedBy mappedBy = mock(MappedBy.class);
    when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class);
    when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName());
    when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD);
    when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName()))
            .thenThrow(new InstantiationException());

    exception.expect(JTransfoException.class);
    exception.expectMessage(/* w w w.j  a  v a2s  .c o  m*/
            "Declared TypeConverter class org.jtransfo.NoConversionTypeConverter cannot be instantiated.");
    converterHelper.getDeclaredTypeConverter(mappedBy);
}

From source file:com.gh.bmd.jrt.android.v4.core.DefaultObjectContextRoutineBuilder.java

@Nonnull
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private static Object getInstance(@Nonnull final Context context, @Nonnull final Class<?> targetClass,
        @Nonnull final Object[] args)
        throws IllegalAccessException, InvocationTargetException, InstantiationException {

    Object target = null;//from w w  w. jav  a 2  s. co  m

    if (context instanceof FactoryContext) {

        // the context here is always the application
        synchronized (context) {

            target = ((FactoryContext) context).geInstance(targetClass, args);
        }
    }

    if (target == null) {

        target = findConstructor(targetClass, args).newInstance(args);

    } else if (!targetClass.isInstance(target)) {

        throw new InstantiationException();
    }

    return target;
}

From source file:org.kuali.kpme.tklm.leave.accrual.bucket.KPMEAccrualCategoryBucket.java

private void adjustBalances(CalendarEntry calendarEntry, DateTime rolloverDate, Set<String> assignmentKeys)
        throws KPMEBalanceException {

    //fetch calendar entries in this sequence belonging to the current leave year.
    String dateString = TKUtils.formatDate(LocalDate.now());
    List<CalendarEntry> calendarEntries = HrServiceLocator.getCalendarEntryService()
            .getAllCalendarEntriesForCalendarIdAndYear(calendarEntry.getHrCalendarId(),
                    dateString.substring(6, 10));

    //calendar change into a different leave calendar year
    if (rolloverDate != null) {

        //clear all balances.
        clearLeaveBalances();//from www  . ja v  a  2  s.  c  o  m

        //reset the asOfDate this bucket will go off of, as well as those of the leave balances
        adjustAsOfDates(calendarEntry, calendarEntries);

        //TODO: if in current calendar year, use current date + planning months for extent of fetch.
        List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksSinceCarryOver(
                principalCalendar.getPrincipalId(),
                LmServiceLocator.getLeaveBlockService().getLastCarryOverBlocks(
                        principalCalendar.getPrincipalId(), rolloverDate.minusDays(1).toLocalDate()),
                rolloverDate.plusDays(365).toLocalDate(), true);

        //retrieve (if any) last carryover blocks given the new asOfDate
        Map<String, LeaveBlock> carryOverBlocks = LmServiceLocator.getLeaveBlockService()
                .getLastCarryOverBlocks(principalCalendar.getPrincipalId(), asOfDate);

        //method taken from leave summary service - map general leave block fetch by accrual category
        Map<String, List<LeaveBlock>> accrualCategoryMappedLeaveBlocks = mapLeaveBlocksByAccrualCategory(
                leaveBlocks);

        //merge carryOverBlock map with accrualCategoryMappedLeaveBlocks.
        for (Entry<String, LeaveBlock> entry : carryOverBlocks.entrySet()) {
            //TODO: modify CarryOverLeaveBalance to identify and make use of CARRY_OVER type leave block
            if (accrualCategoryMappedLeaveBlocks.containsKey(entry.getKey()))
                accrualCategoryMappedLeaveBlocks.get(entry.getKey()).add(entry.getValue());
            else {
                List<LeaveBlock> carryOverList = new ArrayList<LeaveBlock>();
                carryOverList.add(entry.getValue());
                accrualCategoryMappedLeaveBlocks.put(entry.getKey(), carryOverList);
            }

        }
        //add leave blocks, accrual category by accrual category, to this bucket.
        for (Entry<String, List<LeaveBlock>> entry : accrualCategoryMappedLeaveBlocks.entrySet()) {
            for (LeaveBlock leaveBlock : entry.getValue()) {
                try {
                    addLeaveBlock(leaveBlock);
                } catch (KPMEBalanceException e) {
                    InstantiationException ie = new InstantiationException();
                    ie.setStackTrace(e.getStackTrace());
                    ie.printStackTrace();
                }
            }
        }
    } else {
        //Calendar change within the same leave year.

        List<String> assignmentKeyList = new ArrayList<String>();
        assignmentKeyList.addAll(assignmentKeys);

        List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();

        //determine which leave blocks are affected by the calendar change, remove them, and re-add them under the new asOfDate
        if (calendarEntry.getEndPeriodFullDateTime()
                .isBefore(viewingCalendarEntry.getEndPeriodFullDateTime().getMillis())) {
            //need to remove leave blocks from otherLeaveCalendarDocument begin date to thisLeaveCalendarDocument end date
            leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(
                    principalCalendar.getPrincipalId(),
                    calendarEntry.getBeginPeriodFullDateTime().toLocalDate(),
                    viewingCalendarEntry.getEndPeriodFullDateTime().toLocalDate());
            //leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForLeaveCalendar(principalCalendar.getPrincipalId(), LocalDate.fromDateFields(calendarEntry.getBeginPeriodDateTime()), LocalDate.fromDateFields(viewingCalendarEntry.getEndPeriodDateTime()), assignmentKeyList);
        } else {
            //need to remove leave blocks from thisLeaveCalendarDocument begin date to otherLeaveCalendarDocument end date
            leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(
                    principalCalendar.getPrincipalId(),
                    viewingCalendarEntry.getBeginPeriodFullDateTime().toLocalDate(),
                    calendarEntry.getEndPeriodFullDateTime().toLocalDate());
            //leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForLeaveCalendar(principalCalendar.getPrincipalId(), LocalDate.fromDateFields(viewingCalendarEntry.getBeginPeriodDateTime()), LocalDate.fromDateFields(calendarEntry.getEndPeriodDateTime()), assignmentKeyList);
        }
        //remove affected leave blocks
        for (LeaveBlock block : leaveBlocks) {
            removeLeaveBlock(block);
        }

        //update this bucket and its leave balances with new relative date information
        LocalDate newAsOfDate = null;
        DateTime currentLeavePlanStartDate = HrServiceLocator.getLeavePlanService()
                .getFirstDayOfLeavePlan(principalCalendar.getLeavePlan(), LocalDate.now());
        if (calendarEntry.getEndPeriodFullDateTime().isBefore(currentLeavePlanStartDate)) {
            //require balances as of the final day of the leave calendar year.
            DateTime calendarEntryLeavePlanRolloverDate = HrServiceLocator.getLeavePlanService()
                    .getRolloverDayOfLeavePlan(principalCalendar.getLeavePlan(),
                            calendarEntry.getEndPeriodFullDateTime().toLocalDate());
            newAsOfDate = LocalDate.fromDateFields(calendarEntryLeavePlanRolloverDate.toDate()).minusDays(1);
        } else {
            Interval interval = new Interval(calendarEntry.getBeginPeriodFullDateTime(),
                    calendarEntry.getEndPeriodFullDateTime());
            if (interval.contains(LocalDate.now().toDate().getTime())) {
                newAsOfDate = LocalDate.now();
            } else if (calendarEntry.getBeginPeriodFullDateTime().toLocalDate().isBefore(LocalDate.now()))
                newAsOfDate = calendarEntry.getEndPeriodFullDateTime().toLocalDate().minusDays(1);
            else // if it's in the calendar interval above, the equals case is taken care of, begin date must be after today
                newAsOfDate = calendarEntry.getBeginPeriodFullDateTime().toLocalDate().minusDays(1);
        }

        asOfDate = newAsOfDate;

        for (Entry<String, List<LeaveBalance>> leaveBalance : leaveBalances.entrySet()) {
            for (LeaveBalance balance : leaveBalance.getValue()) {
                balance.calendarPeriodBeginDate = calendarEntry.getBeginPeriodFullDateTime().toLocalDate();
                balance.calendarPeriodEndDate = calendarEntry.getEndPeriodFullDateTime().toLocalDate();
                balance.asOfDate = newAsOfDate;
            }
        }
        //re-add the affected leave blocks under the new date / calendar information
        for (LeaveBlock block : leaveBlocks) {
            addLeaveBlock(block);
        }
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private T prepareInstanceAndFetchList(String id, RBatch redissonBatch, ArrayList<String> pList, String parent,
        Boolean deepFetch) throws InstantiationException, IllegalAccessException {
    BeanMap pMap = new BeanMap(cls.newInstance());
    try {/* w w w. j  a  va  2  s .com*/
        pMap.keySet().forEach(e -> {
            if ("class".equals(e)) {
                return;
            }
            Class type = pMap.getType(e.toString());
            String fieldName = (parent == null ? "" : parent.concat(".")).concat(e.toString());
            if (isRedisEntity(type)) {
                if (deepFetch) {
                    try {
                        RedisRepositoryImpl innerRepo = (RedisRepositoryImpl) factory.instance(type);
                        pMap.put(e.toString(), innerRepo.prepareInstanceAndFetchList(id, redissonBatch, pList,
                                fieldName, deepFetch));
                    } catch (InstantiationException | IllegalAccessException | RepositoryException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            } else {
                if ("id".equals(e)) {
                    redissonBatch.getMap(getStorageKey(), StringCodec.INSTANCE).getAsync(id);
                } else {
                    redissonBatch.getMap(getStorageKey(e.toString())).getAsync(id);
                }
                pList.add(fieldName);
            }
        });
    } catch (RuntimeException ex) {
        throw new InstantiationException();
    }
    return (T) pMap.getBean();
}