当前位置: 首页>>代码示例>>Java>>正文


Java Constructor.setAccessible方法代码示例

本文整理汇总了Java中java.lang.reflect.Constructor.setAccessible方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.setAccessible方法的具体用法?Java Constructor.setAccessible怎么用?Java Constructor.setAccessible使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.reflect.Constructor的用法示例。


在下文中一共展示了Constructor.setAccessible方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateConstructor

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private final Constructor<?> generateConstructor(Class<?> cl,
                                                 Constructor<?> constructorToCall) {


    ConstructorAccessor acc = new MethodAccessorGenerator().
        generateSerializationConstructor(cl,
                                         constructorToCall.getParameterTypes(),
                                         constructorToCall.getExceptionTypes(),
                                         constructorToCall.getModifiers(),
                                         constructorToCall.getDeclaringClass());
    Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
                                      constructorToCall.getParameterTypes(),
                                      constructorToCall.getExceptionTypes(),
                                      constructorToCall.getModifiers(),
                                      langReflectAccess().
                                      getConstructorSlot(constructorToCall),
                                      langReflectAccess().
                                      getConstructorSignature(constructorToCall),
                                      langReflectAccess().
                                      getConstructorAnnotations(constructorToCall),
                                      langReflectAccess().
                                      getConstructorParameterAnnotations(constructorToCall));
    setConstructorAccessor(c, acc);
    c.setAccessible(true);
    return c;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:ReflectionFactory.java

