use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.
the class Class method getEnclosingConstructor.
/**
* If this {@code Class} object represents a local or anonymous
* class within a constructor, returns a {@link
* java.lang.reflect.Constructor Constructor} object representing
* the immediately enclosing constructor of the underlying
* class. Returns {@code null} otherwise. In particular, this
* method returns {@code null} if the underlying class is a local
* or anonymous class immediately enclosed by a type declaration,
* instance initializer or static initializer.
*
* @return the immediately enclosing constructor of the underlying class, if
* that class is a local or anonymous class; otherwise {@code null}.
* @throws SecurityException
* If a security manager, <i>s</i>, is present and any of the
* following conditions is met:
*
* <ul>
*
* <li> the caller's class loader is not the same as the
* class loader of the enclosing class and invocation of
* {@link SecurityManager#checkPermission
* s.checkPermission} method with
* {@code RuntimePermission("accessDeclaredMembers")}
* denies access to the constructors within the enclosing class
*
* <li> the caller's class loader is not the same as or an
* ancestor of the class loader for the enclosing class and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package
* of the enclosing class
*
* </ul>
* @since 1.5
*/
@CallerSensitive
public Constructor<?> getEnclosingConstructor() throws SecurityException {
EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
if (enclosingInfo == null)
return null;
else {
if (!enclosingInfo.isConstructor())
return null;
ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), getFactory());
Type[] parameterTypes = typeInfo.getParameterTypes();
Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
// don't have generics information
for (int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]);
// Perform access check
final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
enclosingCandidate.checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
}
Constructor<?>[] candidates = enclosingCandidate.privateGetDeclaredConstructors(false);
/*
* Loop over all declared constructors; match number
* of and type of parameters.
*/
ReflectionFactory fact = getReflectionFactory();
for (Constructor<?> c : candidates) {
if (arrayContentsEq(parameterClasses, fact.getExecutableSharedParameterTypes(c))) {
return fact.copyConstructor(c);
}
}
throw new InternalError("Enclosing constructor not found");
}
}
use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.
the class Class method getDeclaredMethod.
/**
* Returns a {@code Method} object that reflects the specified
* declared method of the class or interface represented by this
* {@code Class} object. The {@code name} parameter is a
* {@code String} that specifies the simple name of the desired
* method, and the {@code parameterTypes} parameter is an array of
* {@code Class} objects that identify the method's formal parameter
* types, in declared order. If more than one method with the same
* parameter types is declared in a class, and one of these methods has a
* return type that is more specific than any of the others, that method is
* returned; otherwise one of the methods is chosen arbitrarily. If the
* name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
* is raised.
*
* <p> If this {@code Class} object represents an array type, then this
* method does not find the {@code clone()} method.
*
* @param name the name of the method
* @param parameterTypes the parameter array
* @return the {@code Method} object for the method of this class
* matching the specified name and parameters
* @throws NoSuchMethodException if a matching method is not found.
* @throws NullPointerException if {@code name} is {@code null}
* @throws SecurityException
* If a security manager, <i>s</i>, is present and any of the
* following conditions is met:
*
* <ul>
*
* <li> the caller's class loader is not the same as the
* class loader of this class and invocation of
* {@link SecurityManager#checkPermission
* s.checkPermission} method with
* {@code RuntimePermission("accessDeclaredMembers")}
* denies access to the declared method
*
* <li> the caller's class loader is not the same as or an
* ancestor of the class loader for the current class and
* invocation of {@link SecurityManager#checkPackageAccess
* s.checkPackageAccess()} denies access to the package
* of this class
*
* </ul>
*
* @jls 8.2 Class Members
* @jls 8.4 Method Declarations
* @since 1.1
*/
@CallerSensitive
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
Objects.requireNonNull(name);
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true);
}
Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(methodToString(name, parameterTypes));
}
return getReflectionFactory().copyMethod(method);
}
use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.
the class Method method invoke.
/**
* Invokes the underlying method represented by this {@code Method}
* object, on the specified object with the specified parameters.
* Individual parameters are automatically unwrapped to match
* primitive formal parameters, and both primitive and reference
* parameters are subject to method invocation conversions as
* necessary.
*
* <p>If the underlying method is static, then the specified {@code obj}
* argument is ignored. It may be null.
*
* <p>If the number of formal parameters required by the underlying method is
* 0, the supplied {@code args} array may be of length 0 or null.
*
* <p>If the underlying method is an instance method, it is invoked
* using dynamic method lookup as documented in The Java Language
* Specification, Second Edition, section 15.12.4.4; in particular,
* overriding based on the runtime type of the target object will occur.
*
* <p>If the underlying method is static, the class that declared
* the method is initialized if it has not already been initialized.
*
* <p>If the method completes normally, the value it returns is
* returned to the caller of invoke; if the value has a primitive
* type, it is first appropriately wrapped in an object. However,
* if the value has the type of an array of a primitive type, the
* elements of the array are <i>not</i> wrapped in objects; in
* other words, an array of primitive type is returned. If the
* underlying method return type is void, the invocation returns
* null.
*
* @param obj the object the underlying method is invoked from
* @param args the arguments used for the method call
* @return the result of dispatching the method represented by
* this object on {@code obj} with parameters
* {@code args}
*
* @exception IllegalAccessException if this {@code Method} object
* is enforcing Java language access control and the underlying
* method is inaccessible.
* @exception IllegalArgumentException if the method is an
* instance method and the specified object argument
* is not an instance of the class or interface
* declaring the underlying method (or of a subclass
* or implementor thereof); if the number of actual
* and formal parameters differ; if an unwrapping
* conversion for primitive arguments fails; or if,
* after possible unwrapping, a parameter value
* cannot be converted to the corresponding formal
* parameter type by a method invocation conversion.
* @exception InvocationTargetException if the underlying method
* throws an exception.
* @exception NullPointerException if the specified object is null
* and the method is an instance method.
* @exception ExceptionInInitializerError if the initialization
* provoked by this method fails.
*/
@CallerSensitive
// to ensure Reflection.getCallerClass optimization
@ForceInline
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!override) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, Modifier.isStatic(modifiers) ? null : obj.getClass(), modifiers);
}
// read volatile
MethodAccessor ma = methodAccessor;
if (ma == null) {
ma = acquireMethodAccessor();
}
return ma.invoke(obj, args);
}
use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.
the class System method getLogger.
/**
* Returns a localizable instance of {@link Logger
* Logger} for the caller's use.
* The returned logger will use the provided resource bundle for message
* localization.
*
* @implSpec
* The returned logger will perform message localization as specified
* by {@link LoggerFinder#getLocalizedLogger(java.lang.String,
* java.util.ResourceBundle, java.lang.Module)
* LoggerFinder.getLocalizedLogger(name, bundle, module)}, where
* {@code module} is the caller's module.
* In cases where {@code System.getLogger} is called from a context where
* there is no caller frame on the stack (e.g when called directly
* from a JNI attached thread), {@code IllegalCallerException} is thrown.
* To obtain a logger in such a context, use an auxiliary class that
* will implicitly be identified as the caller, or use the system {@link
* LoggerFinder#getLoggerFinder() LoggerFinder} to obtain a logger instead.
* Note that doing the latter may eagerly initialize the underlying
* logging system.
*
* @apiNote
* This method is intended to be used after the system is fully initialized.
* This method may trigger the immediate loading and initialization
* of the {@link LoggerFinder} service, which may cause issues if the
* Java Runtime is not ready to initialize the concrete service
* implementation yet.
* System classes which may be loaded early in the boot sequence and
* need to log localized messages should create a logger using
* {@link #getLogger(java.lang.String)} and then use the log methods that
* take a resource bundle as parameter.
*
* @param name the name of the logger.
* @param bundle a resource bundle.
* @return an instance of {@link Logger} which will use the provided
* resource bundle for message localization.
* @throws NullPointerException if {@code name} is {@code null} or
* {@code bundle} is {@code null}.
* @throws IllegalCallerException if there is no Java caller frame on the
* stack.
*
* @since 9
*/
@CallerSensitive
public static Logger getLogger(String name, ResourceBundle bundle) {
final ResourceBundle rb = Objects.requireNonNull(bundle);
Objects.requireNonNull(name);
final Class<?> caller = Reflection.getCallerClass();
if (caller == null) {
throw new IllegalCallerException("no caller frame");
}
final SecurityManager sm = System.getSecurityManager();
// when logging. This could be revisited later, if it needs to.
if (sm != null) {
final PrivilegedAction<Logger> pa = () -> LoggerFinder.accessProvider().getLocalizedLogger(name, rb, caller.getModule());
return AccessController.doPrivileged(pa, null, LoggerFinder.LOGGERFINDER_PERMISSION);
}
return LoggerFinder.accessProvider().getLocalizedLogger(name, rb, caller.getModule());
}
use of jdk.internal.reflect.CallerSensitive in project Bytecoder by mirkosertic.
the class ResourceBundle method getBundle.
/**
* Returns a resource bundle using the specified base name, the
* default locale and the specified control. Calling this method
* is equivalent to calling
* <pre>
* getBundle(baseName, Locale.getDefault(),
* this.getClass().getClassLoader(), control),
* </pre>
* except that <code>getClassLoader()</code> is run with the security
* privileges of <code>ResourceBundle</code>. See {@link
* #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
* complete description of the resource bundle loading process with a
* <code>ResourceBundle.Control</code>.
*
* @param baseName
* the base name of the resource bundle, a fully qualified class
* name
* @param control
* the control which gives information for the resource bundle
* loading process
* @return a resource bundle for the given base name and the default locale
* @throws NullPointerException
* if <code>baseName</code> or <code>control</code> is
* <code>null</code>
* @throws MissingResourceException
* if no resource bundle for the specified base name can be found
* @throws IllegalArgumentException
* if the given <code>control</code> doesn't perform properly
* (e.g., <code>control.getCandidateLocales</code> returns null.)
* Note that validation of <code>control</code> is performed as
* needed.
* @throws UnsupportedOperationException
* if this method is called in a named module
* @since 1.6
* @revised 9
* @spec JPMS
*/
@CallerSensitive
public static final ResourceBundle getBundle(String baseName, Control control) {
Class<?> caller = Reflection.getCallerClass();
Locale targetLocale = Locale.getDefault();
checkNamedModule(caller);
return getBundleImpl(baseName, targetLocale, caller, control);
}
Aggregations