19
19
import java .util .Collections ;
20
20
import java .util .HashMap ;
21
21
import java .util .HashSet ;
22
- import java .util .List ;
23
22
import java .util .Map ;
24
- import java .util .Objects ;
25
23
import java .util .Set ;
26
24
import java .util .stream .Collectors ;
27
25
import javax .annotation .Nullable ;
28
- import org .sonar .plugins .python .api .symbols .AmbiguousSymbol ;
29
- import org .sonar .plugins .python .api .symbols .ClassSymbol ;
30
- import org .sonar .plugins .python .api .symbols .FunctionSymbol ;
31
26
import org .sonar .plugins .python .api .symbols .Symbol ;
32
27
import org .sonar .plugins .python .api .types .InferredType ;
33
28
import org .sonar .python .semantic .AmbiguousSymbolImpl ;
@@ -45,76 +40,6 @@ public class DescriptorUtils {
45
40
46
41
private DescriptorUtils () {}
47
42
48
- public static Descriptor descriptor (Symbol symbol ) {
49
- switch (symbol .kind ()) {
50
- case FUNCTION :
51
- return functionDescriptor (((FunctionSymbol ) symbol ));
52
- case CLASS :
53
- return classDescriptor ((ClassSymbol ) symbol );
54
- case AMBIGUOUS :
55
- return ambiguousDescriptor ((AmbiguousSymbol ) symbol );
56
- default :
57
- return new VariableDescriptor (symbol .name (), symbol .fullyQualifiedName (), symbol .annotatedTypeName ());
58
- }
59
- }
60
-
61
- private static ClassDescriptor classDescriptor (ClassSymbol classSymbol ) {
62
- ClassDescriptor .ClassDescriptorBuilder classDescriptor = new ClassDescriptor .ClassDescriptorBuilder ()
63
- .withName (classSymbol .name ())
64
- .withFullyQualifiedName (classSymbol .fullyQualifiedName ())
65
- .withMembers (classSymbol .declaredMembers ().stream ().map (DescriptorUtils ::descriptor ).collect (Collectors .toSet ()))
66
- .withSuperClasses (classSymbol .superClasses ().stream ().map (Symbol ::fullyQualifiedName ).filter (Objects ::nonNull ).toList ())
67
- .withDefinitionLocation (classSymbol .definitionLocation ())
68
- .withHasMetaClass (((ClassSymbolImpl ) classSymbol ).hasMetaClass ())
69
- .withHasSuperClassWithoutDescriptor (((ClassSymbolImpl ) classSymbol ).hasSuperClassWithoutSymbol () ||
70
- // Setting hasSuperClassWithoutDescriptor if a parent has a null FQN as it would be impossible to retrieve it without one, even if the parent exists.
71
- classSymbol .superClasses ().stream ().anyMatch (s -> s .fullyQualifiedName () == null ))
72
- .withMetaclassFQN (((ClassSymbolImpl ) classSymbol ).metaclassFQN ())
73
- .withHasDecorators (classSymbol .hasDecorators ())
74
- .withSupportsGenerics (((ClassSymbolImpl ) classSymbol ).supportsGenerics ());
75
-
76
- return classDescriptor .build ();
77
- }
78
-
79
- private static FunctionDescriptor functionDescriptor (FunctionSymbol functionSymbol ) {
80
- return new FunctionDescriptor .FunctionDescriptorBuilder ()
81
- .withName (functionSymbol .name ())
82
- .withFullyQualifiedName (functionSymbol .fullyQualifiedName ())
83
- .withParameters (parameters (functionSymbol .parameters ()))
84
- .withHasDecorators (functionSymbol .hasDecorators ())
85
- .withDecorators (functionSymbol .decorators ())
86
- .withIsAsynchronous (functionSymbol .isAsynchronous ())
87
- .withIsInstanceMethod (functionSymbol .isInstanceMethod ())
88
- .withAnnotatedReturnTypeName (functionSymbol .annotatedReturnTypeName ())
89
- .withDefinitionLocation (functionSymbol .definitionLocation ())
90
- .build ();
91
- }
92
-
93
- private static AmbiguousDescriptor ambiguousDescriptor (AmbiguousSymbol ambiguousSymbol ) {
94
- return ambiguousDescriptor (ambiguousSymbol , null );
95
- }
96
-
97
- public static AmbiguousDescriptor ambiguousDescriptor (AmbiguousSymbol ambiguousSymbol , @ Nullable String overriddenFQN ) {
98
- String fullyQualifiedName = overriddenFQN != null ? overriddenFQN : ambiguousSymbol .fullyQualifiedName ();
99
- Set <Descriptor > alternatives = ambiguousSymbol .alternatives ().stream ()
100
- .map (DescriptorUtils ::descriptor )
101
- .collect (Collectors .toSet ());
102
- return new AmbiguousDescriptor (ambiguousSymbol .name (), fullyQualifiedName , alternatives );
103
- }
104
-
105
- private static List <FunctionDescriptor .Parameter > parameters (List <FunctionSymbol .Parameter > parameters ) {
106
- return parameters .stream ().map (parameter -> new FunctionDescriptor .Parameter (
107
- parameter .name (),
108
- ((FunctionSymbolImpl .ParameterImpl ) parameter ).annotatedTypeName (),
109
- parameter .hasDefaultValue (),
110
- parameter .isKeywordOnly (),
111
- parameter .isPositionalOnly (),
112
- parameter .isPositionalVariadic (),
113
- parameter .isKeywordVariadic (),
114
- parameter .location ()
115
- )).toList ();
116
- }
117
-
118
43
// TODO SONARPY-958: Cleanup the symbol construction from descriptors by extracting this logic in a builder class
119
44
public static Symbol symbolFromDescriptor (Descriptor descriptor , ProjectLevelSymbolTable projectLevelSymbolTable ,
120
45
@ Nullable String localSymbolName , Map <Descriptor , Symbol > createdSymbolsByDescriptor , Map <String , Symbol > createdSymbolsByFqn ) {
0 commit comments