001/*
002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 */
009
010package org.eclipse.golo.runtime.augmentation;
011
012import java.util.stream.Stream;
013import org.eclipse.golo.runtime.MethodInvocation;
014
015import static java.lang.reflect.Modifier.*;
016import static org.eclipse.golo.runtime.augmentation.DefiningModule.Scope;
017
018/**
019 * Encapsulate runtime information for an augmentation application resolution.
020 */
021public final class AugmentationApplication {
022
023  enum Kind { SIMPLE, NAMED }
024
025  private final Class<?> augmentation;
026  private final Class<?> target;
027  private final Scope scope;
028  private final Kind kind;
029
030  AugmentationApplication(Class<?> augmentation, Class<?> target, Scope scope, Kind kind) {
031    this.scope = scope;
032    this.target = target;
033    this.augmentation = augmentation;
034    this.kind = kind;
035  }
036
037  @Override
038  public String toString() {
039    return String.format("AugmentationApplication<%s,%s,%s,%s>", augmentation, target, scope, kind);
040  }
041
042  public Stream<AugmentationMethod> methodsMaching(MethodInvocation invocation) {
043    if (augmentation == null) {
044      return Stream.empty();
045    }
046    return Stream.of(augmentation.getMethods())
047      .filter(method -> invocation.match(method))
048      .map(method -> new AugmentationMethod(kind, scope, target, method));
049  }
050}
051