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


Java ReflectionUtils.makeAccessible方法代码示例

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


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

示例1: toString

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
private String toString(DataSource dataSource) {
    if (dataSource == null) {
        return "<none>";
    } else {
        try {
            Field urlField = ReflectionUtils.findField(dataSource.getClass(), "url");
            ReflectionUtils.makeAccessible(urlField);
            return stripCredentials((String) urlField.get(dataSource));
        } catch (Exception fe) {
            try {
                Method urlMethod = ReflectionUtils.findMethod(dataSource.getClass(), "getUrl");
                ReflectionUtils.makeAccessible(urlMethod);
                return stripCredentials((String) urlMethod.invoke(dataSource, (Object[])null));
            } catch (Exception me){
                return "<unknown> " + dataSource.getClass();
            }
        }
    }
}
 
开发者ID:PacktPublishing,项目名称:Cloud-Foundry-For-Developers,代码行数:20,代码来源:HomeController.java

示例2: merge

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
 * 合并pipeline参数设置
 */
public void merge(PipelineParameter pipelineParameter) {
    try {
        Field[] fields = this.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            // Skip static and final fields.
            if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
                continue;
            }

            ReflectionUtils.makeAccessible(field);
            Object srcValue = field.get(pipelineParameter);
            if (srcValue != null) { // 忽略null值
                field.set(this, srcValue);
            }
        }
    } catch (Exception e) {
        // ignore
    }
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:24,代码来源:PipelineParameter.java

示例3: invokeMethod

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
 * 调用方法,可以是一些私有方法
 * 
 * @param target
 * @param methodName
 * @param args
 * @return
 * @throws Exception
 */
public static Object invokeMethod(Object target, String methodName, Object... args) throws Exception {
    Method method = null;
    // 查找对应的方法
    if (args == null || args.length == 0) {
        method = ReflectionUtils.findMethod(target.getClass(), methodName);
    } else {
        Class[] argsClass = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            argsClass[i] = args[i].getClass();
        }
        method = ReflectionUtils.findMethod(target.getClass(), methodName, argsClass);
    }
    ReflectionUtils.makeAccessible(method);

    if (args == null || args.length == 0) {
        return ReflectionUtils.invokeMethod(method, target);
    } else {
        return ReflectionUtils.invokeMethod(method, target, args);
    }
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:30,代码来源:TestUtils.java

示例4: encode

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@PrePersist
@PreUpdate
public void encode(Object target) {
	AnnotationCheckingMetadata metadata = AnnotationCheckingMetadata.getMetadata(target.getClass());
	if (metadata.isCheckable()) {
		StringBuilder sb = new StringBuilder();
		for (Field field : metadata.getCheckedFields()) {
			ReflectionUtils.makeAccessible(field);
			Object value = ReflectionUtils.getField(field, target);
			if (value instanceof Date) {
				throw new RuntimeException("不支持时间类型字段加密!");
			}
			sb.append(value).append(" - ");
		}
		sb.append(MD5_KEY);
		LOGGER.debug("加密数据:" + sb);
		String hex = MD5Utils.encode(sb.toString());
		Field checksumField = metadata.getCheckableField();
		ReflectionUtils.makeAccessible(checksumField);
		ReflectionUtils.setField(checksumField, target, hex);
	}
}
 
开发者ID:onsoul,项目名称:os,代码行数:23,代码来源:CheckingEntityListener.java

