Aspect.cs :  » Aspect-Oriented-Frameworks » NKalore-Compiler » Mono » CSharp » AOP » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Aspect Oriented Frameworks » NKalore Compiler 
NKalore Compiler » Mono » CSharp » AOP » Aspect.cs
//
// NKalore Compiler related class
//
// Licensed under the terms of the GNU GPL
//
// Author:
//   Guilherme Labigalini (glabega@gmail.com), 2005
//
// http://aspectsharpcomp.sourceforge.net/

using System;
using System.Collections;

namespace Mono.CSharp.AOP{
  public class Aspect: Class
  {
    private ArrayList pointCuts;
    private ArrayList advices;

    #region CTOR
    public Aspect(NamespaceEntry ns, TypeContainer parent, string name, int mod,
      Attributes attrs, Location l)
      : base (ns, parent, name, Modifiers.INTERNAL | Modifiers.SEALED, attrs, l)
    {
      pointCuts = new ArrayList();
      advices = new ArrayList();
    }
    #endregion

    #region Public Members
    public override AdditionResult AddField (Field field)
    {
      field.ModFlags |= Modifiers.STATIC;
      return base.AddField(field);
    }

    public AdditionResult AddPointCut(PointCut pointCut)
    {
      AdditionResult res;
      string basename = pointCut.Name;
      string fullname = Name + "." + basename;

      if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
        return res;

      pointCuts.Add (pointCut);

      DefineName (fullname, pointCut);

      return AdditionResult.Success;
    }

    public AdditionResult AddAdvice(Advice advice)
    {
      advices.Add (advice);

      return AdditionResult.Success;
    }

    public override bool DefineMembers(TypeContainer parent)
    {
      //make all members in the aspect static 
      DefineStatic();

      //generate a private and static constructor or make user constructor private and static
      DefineConstructor();

      //apply the advicess to user classes
      DefineAdvices(true);
      DefineAdvices(false);

      return base.DefineMembers(parent);
    }

    #endregion
  
    #region Private Members
    private PointCut FindPointCut(Advice advice)
    {
      foreach (PointCut pointCut in pointCuts)
      {
        if (advice.AdviceName == pointCut.Name)
        {
          if (advice.CanHandle(pointCut))
          {
            return pointCut;
          }
          break;
        }
      }
      return null;
    }

    private void DefineAdvices(bool arounds)
    {
      foreach (Advice advice in advices)
      {
        if ((arounds && (! (advice is Around))) ||
          ((! arounds) && (advice is Around)))
        {
          continue;
        }

        AddMethod(advice);

        PointCut pointCut = FindPointCut(advice);

        if (pointCut == null)
        {
          Report.Error(0, 
            advice.Location, 
            "Pointcut named '{0}' does not exists", advice.AdviceName);
        }

        advice.ApplyAspect(pointCut);
      }      
    }

    private void AddPrivateConstrutor()
    {
      Location l = this.Location;
      Constructor c = new Constructor(this, this.Basename, 
        Modifiers.PRIVATE, 
        new Parameters(null, false, l), null, l);
      c.Block = new Block(null);
      this.AddConstructor(c);
    }

    private void DefineConstructor()
    {  
      int numConstructor = 0; 
      
      if (InstanceConstructors != null)
        numConstructor = this.InstanceConstructors.Count;

      if (numConstructor == 0)
      {
        AddPrivateConstrutor();
      }
      else if (numConstructor == 1 && default_static_constructor == null)
      {
        Constructor c = (Constructor)InstanceConstructors[0];
        if ((c.ModFlags & Modifiers.STATIC) != 0)
          return;
        if (! c.Parameters.Empty)
          Report.Error(0, c.Location, 
            "Arguments are not allowed in an aspect constructors");
        //set the protection level of the constructor to protected
        c.ModFlags = Modifiers.STATIC;
        this.InstanceConstructors.Clear();
        this.AddConstructor(c);

        AddPrivateConstrutor();
      }
      else
      {
        Report.Error(0, this.Location, "Multiple constructors are not allowed in an aspect");
      }
    }

    private void SetStatic(ArrayList members)
    {
      if (members != null)
      {
        foreach (MemberCore m in members)
          m.ModFlags |= Modifiers.STATIC;
      }
    }

    private void DefineStatic()
    {
      SetStatic(this.Methods);
      SetStatic(this.Fields);
    }
    #endregion

  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.