sun.reflect.Reflection.getCallerClass()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(695)

本文整理了Java中sun.reflect.Reflection.getCallerClass方法的一些代码示例,展示了Reflection.getCallerClass的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reflection.getCallerClass方法的具体详情如下:
包路径:sun.reflect.Reflection
类名称:Reflection
方法名:getCallerClass

Reflection.getCallerClass介绍

[英]Returns the class of the caller of the method calling this method, ignoring frames associated with java.lang.reflect.Method.invoke() and its implementation.
[中]返回调用此方法的方法的调用方的类,忽略与java关联的帧。朗,反思一下。方法invoke()及其实现。

代码示例

代码示例来源:origin: wildfly/wildfly

public static Class<?> getCallerClass(int n){
  return Reflection.getCallerClass(n);
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns the Class object of the currently
 * probed (or traced) class.
 * @deprecated Since 1.1. Use {@linkplain ProbeClassName} and {@linkplain Self} annotations instead
 */
@Deprecated
public static Class probeClass() {
  return Reflection.getCallerClass(STACK_DEC);
}

代码示例来源:origin: btraceio/btrace

@CallerSensitive
public Class defineClass(byte[] code) {
  Class caller = isNewerThan8 ? Reflection.getCallerClass() : Reflection.getCallerClass(2);
  if (! caller.getName().startsWith("com.sun.btrace.")) {
    throw new SecurityException("unsafe defineClass");
  }
  return defineClassImpl(code, true);
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns Class object for given class name.
 */
public static Class classForName(String name) {
  ClassLoader callerLoader = Reflection.getCallerClass(STACK_DEC).getClassLoader();
  return classForName(name, callerLoader);
}

代码示例来源:origin: btraceio/btrace

@CallerSensitive
public Class defineClass(byte[] code, boolean mustBeBootstrap) {
  Class caller = isNewerThan8 ? Reflection.getCallerClass() : Reflection.getCallerClass(2);
  if (! caller.getName().startsWith("com.sun.btrace.")) {
    throw new SecurityException("unsafe defineClass");
  }
  return defineClassImpl(code, mustBeBootstrap);
}

代码示例来源:origin: btraceio/btrace

@CallerSensitive
public static void init(PerfReader perfRead) {
  initUnsafe();
  Class caller = isNewerThan8 ? Reflection.getCallerClass() : Reflection.getCallerClass(2);
  if (! caller.getName().equals("com.sun.btrace.agent.Client")) {
    throw new SecurityException("unsafe init");
  }
  perfReader = perfRead;
  loadLibrary(perfRead.getClass().getClassLoader());
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns a <code>Field</code> object that reflects the specified declared
 * field of the class or interface represented by the given <code>Class</code>
 * object. The <code>name</code> parameter is a <code>String</code> that
 * specifies the simple name of the desired field. Throws a <code>RuntimeException</code>
 * when field is not found.
 *
 * @param clazz Class whose field is returned
 * @param name the name of the field
 * @return the <code>Field</code> object for the specified field in this
 * class
 */
public static Field field(String clazz, String name) {
  ClassLoader callerLoader = Reflection.getCallerClass(STACK_DEC).getClassLoader();
  return field(classForName(clazz, callerLoader), name);
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns a <code>Field</code> object that reflects the specified declared
 * field of the class or interface represented by the given <code>Class</code>
 * object. The <code>name</code> parameter is a <code>String</code> that
 * specifies the simple name of the desired field. Throws a <code>RuntimeException</code>
 * when field is not found.
 *
 * @param clazz Class whose field is returned
 * @param name the name of the field
 * @return the <code>Field</code> object for the specified field in this
 * class
 */
public static Field field(String clazz, String name) {
  ClassLoader callerLoader = Reflection.getCallerClass(STACK_DEC).getClassLoader();
  return Reflective.field(classForName(clazz, callerLoader), name);
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns a <code>Field</code> object that reflects the specified declared
 * field of the class or interface represented by the given <code>Class</code>
 * object. The <code>name</code> parameter is a <code>String</code> that
 * specifies the simple name of the desired field. Returns <code>null</code> on not finding
 * field if throwException parameter is <code>false</code>. Else throws a <code>RuntimeException</code>
 * when field is not found.
 *
 * @param clazz Class whose field is returned
 * @param name the name of the field
 * @param throwException whether to throw exception on failing to find field or not
 * @return the <code>Field</code> object for the specified field in this
 * class
 */
public static Field field(String clazz, String name, boolean throwException) {
  ClassLoader callerLoader = Reflection.getCallerClass(STACK_DEC).getClassLoader();
  return Reflective.field(classForName(clazz, callerLoader), name, throwException);
}

代码示例来源:origin: btraceio/btrace

/**
 * Returns a <code>Field</code> object that reflects the specified declared
 * field of the class or interface represented by the given <code>Class</code>
 * object. The <code>name</code> parameter is a <code>String</code> that
 * specifies the simple name of the desired field. Returns <code>null</code> on not finding
 * field if throwException parameter is <code>false</code>. Else throws a <code>RuntimeException</code>
 * when field is not found.
 *
 * @param clazz Class whose field is returned
 * @param name the name of the field
 * @param throwException whether to throw exception on failing to find field or not
 * @return the <code>Field</code> object for the specified field in this
 * class
 */
public static Field field(String clazz, String name, boolean throwException) {
  ClassLoader callerLoader = Reflection.getCallerClass(STACK_DEC).getClassLoader();
  return field(classForName(clazz, callerLoader), name, throwException);
}

代码示例来源:origin: ch.qos.logback/logback-classic

Class callerClass = null;
if (GET_CALLER_CLASS_METHOD_AVAILABLE) {
  callerClass = Reflection.getCallerClass(localFirstCommon + i - missfireCount + 1);

代码示例来源:origin: stackoverflow.com

return sun.reflect.Reflection.getCallerClass(callStackDepth).getName();

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Test
public void testStackTraceEquivalence() throws Exception {
  for (int i = 1; i < 15; i++) {
    final Class<?> expected = Reflection.getCallerClass(i + StackLocator.JDK_7u25_OFFSET);
    final Class<?> actual = StackLocatorUtil.getCallerClass(i);
    final Class<?> fallbackActual = Class.forName(
      StackLocatorUtil.getStackTraceElement(i).getClassName());
    assertSame(expected, actual);
    assertSame(expected, fallbackActual);
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

Class callerClass = null;
if (GET_CALLER_CLASS_METHOD_AVAILABLE) {
 callerClass = Reflection.getCallerClass(localFirstCommon + i
     - missfireCount + 1);

代码示例来源:origin: com.ailyr/lyr-tool-core

@Override
  public Class<?> getCaller(int depth) {
    return sun.reflect.Reflection.getCallerClass(OFFSET + depth);
  }
}

代码示例来源:origin: com.xiaoleilu/hutool-core

@Override
  public Class<?> getCaller(int depth) {
    return sun.reflect.Reflection.getCallerClass(OFFSET + depth);
  }
}

代码示例来源:origin: com.ailyr/lyr-tool-core

@Override
public Class<?> getCallerCaller() {
  return sun.reflect.Reflection.getCallerClass(OFFSET + 2);
}

代码示例来源:origin: stackoverflow.com

public class PrivateConstructorClass {

private PrivateConstructorClass() {
  checkPerMission();
     //you own code go below
}

void checkPerMission() {
  Class self = sun.reflect.Reflection.getCallerClass(1);
  Class caller = sun.reflect.Reflection.getCallerClass(3);
  if (self != caller) {
    throw new java.lang.IllegalAccessError();
  }
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

@SuppressWarnings("deprecation")
static Class<?> getCallerClass(int n) {
  if (hasGetCallerClass) {
    return Reflection.getCallerClass(n + callerOffset);
  } else {
    return getCallStack()[n + callerOffset];
  }
}

代码示例来源:origin: org.jboss.modules/jboss-modules

static Class<?> getCallingClass() {
  // 0 == this class
  // 1 == immediate caller in jboss-modules
  // 2 == user caller
  if (hasGetCallerClass) {
    return Reflection.getCallerClass(2 + callerOffset);
  } else {
    return hack.getClassContext()[2 + callerOffset];
  }
}

相关文章