示例5: after

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@Override protected void after()
{
    // For all objects that have been tampered with, we'll revert them to their original state.
    for (int i = pristineFieldValues.size() - 1; i >= 0; i-- )
    {
        FieldValueOverride override = pristineFieldValues.get(i);
        
        if (log.isDebugEnabled())
        {
            log.debug("Reverting mocked field '" + override.fieldName + "' on object " + override.objectContainingField + " to original value '" + override.fieldPristineValue + "'");
        }
        
        // Hack into the Java field object
        Field f = ReflectionUtils.findField(override.objectContainingField.getClass(), override.fieldName);
        ReflectionUtils.makeAccessible(f);
        // and revert its value.
        ReflectionUtils.setField(f, override.objectContainingField, override.fieldPristineValue);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:20,代码来源:TemporaryMockOverride.java

示例6: testNotInPeriod

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@Test
public void testNotInPeriod() {
    ReflectionUtils.makeAccessible(inPeriodMethod);
    String rule = "[email protected]@qwas:[email protected]:40-15:00";
    boolean isInPeriod = (Boolean) ReflectionUtils.invokeMethod(inPeriodMethod, monitor, new Object[] { rule });
    Assert.assertFalse(isInPeriod);
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:8,代码来源:AbstractRuleMonitorInPeriodTest.java

示例7: testErrorFormatInPeriod

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@Test
public void testErrorFormatInPeriod() {
    ReflectionUtils.makeAccessible(inPeriodMethod);
    String rule = "[email protected]@qwas:[email protected]:331-15:00";
    boolean isInPeriod = (Boolean) ReflectionUtils.invokeMethod(inPeriodMethod, monitor, new Object[] { rule });
    Assert.assertTrue(isInPeriod);
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:8,代码来源:AbstractRuleMonitorInPeriodTest.java

示例8: assertDataSourceOfType

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private <T extends DataSource> FlexyPoolDataSource<T> assertDataSourceOfType(DataSource dataSource, Class<T> realDataSourceClass) {
    assertThat(dataSource).isInstanceOf(DecoratedDataSource.class);
    DataSource decoratedDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource();
    assertThat(decoratedDataSource).isInstanceOf(FlexyPoolDataSource.class);
    Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "targetDataSource");
    ReflectionUtils.makeAccessible(field);
    Object targetDataSource = ReflectionUtils.getField(field, decoratedDataSource);
    assertThat(targetDataSource).isInstanceOf(realDataSourceClass);
    return (FlexyPoolDataSource<T>) decoratedDataSource;
}
 
开发者ID:gavlyukovskiy,项目名称:spring-boot-data-source-decorator,代码行数:12,代码来源:FlexyPoolConfigurationTests.java

示例9: JBossMCAdapter

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public JBossMCAdapter(ClassLoader classLoader) {
	try {
		// Resolve BaseClassLoader.class
		Class<?> clazzLoaderType = classLoader.loadClass(LOADER_NAME);

		ClassLoader clazzLoader = null;
		// Walk the hierarchy to detect the instrumentation aware ClassLoader
		for (ClassLoader cl = classLoader; cl != null && clazzLoader == null; cl = cl.getParent()) {
			if (clazzLoaderType.isInstance(cl)) {
				clazzLoader = cl;
			}
		}

		if (clazzLoader == null) {
			throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: " +
					"A [" + LOADER_NAME + "] implementation is required.");
		}

		this.classLoader = clazzLoader;
		// Use the ClassLoader that loaded the ClassLoader to load the types for reflection purposes
		classLoader = clazzLoader.getClass().getClassLoader();

		// BaseClassLoader#getPolicy
		Method method = clazzLoaderType.getDeclaredMethod("getPolicy");
		ReflectionUtils.makeAccessible(method);
		this.target = method.invoke(this.classLoader);

		// Check existence of BaseClassLoaderPolicy#addTranslator(Translator)
		this.translatorClass = classLoader.loadClass(TRANSLATOR_NAME);
		this.addTranslator = this.target.getClass().getMethod("addTranslator", this.translatorClass);
	}
	catch (Exception ex) {
		throw new IllegalStateException(
				"Could not initialize JBoss LoadTimeWeaver because the JBoss 6 API classes are not available", ex);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:37,代码来源:JBossMCAdapter.java

示例10: getVarType

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
protected int getVarType(ReflectionVar v) {
	try {
		ReflectionUtils.makeAccessible(varTypeField);
		return (Integer) varTypeField.get(v);
	}
	catch (IllegalAccessException ex) {
		throw new IllegalStateException(ex);
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:10,代码来源:RuntimeTestWalker.java

示例11: testCriticalInPeriod

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
@Test
public void testCriticalInPeriod() {
    ReflectionUtils.makeAccessible(inPeriodMethod);
    String rule = "[email protected]@qwas:[email protected]:33-15:00";
    boolean isInPeriod = (Boolean) ReflectionUtils.invokeMethod(inPeriodMethod, monitor, new Object[] { rule });
    Assert.assertTrue(isInPeriod);
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:8,代码来源:AbstractRuleMonitorInPeriodTest.java

示例12: setTemporaryField

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public void setTemporaryField(Object objectContainingField, String fieldName, Object fieldValue)
{
    if (log.isDebugEnabled())
    {
        log.debug("Overriding field '" + fieldName + "' on object " + objectContainingField + " to new value '" + fieldValue + "'");
    }
    
    // Extract the pristine value of the field we're going to mock.
    Field f = ReflectionUtils.findField(objectContainingField.getClass(), fieldName);
    
    if (f == null)
    {
        final String msg = "Object of type '" + objectContainingField.getClass().getSimpleName() + "' has no field named '" + fieldName + "'";
        if (log.isDebugEnabled())
        {
            log.debug(msg);
        }
        throw new IllegalArgumentException(msg);
    }
    
    ReflectionUtils.makeAccessible(f);
    Object pristineValue = ReflectionUtils.getField(f, objectContainingField);
    
    // and add it to the list.
    pristineFieldValues.add(new FieldValueOverride(objectContainingField, fieldName, pristineValue));
    
    // and set it on the object
    ReflectionUtils.setField(f, objectContainingField, fieldValue);
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:30,代码来源:TemporaryMockOverride.java

示例13: testDefaultJarSettings

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public void testDefaultJarSettings() throws Exception {

		Properties defaultSettings = bundleCreator.getSettings();
		Field field = ReflectionUtils.findField(AbstractConfigurableBundleCreatorTests.class, "jarSettings" , Properties.class);
		ReflectionUtils.makeAccessible(field);
		ReflectionUtils.setField(field, null, defaultSettings);
		assertNotNull(defaultSettings);
		assertNotNull(bundleCreator.getRootPath());
		assertNotNull(bundleCreator.getBundleContentPattern());
		assertNotNull(bundleCreator.getManifestLocation());
	}
 
开发者ID:eclipse,项目名称:gemini.blueprint,代码行数:12,代码来源:ConfigurableBundleCreatorTestsTest.java

示例14: executeFunctionJLRMethod

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
/**
 * Execute a function represented as a java.lang.reflect.Method.
 *
 * @param state the expression evaluation state
 * @param the java method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
		throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
				functionArgs.length, method.getParameterTypes().length);
	}
	// Only static methods can be called in this way
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC,
				method.getDeclaringClass().getName() + "." + method.getName(), this.name);
	}

	// Convert arguments if necessary and remap them for varargs if required
	if (functionArgs != null) {
		TypeConverter converter = state.getEvaluationContext().getTypeConverter();
		ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	}
	if (method.isVarArgs()) {
		functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
				method.getParameterTypes(), functionArgs);
	}

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method,-1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:43,代码来源:FunctionReference.java

示例15: setLastTimeRun

import org.springframework.util.ReflectionUtils; //导入方法依赖的package包/类
public void setLastTimeRun(LocalDateTime lastTimeRun) {
    Field lastTimeRunField = ReflectionUtils.findField(AbstractNotifier.class, "lastTimeRun");
    ReflectionUtils.makeAccessible(lastTimeRunField);
    ReflectionUtils.setField(lastTimeRunField, this, lastTimeRun);
}
 
开发者ID:gilles-stragier,项目名称:quickmon,代码行数:6,代码来源:AbstractNotifierTest.java


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