java.lang.Class.getProtectionDomain()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(330)

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

Class.getProtectionDomain介绍

[英]Returns null.
[中]返回null。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public Object run() {
    return source.getProtectionDomain();
  }
});

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

public ProtectionDomain run() {
   return ClassDefinitionUtils.class.getProtectionDomain();
  }
});

代码示例来源:origin: org.mockito/mockito-core

@Override
  public ClassLoadingStrategy<ClassLoader> resolveStrategy(Class<?> mockedType, ClassLoader classLoader, boolean localMock) {
    return ClassLoadingStrategy.Default.INJECTION.with(localMock ? mockedType.getProtectionDomain() : InjectionBase.class.getProtectionDomain());
  }
}

代码示例来源:origin: apache/incubator-dubbo

public static String getCodeBase(Class<?> cls) {
  if (cls == null) {
    return null;
  }
  ProtectionDomain domain = cls.getProtectionDomain();
  if (domain == null) {
    return null;
  }
  CodeSource source = domain.getCodeSource();
  if (source == null) {
    return null;
  }
  URL location = source.getLocation();
  if (location == null) {
    return null;
  }
  return location.getFile();
}

代码示例来源:origin: apache/incubator-dubbo

public static String getCodeBase(Class<?> cls) {
  if (cls == null) {
    return null;
  }
  ProtectionDomain domain = cls.getProtectionDomain();
  if (domain == null) {
    return null;
  }
  CodeSource source = domain.getCodeSource();
  if (source == null) {
    return null;
  }
  URL location = source.getLocation();
  if (location == null) {
    return null;
  }
  return location.getFile();
}

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

/**
 * Returns location of the class. If class is not in a jar, it's classpath
 * is returned; otherwise the jar location.
 */
public static String classLocation(Class clazz) {
  return clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
}

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

protected ProtectionDomain getDomain() {
  Class clazz;
  if (superClass != null && !superClass.getName().equals("java.lang.Object"))
    clazz = superClass;
  else if (interfaces != null && interfaces.length > 0)
    clazz = interfaces[0];
  else
    clazz = this.getClass();
  return clazz.getProtectionDomain();
}

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

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());

代码示例来源:origin: springside/springside4

/**
   * 获得参数clazz所在的Jar文件的绝对路径
   */
  public static String getJarPath(Class<?> clazz) {
    return clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
  }
}

代码示例来源:origin: Tencent/tinker

private static void setRunningLocation(CliMain m) {
  mRunningLocation = m.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  try {
    mRunningLocation = URLDecoder.decode(mRunningLocation, "utf-8");
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }
  if (mRunningLocation.endsWith(".jar")) {
    mRunningLocation = mRunningLocation.substring(0, mRunningLocation.lastIndexOf(File.separator) + 1);
  }
  File f = new File(mRunningLocation);
  mRunningLocation = f.getAbsolutePath();
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Get the code source file or class path of the Class passed in.
 *
 * @param clazz
 * @return Jar file name or class path.
 */
public static String getCodeSource(Class<?> clazz) {
  ProtectionDomain protectionDomain = clazz.getProtectionDomain();
  if (protectionDomain == null || protectionDomain.getCodeSource() == null) {
    return null;
  }
  CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
  URL location = codeSource.getLocation();
  if (location == null) {
    return null;
  }
  String path = codeSource.getLocation().toExternalForm();
  if (path.endsWith(".jar") && path.contains("/")) {
    return path.substring(path.lastIndexOf('/') + 1);
  }
  return path;
}

代码示例来源:origin: alibaba/Sentinel

static String getEagleEyeLocation() {
  try {
    URL resource = EagleEye.class.getProtectionDomain().getCodeSource().getLocation();
    if (resource != null) {
      return resource.toString();
    }
  } catch (Throwable t) {
    // ignore
  }
  return "unknown location";
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Get the code source file or class path of the Class passed in.
 *
 * @param clazz
 * @return Jar file name or class path.
 */
public static String getCodeSource(Class<?> clazz) {
  ProtectionDomain protectionDomain = clazz.getProtectionDomain();
  if (protectionDomain == null || protectionDomain.getCodeSource() == null) {
    return null;
  }
  CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
  URL location = codeSource.getLocation();
  if (location == null) {
    return null;
  }
  String path = codeSource.getLocation().toExternalForm();
  if (path.endsWith(".jar") && path.contains("/")) {
    return path.substring(path.lastIndexOf('/') + 1);
  }
  return path;
}

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

@Override
  public String toString() {
    final URL location = klass.getProtectionDomain().getCodeSource().getLocation();
    try {
      final String jar = new File(location.toURI()).getName();
      if (jar.endsWith(".jar")) {
        return jar;
      }
      return "project.jar";
    } catch (Exception ignored) {
      return "project.jar";
    }
  }
}

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

static File currentJarFile() {
  try {
    return new File(Boot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
  } catch (URISyntaxException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/incubator-druid

@SuppressWarnings("unchecked")
private Class<T> defineClass(String specializedClassName, byte[] specializedClassBytecode)
{
 return (Class<T>) UNSAFE.defineClass(
   specializedClassName,
   specializedClassBytecode,
   0,
   specializedClassBytecode.length,
   prototypeClass.getClassLoader(),
   prototypeClass.getProtectionDomain()
 );
}

代码示例来源:origin: medcl/elasticsearch-analysis-ik

public Path getConfigInPluginDir() {
  return PathUtils
      .get(new File(AnalysisIkPlugin.class.getProtectionDomain().getCodeSource().getLocation().getPath())
          .getParent(), "config")
      .toAbsolutePath();
}

代码示例来源:origin: apache/incubator-dubbo

public Class<?> toClass() {
  return toClass(ClassHelper.getClassLoader(ClassGenerator.class),
      getClass().getProtectionDomain());
}

代码示例来源:origin: apache/incubator-dubbo

public Class<?> toClass() {
  return toClass(ClassHelper.getClassLoader(ClassGenerator.class),
      getClass().getProtectionDomain());
}

代码示例来源:origin: apache/incubator-shardingsphere

@SneakyThrows
protected static Map<String, SQLCase> loadSQLCases(final String path) {
  File file = new File(SQLCasesLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath());
  return file.isFile() ? loadSQLCasesFromJar(path, file) : loadSQLCasesFromTargetDirectory(path);
}

相关文章

微信公众号

最新文章

更多

Class类方法