Skip to content

Commit

Permalink
Merge pull request #41 from DoclerLabs/develop
Browse files Browse the repository at this point in the history
prepare 0.33.0
  • Loading branch information
aliokan authored Nov 21, 2017
2 parents 85812df + 2c2d5df commit 7f03b0e
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 38 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Francis Bourre

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions src/hex/di/IInjectable.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hex.di;

/**
* @deprecated
* @author Francis Bourre
*/
#if !macro
@:remove
@:autoBuild( hex.di.annotation.FastAnnotationReader.readMetadata( hex.di.IInjectable ) )
#end
interface IInjectable
{

}
2 changes: 1 addition & 1 deletion src/hex/di/IInjectorContainer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package hex.di;
*/
#if !macro
@:remove
@:autoBuild( hex.di.annotation.FastAnnotationReader.readMetadata( hex.di.IInjectorContainer ) )
@:autoBuild( hex.di.annotation.AnnotationTransformer.readMetadata( hex.di.IInjectorContainer ) )
#end
interface IInjectorContainer
{
Expand Down
109 changes: 109 additions & 0 deletions src/hex/di/annotation/AnnotationTransformer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package hex.di.annotation;

import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Expr.Field;
import hex.annotation.AnnotationReplaceBuilder;
import hex.error.PrivateConstructorException;
import hex.reflect.ClassReflectionData;

using Lambda;

/**
* ...
* @author Francis Bourre
*/
class AnnotationTransformer
{
#if macro
private static var _map : Map<String, Bool> = new Map();

/** @private */
function new()
{
throw new PrivateConstructorException();
}

macro public static function readMetadata( metadataExpr : Expr ) : Array<Field>
{
//if it's an interface we don't want to build reflection data
if ( Context.getLocalClass().get().isInterface )
{
return Context.getBuildFields();
}

return reflect( metadataExpr, Context.getBuildFields() );
}

public static function reflect( metadataExpr : Expr, fields : Array<Field> ) : Array<Field>
{
var localClass = Context.getLocalClass().get();
var className = localClass.pack.join( "." ) + "." + localClass.name;
var hasBeenBuilt = AnnotationTransformer._map.exists( className );

//Reflection data to be used to generate fields
var data : ClassReflectionData;

var annotationFilter = [ "Inject", "PostConstruct", "Optional", "PreDestroy" ];

// use AnnotationReplaceBuilder to to process the metadata but only the ones that we care about
fields
.flatMap( function ( f ) return f.meta )
.filter( function ( m ) return annotationFilter.indexOf( m.name ) != -1 )
.map( AnnotationReplaceBuilder.processMetadata );

if ( hasBeenBuilt )
{
// get the existing data and remove them from the static_classes
var existingData = hex.reflect.ReflectionBuilder._static_classes.find( function(d) return d.name == localClass.module );
hex.reflect.ReflectionBuilder._static_classes.remove( existingData );

// reflect new fields
fields = hex.reflect.ReflectionBuilder.parseMetadata( metadataExpr, fields, annotationFilter, false );

// get new fields
data = hex.reflect.ReflectionBuilder._static_classes[ hex.reflect.ReflectionBuilder._static_classes.length - 1 ];

//merge everything together
data = mergeReflectionData( existingData, data );

//write the merged data back
hex.reflect.ReflectionBuilder._static_classes[ hex.reflect.ReflectionBuilder._static_classes.length - 1 ] = data;
}
else
{
//parse annotations
fields = hex.reflect.ReflectionBuilder.parseMetadata( metadataExpr, fields, annotationFilter, false );

//get/set data result
data = hex.reflect.ReflectionBuilder._static_classes[ hex.reflect.ReflectionBuilder._static_classes.length - 1 ];
}

AnnotationTransformer._map.set( className, true );

var f = fields.filter( function ( f ) { return f.name == "__ai" || f.name == "__ac" || f.name == "__ap"; } );
if ( f.length != 0 )
{
//remove existing reflection data
for ( removedField in f ) fields.remove( removedField );
}

// Generate and append fields
hex.di.annotation.FastInjectionBuilder._generateInjectionProcessorExpr( fields, data );
return fields;
}

static private function mergeReflectionData( data1 : ClassReflectionData, data2 : ClassReflectionData ) : ClassReflectionData
{
return
{
name: data1.name,
superClassName: data1.superClassName,
constructor: data2.constructor, // using constructor from the new data (nothing to merge here)
properties: data1.properties.concat( data2.properties ),
methods: data1.methods.concat( data2.methods )
};
}

#end
}
9 changes: 6 additions & 3 deletions src/hex/di/annotation/FastAnnotationReader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import hex.reflect.ClassReflectionData;
using Lambda;

/**
* ...
* This class is used to generate reflection footprints.
* It was designed previously for automatic DI.
* @see AnnotationTransformer for faster implementation.
*
* @author Francis Bourre
*/
class FastAnnotationReader
Expand Down Expand Up @@ -56,7 +59,7 @@ class FastAnnotationReader
{
// get the existing data and remove them from the static_classes
var existingData = hex.reflect.ReflectionBuilder._static_classes.find( function(d) return d.name == localClass.module );
hex.reflect.ReflectionBuilder._static_classes.remove(existingData);
hex.reflect.ReflectionBuilder._static_classes.remove( existingData );

// reflect new fields
fields = hex.reflect.ReflectionBuilder.parseMetadata( metadataExpr, fields, annotationFilter, false );
Expand All @@ -65,7 +68,7 @@ class FastAnnotationReader
var data = hex.reflect.ReflectionBuilder._static_classes[ hex.reflect.ReflectionBuilder._static_classes.length - 1 ];

//merge everything together
var mergedData = mergeReflectionData(existingData, data);
var mergedData = mergeReflectionData( existingData, data );

//write the merged data back
hex.reflect.ReflectionBuilder._static_classes[ hex.reflect.ReflectionBuilder._static_classes.length - 1 ] = mergedData;
Expand Down
Loading

0 comments on commit 7f03b0e

Please sign in to comment.