示例2: newInstance

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static Object newInstance(Class clazz, Class[] constructors_class, Object[] constructors_args) {
    Object instance = null;
    try {
        Constructor constructor = clazz.getDeclaredConstructor(constructors_class);
        constructor.setAccessible(true);
        instance = constructor.newInstance(constructors_args);
    } catch (NoSuchMethodException e) {
        JLog.i("clf", "NoSuchMethodException..e=" + e.getMessage());
    } catch (IllegalArgumentException e2) {
        JLog.i("clf", "IllegalArgumentException..e=" + e2.getMessage());
    } catch (InstantiationException e3) {
        JLog.i("clf", "InstantiationException..e=" + e3.getMessage());
    } catch (IllegalAccessException e4) {
        JLog.i("clf", "IllegalAccessException..e=" + e4.getMessage());
    } catch (InvocationTargetException e5) {
        JLog.i("clf", "InvocationTargetException..e=" + e5.getMessage());
    }
    return instance;
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:20,代码来源:JarLoader.java

示例3: createView

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private View createView(Context context, String name, String prefix)
        throws ClassNotFoundException, InflateException {
    Constructor<? extends View> constructor = sConstructorMap.get(name);

    try {
        if (constructor == null) {
            // Class not found in the cache, see if it's real, and try to add it
            Class<? extends View> clazz = context.getClassLoader().loadClass(
                    prefix != null ? (prefix + name) : name).asSubclass(View.class);

            constructor = clazz.getConstructor(sConstructorSignature);
            sConstructorMap.put(name, constructor);
        }
        constructor.setAccessible(true);
        return constructor.newInstance(mConstructorArgs);
    } catch (Exception e) {
        // We do not want to catch these, lets return null and let the actual LayoutInflater
        // try
        return null;
    }
}
 
开发者ID:wutongke,项目名称:AndroidSkinAnimator,代码行数:22,代码来源:SkinCompatViewInflater.java

示例4: testExecutingConstructor

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void testExecutingConstructor() throws Exception {
    ClassLoader l = new L();
    Class<?> c = l.loadClass(CAPI.class.getName());        
    Member m = c.getDeclaredConstructor(int.class);
    Constructor ctor = (Constructor)m;
    ctor.setAccessible(true);
    
    Object o = ctor.newInstance(5);
    assertSame(c, o.getClass());
    assertTrue("Invalid API superclass", Superclazz.class.isInstance(o));
    
    assertEquals("@ConstructorDelegate method did not execute", 5, ((Superclazz)o).val);
    
    Field f = o.getClass().getField("otherVal");
    Object v = f.get(o);
    assertEquals("Patched API constructor did not execute", v, 1);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:AsmTest.java

示例5: addMethodProxy

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private void addMethodProxy(Class<?> hookType) {
    try {
        Constructor<?> constructor = hookType.getDeclaredConstructors()[0];
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        MethodProxy methodProxy;
        if (constructor.getParameterTypes().length == 0) {
            methodProxy = (MethodProxy) constructor.newInstance();
        } else {
            methodProxy = (MethodProxy) constructor.newInstance(this);
        }
        mInvocationStub.addMethodProxy(methodProxy);
    } catch (Throwable e) {
        throw new RuntimeException("Unable to instance Hook : " + hookType + " : " + e.getMessage());
    }
}
 
开发者ID:coding-dream,项目名称:TPlayer,代码行数:18,代码来源:MethodInvocationProxy.java

示例6: get

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Block get(int id, Integer meta, Position pos) {
    Block block;
    try {
        Class<? extends Block> c = list[id];
        if (c != null) {
            Constructor<? extends Block> constructor = c.getDeclaredConstructor(int.class);
            constructor.setAccessible(true);
            block = (Block) constructor.newInstance(meta);
        } else {
            block = new BlockUnknown(id, meta);
        }
    } catch (Exception e) {
        block = new BlockUnknown(id, meta);
    }

    if (pos != null) {
        block.x = pos.x;
        block.y = pos.y;
        block.z = pos.z;
        block.level = pos.level;
    }
    return block;
}
 
开发者ID:JupiterDevelopmentTeam,项目名称:Jupiter,代码行数:25,代码来源:Block.java

示例7:

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
 * isChangedメソッドでMemcachedからキャッシュを取得できた場合falseを返すこと.
 * @throws Exception テスト中の例外
 */
@SuppressWarnings("unchecked")
@Test
public void isChangedメソッドでMemcachedからキャッシュを取得できた場合falseを返すこと() throws Exception {
    String nodeId = "nodeId";
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("testKey", "testValue");

    // MemcachedClientのモックを作成(getしたときにMemchachedClientExceptionをthrowするモック)
    Constructor<MemcachedClient> c = MemcachedClient.class.getDeclaredConstructor();
    c.setAccessible(true);
    MemcachedClient mockMemcachedClient = Mockito.spy(c.newInstance());
    Mockito.doReturn(data).when(mockMemcachedClient).get(Mockito.anyString(), Mockito.any(Class.class));

    // UserDataSchemaCacheのモックを作成(MemcachedClientのモックを使用するモック)
    PowerMockito.spy(UserDataSchemaCache.class);
    PowerMockito.when(UserDataSchemaCache.class, "getMcdClient").thenReturn(mockMemcachedClient);

    // キャッシュの設定を有効にする
    PowerMockito.spy(PersoniumUnitConfig.class);
    PowerMockito.when(PersoniumUnitConfig.class, "isSchemaCacheEnabled").thenReturn(true);

    // isChangedメソッドのテスト
    boolean isChanged = UserDataSchemaCache.isChanged(nodeId, data);
    assertFalse(isChanged);
}
 
开发者ID:personium,项目名称:personium-core,代码行数:30,代码来源:UserDataSchemaCacheTest.java

示例8: jskLogTest

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Test(expected = JSKException.class)
  public void jskLogTest() throws Exception {

Constructor<JSKLog> constructor = JSKLog.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));

try {
	constructor.setAccessible(true);
	constructor.newInstance();
}
catch(Exception exception) {
	throw new JSKException(ERROR_UTILITY_CLASS);
}
  }
 
开发者ID:Varoso,项目名称:JSK,代码行数:15,代码来源:JSKLogTest.java

