org.eclipse.xtext.util.ReflectionUtil Java Examples

The following examples show how to use org.eclipse.xtext.util.ReflectionUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticImplicitMethodsFeatureForTypeProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Map<String, Collection<String>> denormalize(Multimap<Class<?>, Class<?>> classMapping) {
	Multimap<String, String> result = LinkedHashMultimap.create();
	for(Map.Entry<Class<?>, Class<?>> entry: classMapping.entries()) {
		Class<?> key = entry.getKey();
		Class<?> keyObjectType = ReflectionUtil.getObjectType(key);
		Class<?> value = entry.getValue();
		for(Method method: value.getDeclaredMethods()) {
			if (Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length > 0) {
				Class<?> paramType = method.getParameterTypes()[0];
				Class<?> paramObjectType = ReflectionUtil.getObjectType(paramType);		
				if (keyObjectType.isAssignableFrom(paramObjectType)) {
					result.put(paramObjectType.getCanonicalName(), value.getCanonicalName());
				}
			}
		}
	}
	return ImmutableMultimap.copyOf(result).asMap();
}
 
Example #2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param context unused in this context but required for dispatching
 * @param indicator unused in this context but required for dispatching
 */
@SuppressWarnings("unchecked")
protected Object _doEvaluate(XNumberLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference expectedType = resolvedTypes.getExpectedType(literal);
	Class<? extends Number> type = numberLiterals.getJavaType(literal);
	if (expectedType != null && expectedType.isSubtypeOf(Number.class)) {
		try {
			Class<?> expectedClassType = getJavaType(expectedType.toJavaCompliantTypeReference().getType());
			if (expectedClassType.isPrimitive()) {
				expectedClassType = ReflectionUtil.getObjectType(expectedClassType);
			}
			if (Number.class != expectedClassType && Number.class.isAssignableFrom(expectedClassType)) {
				type = (Class<? extends Number>) expectedClassType;
			}
		} catch (ClassNotFoundException e) {
		}
	}
	return numberLiterals.numberValue(literal, type);
}
 
Example #3
Source File: GenericModuleTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isConfigured(CompoundModule module, Class<?> from, Object to, boolean singleton,
		boolean eagerSingleton, boolean provider) {
	for(Module m: module.getModules()) {
		if (m instanceof MethodBasedModule) {
			MethodBasedModule mod = (MethodBasedModule) m;
			if (mod.isSingleton()!=singleton)
				continue;
			if (mod.isEager()!=eagerSingleton)
				continue;
			if (provider != m instanceof ProviderModule)
				continue;
			if (!ReflectionUtil.getRawType(mod.getKeyType()).equals(from))
				continue;
			Object object = mod.invokeMethod();
			if (object==null && to==null || object!=null && object.equals(to))
				return true;
		}
	}
	return false;
}
 
Example #4
Source File: JavaInlineExpressionCompiler.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Append the inline code for the given number value.
 *
 * @param expression the expression of the operation.
 * @param parentExpression is the expression that contains this one, or {@code null} if the current expression is
 *     the root expression.
 * @param feature the feature that contains the expression.
 * @param output the output.
 * @return {@code true} if a text was appended.
 */
@SuppressWarnings("static-method")
protected Boolean _generate(Number expression, XExpression parentExpression, XtendExecutable feature,
		InlineAnnotationTreeAppendable output) {
	final Class<?> type = ReflectionUtil.getRawType(expression.getClass());
	if (Byte.class.equals(type) || byte.class.equals(type)) {
		output.appendConstant("(byte) (" + expression.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
	} else if (Short.class.equals(type) || short.class.equals(type)) {
		output.appendConstant("(short) (" + expression.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
	} else if (Float.class.equals(type) || float.class.equals(type)) {
		output.appendConstant(expression.toString() + "f"); //$NON-NLS-1$
	} else {
		output.appendConstant(expression.toString());
	}
	return Boolean.TRUE;
}
 
Example #5
Source File: ProviderModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isInstanceOf(Type keyType, Class<?> class1) {
	return class1.isAssignableFrom(ReflectionUtil.getRawType(keyType));
}