21
21
import java .util .Collection ;
22
22
import java .util .HashSet ;
23
23
import java .util .List ;
24
- import java .util .Map ;
25
24
import java .util .Set ;
26
25
27
26
import com .google .common .collect .ImmutableList ;
28
- import com .google .common .collect .ImmutableMap ;
29
27
import com .google .common .collect .ImmutableSet ;
30
28
import com .google .common .primitives .Booleans ;
31
29
import com .google .common .primitives .Bytes ;
36
34
import com .google .common .primitives .Longs ;
37
35
import com .google .common .primitives .Shorts ;
38
36
import com .tngtech .archunit .Internal ;
39
- import com .tngtech .archunit .base .Function ;
40
37
import com .tngtech .archunit .base .HasDescription ;
41
38
import com .tngtech .archunit .base .Optional ;
42
39
import com .tngtech .archunit .core .MayResolveTypesViaReflection ;
@@ -207,7 +204,7 @@ public FieldVisitor visitField(int access, String name, String desc, String sign
207
204
208
205
DomainBuilders .JavaFieldBuilder fieldBuilder = new DomainBuilders .JavaFieldBuilder ()
209
206
.withName (name )
210
- .withType (JavaTypeImporter .importAsmType (Type . getType ( desc ) ))
207
+ .withType (JavaTypeImporter .importAsmType (desc ))
211
208
.withModifiers (JavaModifier .getModifiersForField (access ))
212
209
.withDescriptor (desc );
213
210
declarationHandler .onDeclaredField (fieldBuilder );
@@ -221,29 +218,21 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si
221
218
}
222
219
223
220
LOG .trace ("Analyzing method {}.{}:{}" , className , name , desc );
224
- accessHandler .setContext (new CodeUnit (name , namesOf (Type .getArgumentTypes (desc )), className ));
221
+ List <JavaType > parameters = JavaTypeImporter .importAsmMethodArgumentTypes (desc );
222
+ accessHandler .setContext (new CodeUnit (name , namesOf (parameters ), className ));
225
223
226
224
DomainBuilders .JavaCodeUnitBuilder <?, ?> codeUnitBuilder = addCodeUnitBuilder (name );
227
- Type methodType = Type .getMethodType (desc );
228
225
codeUnitBuilder
229
226
.withName (name )
230
227
.withModifiers (JavaModifier .getModifiersForMethod (access ))
231
- .withParameters (typesFrom ( methodType . getArgumentTypes ()) )
232
- .withReturnType (JavaTypeImporter .importAsmType ( methodType . getReturnType () ))
228
+ .withParameters (parameters )
229
+ .withReturnType (JavaTypeImporter .importAsmMethodReturnType ( desc ))
233
230
.withDescriptor (desc )
234
231
.withThrowsClause (typesFrom (exceptions ));
235
232
236
233
return new MethodProcessor (className , accessHandler , codeUnitBuilder );
237
234
}
238
235
239
- private List <JavaType > typesFrom (Type [] asmTypes ) {
240
- List <JavaType > result = new ArrayList <>();
241
- for (Type asmType : asmTypes ) {
242
- result .add (JavaTypeImporter .importAsmType (asmType ));
243
- }
244
- return result ;
245
- }
246
-
247
236
private List <JavaType > typesFrom (String [] throwsDeclarations ) {
248
237
List <JavaType > result = new ArrayList <>();
249
238
if (throwsDeclarations != null ) {
@@ -289,10 +278,10 @@ public void visitEnd() {
289
278
LOG .trace ("Done analyzing {}" , className );
290
279
}
291
280
292
- private static List <String > namesOf (Type [] types ) {
281
+ private static List <String > namesOf (Iterable < JavaType > types ) {
293
282
ImmutableList .Builder <String > result = ImmutableList .builder ();
294
- for (Type type : types ) {
295
- result .add (JavaTypeImporter . importAsmType ( type ) .getName ());
283
+ for (JavaType type : types ) {
284
+ result .add (type .getName ());
296
285
}
297
286
return result .build ();
298
287
}
@@ -487,7 +476,7 @@ public void visitEnd() {
487
476
}
488
477
489
478
private static DomainBuilders .JavaAnnotationBuilder annotationBuilderFor (String desc ) {
490
- return new DomainBuilders .JavaAnnotationBuilder ().withType (JavaTypeImporter .importAsmType (Type . getType ( desc ) ));
479
+ return new DomainBuilders .JavaAnnotationBuilder ().withType (JavaTypeImporter .importAsmType (desc ));
491
480
}
492
481
493
482
private static class AnnotationProcessor extends AnnotationVisitor {
@@ -575,7 +564,11 @@ private AnnotationArrayProcessor(AnnotationArrayContext annotationArrayContext)
575
564
576
565
@ Override
577
566
public void visit (String name , Object value ) {
578
- setDerivedComponentType (value );
567
+ if (value instanceof Type ) {
568
+ setDerivedComponentType (JavaClass .class );
569
+ } else {
570
+ setDerivedComponentType (value .getClass ());
571
+ }
579
572
values .add (AnnotationTypeConversion .convert (value ));
580
573
}
581
574
@@ -596,16 +589,11 @@ public void visitEnum(String name, final String desc, final String value) {
596
589
values .add (javaEnumBuilder (desc , value ));
597
590
}
598
591
599
- private void setDerivedComponentType (Object value ) {
600
- setDerivedComponentType (value .getClass ());
601
- }
602
-
603
592
// NOTE: If the declared annotation is not imported itself, we can still use this heuristic,
604
593
// to determine additional information about the respective array.
605
594
// (It the annotation is imported itself, we could easily determine this from the respective
606
595
// JavaClass methods)
607
596
private void setDerivedComponentType (Class <?> type ) {
608
- type = AnnotationTypeConversion .convert (type );
609
597
checkState (derivedComponentType == null || derivedComponentType .equals (type ),
610
598
"Found mixed component types while importing array, this is most likely a bug" );
611
599
@@ -668,22 +656,21 @@ private Optional<Class<?>> determineComponentType(ClassesByTypeName importedClas
668
656
.tryGetMethod (annotationArrayContext .getDeclaringAnnotationMemberName ());
669
657
670
658
return method .isPresent () ?
671
- determineComponentTypeFromReturnValue (method ) :
659
+ determineComponentTypeFromReturnValue (method . get () ) :
672
660
Optional .<Class <?>>absent ();
673
661
}
674
662
675
- private Optional <Class <?>> determineComponentTypeFromReturnValue (Optional <JavaMethod > method ) {
676
- String name = method .get ().getRawReturnType ().getName ();
677
- Optional <Class <?>> result = AnnotationTypeConversion .tryConvert (name );
678
- if (result .isPresent ()) {
679
- return Optional .<Class <?>>of (result .get ().getComponentType ());
663
+ private Optional <Class <?>> determineComponentTypeFromReturnValue (JavaMethod method ) {
664
+ if (method .getRawReturnType ().isEquivalentTo (Class [].class )) {
665
+ return Optional .<Class <?>>of (JavaClass .class );
680
666
}
681
- return resolveComponentTypeFrom (name );
667
+
668
+ return resolveComponentTypeFrom (method .getRawReturnType ().getName ());
682
669
}
683
670
684
671
@ MayResolveTypesViaReflection (reason = "Resolving primitives does not really use reflection" )
685
- private Optional <Class <?>> resolveComponentTypeFrom (String name ) {
686
- JavaType type = JavaType .From .name (name );
672
+ private Optional <Class <?>> resolveComponentTypeFrom (String arrayTypeName ) {
673
+ JavaType type = JavaType .From .name (arrayTypeName );
687
674
JavaType componentType = getComponentType (type );
688
675
689
676
if (componentType .isPrimitive ()) {
@@ -725,50 +712,25 @@ private static ValueBuilder javaEnumBuilder(final String desc, final String valu
725
712
public <T extends HasDescription > Optional <Object > build (T owner , ClassesByTypeName importedClasses ) {
726
713
return Optional .<Object >of (
727
714
new DomainBuilders .JavaEnumConstantBuilder ()
728
- .withDeclaringClass (importedClasses .get (Type . getType (desc ).getClassName ()))
715
+ .withDeclaringClass (importedClasses .get (JavaTypeImporter . importAsmType (desc ).getName ()))
729
716
.withName (value )
730
717
.build ());
731
718
}
732
719
};
733
720
}
734
721
735
722
private static class AnnotationTypeConversion {
736
- private static final Map <String , Class <?>> externalTypeToInternalType = ImmutableMap .of (
737
- Type .class .getName (), JavaClass .class ,
738
- Class .class .getName (), JavaClass .class ,
739
- Class [].class .getName (), JavaClass [].class
740
- );
741
- private static final Map <Class <?>, Function <Object , ValueBuilder >> importedValueToInternalValue =
742
- ImmutableMap .<Class <?>, Function <Object , ValueBuilder >>of (
743
- Type .class , new Function <Object , ValueBuilder >() {
744
- @ Override
745
- public ValueBuilder apply (final Object input ) {
746
- return new ValueBuilder () {
747
- @ Override
748
- public <T extends HasDescription > Optional <Object > build (T owner , ClassesByTypeName importedClasses ) {
749
- return Optional .<Object >of (importedClasses .get (((Type ) input ).getClassName ()));
750
- }
751
- };
752
- }
753
- }
754
- );
755
-
756
- static Class <?> convert (Class <?> type ) {
757
- return externalTypeToInternalType .containsKey (type .getName ()) ?
758
- externalTypeToInternalType .get (type .getName ()) :
759
- type ;
760
- }
761
-
762
- static Optional <Class <?>> tryConvert (String typeName ) {
763
- return externalTypeToInternalType .containsKey (typeName ) ?
764
- Optional .<Class <?>>of (externalTypeToInternalType .get (typeName )) :
765
- Optional .<Class <?>>absent ();
766
- }
767
-
768
- static ValueBuilder convert (Object value ) {
769
- return importedValueToInternalValue .containsKey (value .getClass ()) ?
770
- importedValueToInternalValue .get (value .getClass ()).apply (value ) :
771
- ValueBuilder .ofFinished (value );
723
+ static ValueBuilder convert (Object input ) {
724
+ final Object value = JavaTypeImporter .importAsmTypeIfPossible (input );
725
+ if (value instanceof JavaType ) {
726
+ return new ValueBuilder () {
727
+ @ Override
728
+ public <T extends HasDescription > Optional <Object > build (T owner , ClassesByTypeName importedClasses ) {
729
+ return Optional .<Object >of (importedClasses .get (((JavaType ) value ).getName ()));
730
+ }
731
+ };
732
+ }
733
+ return ValueBuilder .ofFinished (value );
772
734
}
773
735
}
774
736
}
0 commit comments