示例9: createPipeStream

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private Pair<InputStream, OutputStream> createPipeStream() throws Exception {
    Class<?> pipeStreamClass = Class.forName("jdk.jshell.execution.PipeInputStream");
    Constructor<?> c = pipeStreamClass.getDeclaredConstructor();
    c.setAccessible(true);
    Object pipeStream = c.newInstance();
    Method createOutputStream = pipeStreamClass.getDeclaredMethod("createOutput");
    createOutputStream.setAccessible(true);
    return Pair.of((InputStream) pipeStream, (OutputStream) createOutputStream.invoke(pipeStream));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:PipeInputStreamTest.java

示例10: getConstructor

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private Constructor<T> getConstructor(Class[] classes) throws NoSuchMethodException {
    Constructor<T> constructor = type.getDeclaredConstructor(classes);
    if (!Modifier.isPublic(constructor.getModifiers())) {
        constructor.setAccessible(true);
    }
    return constructor;
}
 
开发者ID:kartashian,项目名称:proteus4j,代码行数:8,代码来源:SimpleChild.java

示例11: allocateDirectBuffer

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
 * Uses internal JDK APIs to allocate a DirectByteBuffer while ignoring the JVM's
 * MaxDirectMemorySize limit (the default limit is too low and we do not want to require users
 * to increase it).
 */
@SuppressWarnings("unchecked")
public static ByteBuffer allocateDirectBuffer(int size) {
    try {
        Class cls = Class.forName("java.nio.DirectByteBuffer");
        Constructor constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE);
        constructor.setAccessible(true);
        Field cleanerField = cls.getDeclaredField("cleaner");
        cleanerField.setAccessible(true);
        final long memory = allocateMemory(size);
        ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size);
        Cleaner cleaner = Cleaner.create(buffer, new Runnable() {
            @Override
            public void run() {
                freeMemory(memory);
            }
        });
        cleanerField.set(buffer, cleaner);
        return buffer;
    } catch (Exception e) {
        throwException(e);
    }
    throw new IllegalStateException("unreachable");
}
 
开发者ID:huang-up,项目名称:mycat-src-1.6.1-RELEASE,代码行数:29,代码来源:Platform.java

示例12: newInstance

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
static PreferenceManager newInstance(Activity activity, int firstRequestCode) {
	try {
		Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
		c.setAccessible(true);
		return c.newInstance(activity, firstRequestCode);
	} catch (Exception e) {
		Log.w(TAG, "Couldn't call constructor PreferenceManager by reflection", e);
	}
	return null;
}
 
开发者ID:uhuru-mobile,项目名称:mobile-store,代码行数:11,代码来源:PreferenceManagerCompat.java

示例13: instantiateException

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private IOException instantiateException(Class<? extends IOException> cls)
    throws Exception {
  Constructor<? extends IOException> cn = cls.getConstructor(String.class);
  cn.setAccessible(true);
  String firstLine = this.getMessage();
  int eol = firstLine.indexOf('\n');
  if (eol>=0) {
    firstLine = firstLine.substring(0, eol);
  }
  IOException ex = cn.newInstance(firstLine);
  ex.initCause(this);
  return ex;
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:14,代码来源:RemoteException.java

示例14: load

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Override
public CachedConstructor load(final Class<?> type) throws Exception {
    try {
        validateType(type);
        Constructor<?> constructor = selectConstructor(type);
        constructor.setAccessible(true);
        return CachedConstructor.of(constructor);
    } catch (Throwable e) {
        return CachedConstructor.of(e);
    }
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:12,代码来源:DependencyInjectingInstantiator.java

示例15: jskStringTest

import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Test(expected = JSKException.class)
  public void jskStringTest() throws Exception {

Constructor<JSKString> constructor = JSKString.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));

try {
	constructor.setAccessible(true);
	constructor.newInstance();
}
catch(Exception exception) {
	throw new JSKException(ERROR_UTILITY_CLASS);
}
  }
 
开发者ID:Varoso,项目名称:JSK,代码行数:15,代码来源:JSKStringTest.java


注:本文中的java.lang.reflect.Constructor.setAccessible方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。