@Override protected Object doGetText(Object element) { Object text = textDispatcher.invoke(element); if (text != null) { return text; } return null; }
/** Constructor. * * @param keywordProvider the provider of extra-language keywords. */ public AbstractExpressionGenerator(IExtraLanguageKeywordProvider keywordProvider) { assert keywordProvider != null; this.generateDispatcher = new PolymorphicDispatcher<>( "_generate", 3, 3, //$NON-NLS-1$ Collections.singletonList(this)); this.keywords = keywordProvider; }
@Override protected IScope handleNoSuchMethod(Object... params) { if (PolymorphicDispatcher.NullErrorHandler.class.equals(errorHandler.getClass())) return null; return super.handleNoSuchMethod(params); } };
@Override protected Collection<FollowElement> getFollowElements(AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>(methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
@SuppressWarnings("unchecked") public RT invoke(Object... params) { if (methodFilter instanceof MethodNameFilter) { MethodNameFilter filter = (MethodNameFilter) methodFilter; if (params.length<filter.getMinParams() || params.length > filter.getMaxParams()) { throw new IllegalArgumentException("Wrong number of arguments. Expected "+filter.getMinParams()+" to "+filter.getMaxParams()+"."); } } List<MethodDesc> result = cache.get(getTypes(params)); // check if ambiguous if (result.size()>1) return handleAmbigousMethods(result, params); if (result.isEmpty()) return handleNoSuchMethod(params); try { MethodDesc current = result.get(0); current.method.setAccessible(true); return (RT) current.method.invoke(current.target, params); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error) e.getTargetException(); return handler.handle(params, e.getTargetException()); } catch (IllegalArgumentException e) { return handler.handle(params, e); } catch (IllegalAccessException e) { return handler.handle(params, e); } }
@Override public Class<?> getDefaultClass(int paramIndex) { if (paramIndex == 0) return EObject.class; return super.getDefaultClass(paramIndex); } };
@Override protected Collection<FollowElement> getFollowElements(AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>(methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
@SuppressWarnings("unchecked") public RT invoke(Object... params) { if (methodFilter instanceof MethodNameFilter) { MethodNameFilter filter = (MethodNameFilter) methodFilter; if (params.length<filter.getMinParams() || params.length > filter.getMaxParams()) { throw new IllegalArgumentException("Wrong number of arguments. Expected "+filter.getMinParams()+" to "+filter.getMaxParams()+"."); } } List<MethodDesc> result = cache.get(getTypes(params)); // check if ambiguous if (result.size()>1) return handleAmbigousMethods(result, params); if (result.isEmpty()) return handleNoSuchMethod(params); try { MethodDesc current = result.get(0); current.method.setAccessible(true); return (RT) current.method.invoke(current.target, params); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error) e.getTargetException(); return handler.handle(params, e.getTargetException()); } catch (IllegalArgumentException e) { return handler.handle(params, e); } catch (IllegalAccessException e) { return handler.handle(params, e); } }
/** * @param params * @return */ private List<Class<?>> getTypes(Object[] params) { List<Class<?>> result = new ArrayList<Class<?>>(params.length); for (int i = 0; i < params.length; i++) { if (params[i]!=null) { result.add(params[i].getClass()); } else { result.add(getDefaultClass(i)); } } return result; }
@Override protected Collection<FollowElement> getFollowElements(AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>(methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
/** Generate the given object. * * @param object the object. * @param context the context. */ protected void generate(EObject object, IExtraLanguageGeneratorContext context) { this.generateDispatcher.invoke(object, context); }
public static <T> PolymorphicDispatcher<T> createForSingleTarget(final String methodName, int min, int max, final Object singleTarget) { return new PolymorphicDispatcher<T>(methodName, min, max, Collections.singletonList(singleTarget)); }
@Override protected IScope handleNoSuchMethod(Object... params) { if (PolymorphicDispatcher.NullErrorHandler.class .equals(errorHandler.getClass())) { return null; } return super.handleNoSuchMethod(params); } };
/** * @param params * @return */ private List<Class<?>> getTypes(Object[] params) { List<Class<?>> result = new ArrayList<Class<?>>(params.length); for (int i = 0; i < params.length; i++) { if (params[i]!=null) { result.add(params[i].getClass()); } else { result.add(getDefaultClass(i)); } } return result; }
@Override protected Collection<FollowElement> getFollowElements(AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>(methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
public String string(Object object) { // don't pass null argument to polymorphic dispatch // otherwise we get ambiguos methods error if (object == null) { return NULL_REPRESENTATION; } return dispatcher.invoke(object); }
public static <T> PolymorphicDispatcher<T> createForSingleTarget(final String methodName, final Object singleTarget) { return new PolymorphicDispatcher<T>(methodName, Collections.singletonList(singleTarget)); }
@Override protected IScope handleNoSuchMethod(Object... params) { if (PolymorphicDispatcher.NullErrorHandler.class.equals(errorHandler.getClass())) return null; return super.handleNoSuchMethod(params); } };
@Override protected Collection<FollowElement> getFollowElements(AbstractInternalContentAssistParser parser) { if (rule == null || rule.eIsProxy()) return Collections.emptyList(); String methodName = "entryRule" + rule.getName(); PolymorphicDispatcher<Collection<FollowElement>> dispatcher = new PolymorphicDispatcher<Collection<FollowElement>>(methodName, 0, 0, Collections.singletonList(parser)); dispatcher.invoke(); return parser.getFollowElements(); }
/** Do something after the generation of the given object. * * @param object the object. * @param context the context. */ protected void after(EObject object, IExtraLanguageGeneratorContext context) { this.afterDispatcher.invoke(object, context); }