tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1100
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    lernen.2007 lernen.2007 ist offline Mitglied Platin
    Registriert seit
    Mar 2005
    Beiträge
    743
    Hallo Leute,

    gibts eine Möglichkeit, wo ich dann sagen kann, bevor bestimmte Klassen Methode-/n ausgeführt werden, sollte zuerst ein andere Methode ausgeführt werden. D.h. ruf zuerst die Methode auf bevor du den anderen aufrufst.

    Gruß
    erkan
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo,

    das kann man ganz einfach mit Dynamic Proxies realisieren:
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    
    /**
     * 
     */
    package de.tutorials;
     
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
     
    /**
     * @author Tom
     */
    public class DynamicProxyExample {
     
      /**
       * @param args
       */
      public static void main(String[] args) throws Exception {
        IService service = (IService) Proxy.newProxyInstance(DynamicProxyExample.class.getClassLoader(),
          new Class[] { IService.class }, new TargetAwareSecureMethodInvocationHandler<IService>(new Service()));
        
        service.businessOperation();
        System.out.println("########");
        service.anotherBusinessOperation();
      }
     
      @Retention(RetentionPolicy.RUNTIME)
      @Target( { ElementType.METHOD, ElementType.TYPE })
      static @interface Secure {
      }
     
      static interface IService {
        void businessOperation();
     
     
        @Secure
        void anotherBusinessOperation();
      }
     
      static class Service implements IService {
        public void anotherBusinessOperation() {
          System.out.println("anotherBusinessOperation");
        }
     
     
        public void businessOperation() {
          System.out.println("businessOperation");
        }
      }
     
      static class TargetAwareSecureMethodInvocationHandler<TTarget> implements InvocationHandler {
     
        TTarget target;
     
     
        /**
         * @param target
         */
        public TargetAwareSecureMethodInvocationHandler(TTarget target) {
          super();
          this.target = target;
        }
     
     
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          if (method.isAnnotationPresent(Secure.class)) {
            checkAccess(method);
          }
          return method.invoke(getTarget(), args);
        }
     
     
        private void checkAccess(Method method) {
          System.out.println("perform access check for: " + method);
        }
     
     
        /**
         * @return the target
         */
        public TTarget getTarget() {
          return target;
        }
     
     
        /**
         * @param target the target to set
         */
        public void setTarget(TTarget target) {
          this.target = target;
        }
     
      }
    }

    Ausgabe:
    Code :
    1
    2
    3
    4
    
    businessOperation
    ########
    perform access check for: public abstract void de.tutorials.DynamicProxyExample$IService.anotherBusinessOperation()
    anotherBusinessOperation

    Eine weitere Variante wäre auch die Verwendung von AspectJ

    Gruß Tom
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

  3. #3
    lernen.2007 lernen.2007 ist offline Mitglied Platin
    Registriert seit
    Mar 2005
    Beiträge
    743
    Danke Thomas.

    Es hat funktioniert

     

Ähnliche Themen

  1. Methode in andere Klasse aufrufen?
    Von jbjb im Forum Java Grundlagen
    Antworten: 2
    Letzter Beitrag: 07.01.10, 21:49
  2. Mittels JS eine C# Methode aufrufen
    Von WaZZkeSS im Forum .NET Web und Kommunikation
    Antworten: 2
    Letzter Beitrag: 11.06.09, 20:25
  3. Mit VB.Net eine PHP-Methode aufrufen
    Von codeman im Forum .NET Web und Kommunikation
    Antworten: 0
    Letzter Beitrag: 23.11.07, 15:26
  4. Antworten: 3
    Letzter Beitrag: 26.10.05, 12:16
  5. Antworten: 3
    Letzter Beitrag: 29.01.05, 17:55