本文整理汇总了Java中java.lang.Class类的典型用法代码示例。如果您正苦于以下问题:Java Class类的具体用法?Java Class怎么用?Java Class使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Class类属于java.lang包,在下文中一共展示了Class类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instantiateXMLService
import java.lang.Class; //导入依赖的package包/类
public static Object instantiateXMLService(String serviceName) throws Exception {
ClassLoader backup = Thread.currentThread().getContextClassLoader();
try {
// set thread context class loader to module class loader
Thread.currentThread().setContextClassLoader(XMLFactoryHelper.class.getClassLoader());
if (serviceName.equals("org.xml.sax.XMLReader"))
return XMLReaderFactory.createXMLReader();
else if (serviceName.equals("javax.xml.validation.SchemaFactory"))
return Class.forName(serviceName).getMethod("newInstance", String.class)
.invoke(null, W3C_XML_SCHEMA_NS_URI);
else
return Class.forName(serviceName).getMethod("newInstance").invoke(null);
} finally {
Thread.currentThread().setContextClassLoader(backup);
}
}
示例2: toHashMap
import java.lang.Class; //导入依赖的package包/类
public static HashMap<Integer, String> toHashMap(Class _enumType) {
if (!_enumType.isEnum()) {
throw new IllegalArgumentException("Only support enum type!");
}
HashMap<Integer, String> dic = new HashMap<Integer, String>();
Field[] ps = _enumType.getFields();
for (Field p : ps) {
if (!p.getType().equals(_enumType)) {
continue;
}
Enum _enum = Enum.valueOf(_enumType, p.getName());
EnumDescriptionAttribute att = p.getAnnotation(EnumDescriptionAttribute.class);
if (att != null) {
dic.put(_enum.ordinal(), att.Text());
} else {
dic.put(_enum.ordinal(), p.getName());
}
}
return dic;
}
示例3: arePaddedPairwise
import java.lang.Class; //导入依赖的package包/类
public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
Field f1 = klass.getDeclaredField(field1);
Field f2 = klass.getDeclaredField(field2);
if (isStatic(f1) != isStatic(f2)) {
return true; // these guys are in naturally disjoint locations
}
int diff = offset(f1) - offset(f2);
if (diff < 0) {
// f1 is first
return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
} else {
// f2 is first
return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
}
}
示例4: transform
import java.lang.Class; //导入依赖的package包/类
/**
* @see GlideOptions#transform(Class<T>, Transformation<T>)
*/
@CheckResult
public <T> GlideRequest<TranscodeType> transform(@NonNull Class<T> arg0,
@NonNull Transformation<T> arg1) {
if (getMutableOptions() instanceof GlideOptions) {
this.requestOptions = ((GlideOptions) getMutableOptions()).transform(arg0, arg1);
} else {
this.requestOptions = new GlideOptions().apply(this.requestOptions).transform(arg0, arg1);
}
return this;
}
示例5: decode
import java.lang.Class; //导入依赖的package包/类
/**
* @see GlideOptions#decode(Class<?>)
*/
@CheckResult
public GlideRequest<TranscodeType> decode(@NonNull Class<?> arg0) {
if (getMutableOptions() instanceof GlideOptions) {
this.requestOptions = ((GlideOptions) getMutableOptions()).decode(arg0);
} else {
this.requestOptions = new GlideOptions().apply(this.requestOptions).decode(arg0);
}
return this;
}
示例6: getSize
import java.lang.Class; //导入依赖的package包/类
public static int getSize(Field field) {
Class type = field.getType();
if (type == byte.class) { return 1; }
if (type == boolean.class) { return 1; }
if (type == short.class) { return 2; }
if (type == char.class) { return 2; }
if (type == int.class) { return 4; }
if (type == float.class) { return 4; }
if (type == long.class) { return 8; }
if (type == double.class) { return 8; }
return ADDRESS_SIZE;
}
示例7: assertBadParameters
import java.lang.Class; //导入依赖的package包/类
public void assertBadParameters(Class<?> cls) throws NoSuchMethodException {
try {
System.err.println("Trying " + cls);
final Method method = cls.getMethod("m", int.class, int.class);
final Parameter[] params = method.getParameters();
System.err.println("Name " + params[0].getName());
System.err.println("Did not see expected exception");
errors++;
} catch(MalformedParametersException e) {
System.err.println("Expected exception seen");
}
}
示例8: optionalTransform
import java.lang.Class; //导入依赖的package包/类
/**
* @see GlideOptions#optionalTransform(Class<T>, Transformation<T>)
*/
@CheckResult
public <T> GlideRequest<TranscodeType> optionalTransform(@NonNull Class<T> arg0,
@NonNull Transformation<T> arg1) {
if (getMutableOptions() instanceof GlideOptions) {
this.requestOptions = ((GlideOptions) getMutableOptions()).optionalTransform(arg0, arg1);
} else {
this.requestOptions = new GlideOptions().apply(this.requestOptions).optionalTransform(arg0, arg1);
}
return this;
}
示例9: run
import java.lang.Class; //导入依赖的package包/类
public void run() throws NoSuchMethodException {
final Class<?> cls = noName;
System.err.println("Trying " + cls);
final Method method = cls.getMethod("m", int.class, int.class);
final Parameter[] params = method.getParameters();
System.err.println("Name " + params[0].getName());
System.err.println("Name " + params[1].getName());
}
示例10: arePaddedPairwise
import java.lang.Class; //导入依赖的package包/类
public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
Field f1 = klass.getField(field1);
Field f2 = klass.getField(field2);
int diff = offset(f1) - offset(f2);
if (diff < 0) {
// f1 is first
return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
} else {
// f2 is first
return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
}
}
示例11: makeAnonExtendsLocal
import java.lang.Class; //导入依赖的package包/类
public Tester makeAnonExtendsLocal(final String message) {
abstract class LocalTester extends Tester {
public LocalTester(final int localparam) {
super(localparam);
}
protected String[] names() {
return new String[] {
"this$1",
"localparam",
"val$message"
};
}
protected int[] modifiers() {
return new int[] {
Modifier.FINAL | MANDATED,
Modifier.FINAL,
Modifier.FINAL | SYNTHETIC
};
}
protected Class[] types() {
return new Class[] {
Encloser.class,
int.class,
String.class
};
}
}
return new LocalTester(2) {
public String message() {
return message;
}
};
}
示例12: run
import java.lang.Class; //导入依赖的package包/类
public void run() throws NoSuchMethodException {
for (Class<?> cls : classes)
assertBadParameters(cls);
if (errors != 0)
throw new RuntimeException(errors + " errors in test");
}
示例13: toServiceHashMap
import java.lang.Class; //导入依赖的package包/类
public static HashMap<Integer, AIService> toServiceHashMap(Class _enumType){
if (!_enumType.isEnum()) {
throw new IllegalArgumentException("Only support enum type!");
}
HashMap<Integer, AIService> dic = new HashMap<Integer, AIService>();
Field[] ps = _enumType.getFields();
for(Field p : ps){
if (!p.getType().equals(_enumType)) {
continue;
}
Enum _enum = Enum.valueOf(_enumType, p.getName());
ServiceDescriptionAttribute att = p.getAnnotation(ServiceDescriptionAttribute.class);
if (att != null) {
AIService newService = new AIService();
newService.setName(att.Name());
newService.setServiceType(att.ServiceType());
newService.setServiceId(att.ServiceId());
dic.put(_enum.ordinal(), newService);
}
else {
throw new IllegalArgumentException(String.format("%s has no ServiceDescriptionAttribute", p.getName()));
}
}
return dic;
}
示例14: BadClassFiles
import java.lang.Class; //导入依赖的package包/类
private BadClassFiles() throws ClassNotFoundException {
classes = new Class<?>[] {
loader.defineClass("EmptyName", EmptyName_bytes),
loader.defineClass("BadModifiers", BadModifiers_bytes),
loader.defineClass("BadNameIndex", BadNameIndex_bytes),
loader.defineClass("NameIndexOutOfBounds", NameIndexOutOfBounds_bytes),
loader.defineClass("ExtraParams", ExtraParams_bytes),
loader.defineClass("BadParams", BadParams_bytes),
// Name with .
loader.defineClass("BadName1", BadName1_bytes),
// Name with [
loader.defineClass("BadName2", BadName2_bytes),
// Name with ;
loader.defineClass("BadName3", BadName3_bytes),
// Name with /
loader.defineClass("BadName4", BadName4_bytes)
};
}
示例15: testCompareXmlAgainstConfigurationClass
import java.lang.Class; //导入依赖的package包/类
/**
* Compares the properties that are in the XML properties file, but not
* in the Configuration class.
*/
@Test
public void testCompareXmlAgainstConfigurationClass() {
// Error if subclass hasn't set class members
assertTrue(xmlFilename!=null);
assertTrue(configurationClasses!=null);
final int missingConfigSize = xmlFieldsMissingInConfiguration.size();
System.out.println("File " + xmlFilename + " (" + xmlKeyValueMap.size() + " properties)");
System.out.println();
StringBuffer configErrorMsg = new StringBuffer();
configErrorMsg.append(xmlFilename);
configErrorMsg.append(" has ");
configErrorMsg.append(missingConfigSize);
configErrorMsg.append(" properties missing in");
for (Class c : configurationClasses) {
configErrorMsg.append(" " + c);
}
System.out.println(configErrorMsg.toString());
System.out.println();
if (missingConfigSize==0) {
System.out.println(" (None)");
} else {
for (String missingField : xmlFieldsMissingInConfiguration) {
System.out.println(" " + missingField);
}
}
System.out.println();
System.out.println("=====");
System.out.println();
if ( errorIfMissingConfigProps ) {
assertTrue(configErrorMsg.toString(), missingConfigSize==0);
}
}