Search in sources :

Example 1 with PrivilegedGetConstructorFor

use of org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor in project eclipselink by eclipse-ee4j.

the class ConstructorReportItem method initialize.

/**
 * INTERNAL:
 * Looks up mapping for attribute during preExecute of ReportQuery
 */
@Override
public void initialize(ReportQuery query) throws QueryException {
    int size = getReportItems().size();
    List<DatabaseMapping> mappings = new ArrayList<>();
    for (int index = 0; index < size; index++) {
        ReportItem item = reportItems.get(index);
        item.initialize(query);
        mappings.add(item.getMapping());
    }
    setConstructorMappings(mappings);
    int numberOfItems = getReportItems().size();
    // Arguments may be initialized depending on how the query was constructed, so types may be undefined though.
    if (getConstructorArgTypes() == null) {
        setConstructorArgTypes(new Class<?>[numberOfItems]);
    }
    Class<?>[] constructorArgTypes = getConstructorArgTypes();
    for (int index = 0; index < numberOfItems; index++) {
        if (constructorArgTypes[index] == null) {
            ReportItem argumentItem = getReportItems().get(index);
            if (mappings.get(index) != null) {
                DatabaseMapping mapping = constructorMappings.get(index);
                if (argumentItem.getAttributeExpression() != null && argumentItem.getAttributeExpression().isMapEntryExpression()) {
                    if (((MapEntryExpression) argumentItem.getAttributeExpression()).shouldReturnMapEntry()) {
                        constructorArgTypes[index] = Map.Entry.class;
                    } else {
                        constructorArgTypes[index] = (Class) mapping.getContainerPolicy().getKeyType();
                    }
                } else {
                    constructorArgTypes[index] = mapping.getAttributeClassification();
                }
            } else if (argumentItem.getResultType() != null) {
                constructorArgTypes[index] = argumentItem.getResultType();
            } else if (argumentItem.getDescriptor() != null) {
                constructorArgTypes[index] = argumentItem.getDescriptor().getJavaClass();
            } else if (argumentItem.getAttributeExpression() != null && argumentItem.getAttributeExpression().isConstantExpression()) {
                constructorArgTypes[index] = ((ConstantExpression) argumentItem.getAttributeExpression()).getValue().getClass();
            } else {
                // Use Object.class by default.
                constructorArgTypes[index] = ClassConstants.OBJECT;
            }
        }
    }
    if (getConstructor() == null) {
        try {
            Constructor<?> constructor = null;
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                try {
                    constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(getResultType(), constructorArgTypes, true));
                } catch (PrivilegedActionException exception) {
                    throw QueryException.exceptionWhileUsingConstructorExpression(exception.getException(), query);
                }
            } else {
                constructor = PrivilegedAccessHelper.getConstructorFor(getResultType(), constructorArgTypes, true);
            }
            setConstructor(constructor);
        } catch (NoSuchMethodException exception) {
            throw QueryException.exceptionWhileUsingConstructorExpression(exception, query);
        }
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ReportItem(org.eclipse.persistence.internal.queries.ReportItem) MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) PrivilegedGetConstructorFor(org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor) Map(java.util.Map)

Example 2 with PrivilegedGetConstructorFor

use of org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor in project eclipselink by eclipse-ee4j.

the class ConstructorResult method initialize.

/**
 * INTERNAL:
 */
protected void initialize(DatabaseRecord record, ResultSetMappingQuery query) {
    int columnResultsSize = getColumnResults().size();
    constructorArgTypes = new Class<?>[columnResultsSize];
    for (int i = 0; i < columnResultsSize; i++) {
        ColumnResult result = getColumnResults().get(i);
        DatabaseField resultField = result.getColumn();
        if (resultField.getType() == null) {
            Object recordResultField = record.get(resultField);
            if (recordResultField == null) {
                throw QueryException.columnResultNotFound(resultField);
            } else {
                constructorArgTypes[i] = recordResultField.getClass();
            }
        } else {
            constructorArgTypes[i] = resultField.getType();
        }
    }
    try {
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            try {
                constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(targetClass, constructorArgTypes, true));
            } catch (PrivilegedActionException exception) {
                throw QueryException.exceptionWhileInitializingConstructor(exception.getException(), query, targetClass);
            }
        } else {
            constructor = PrivilegedAccessHelper.getConstructorFor(targetClass, constructorArgTypes, true);
        }
    } catch (NoSuchMethodException exception) {
        throw QueryException.exceptionWhileInitializingConstructor(exception, query, targetClass);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) PrivilegedGetConstructorFor(org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor)

Example 3 with PrivilegedGetConstructorFor

use of org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor in project eclipselink by eclipse-ee4j.

the class CriteriaQueryImpl method populateAndSetConstructorSelection.

/**
 * This method will set this queryImpl's selection to a ConstructorSelectionImpl, creating a new
 * instance or populating the one passed in as necessary.
 * Throws IllegalArgumentException if a constructor taking arguments represented
 * by the selections array doesn't exist for the given class.
 *
 * Also sets the query result to ResultType.CONSTRUCTOR
 */
public void populateAndSetConstructorSelection(ConstructorSelectionImpl constructorSelection, Class<?> class1, Selection<?>... selections) throws IllegalArgumentException {
    Class<?>[] constructorArgs = new Class<?>[selections.length];
    int count = 0;
    for (Selection select : selections) {
        if (select instanceof ConstructorSelectionImpl) {
            ConstructorSelectionImpl constructorSelect = (ConstructorSelectionImpl) select;
            Selection[] selectArray = constructorSelect.getCompoundSelectionItems().toArray(new Selection[constructorSelect.getCompoundSelectionItems().size()]);
            populateAndSetConstructorSelection(constructorSelect, constructorSelect.getJavaType(), selectArray);
        }
        constructorArgs[count++] = select.getJavaType();
    }
    Constructor constructor = null;
    try {
        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
            constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(class1, constructorArgs, false));
        } else {
            constructor = PrivilegedAccessHelper.getConstructorFor(class1, constructorArgs, false);
        }
        if (constructorSelection == null) {
            constructorSelection = new ConstructorSelectionImpl(class1, selections);
        }
        this.queryResult = ResultType.CONSTRUCTOR;
        constructorSelection.setConstructor(constructor);
        constructorSelection.setConstructorArgTypes(constructorArgs);
        this.selection = constructorSelection;
    } catch (Exception e) {
        // PrivilegedActionException and NoSuchMethodException are possible
        Object[] params = new Object[1];
        params[0] = this.queryType;
        throw new IllegalArgumentException(ExceptionLocalization.buildMessage("criteria_no_constructor_found", params), e);
    }
}
Also used : Selection(jakarta.persistence.criteria.Selection) Constructor(java.lang.reflect.Constructor) PrivilegedGetConstructorFor(org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor)

Aggregations

PrivilegedGetConstructorFor (org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor)3 PrivilegedActionException (java.security.PrivilegedActionException)2 Selection (jakarta.persistence.criteria.Selection)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1