@@ -179,6 +179,11 @@ public static String normalizedFqn(String fqn) {
179
179
}
180
180
181
181
public static String normalizedFqn (String fqn , String moduleName , String localName ) {
182
+ return normalizedFqn (fqn , moduleName , localName , null );
183
+ }
184
+
185
+ public static String normalizedFqn (String fqn , String moduleName , String localName , @ Nullable String containerClassFqn ) {
186
+ if (containerClassFqn != null ) return containerClassFqn + "." + localName ;
182
187
if (fqn .startsWith (moduleName )) return normalizedFqn (fqn );
183
188
return moduleName + "." + localName ;
184
189
}
@@ -192,20 +197,20 @@ public static boolean isValidForProjectPythonVersion(List<String> validForPython
192
197
return !intersection .isEmpty ();
193
198
}
194
199
195
- public static Set <Symbol > symbolsFromProtobufDescriptors (Set <Object > protobufDescriptors , boolean isInsideClass , String moduleName ) {
200
+ public static Set <Symbol > symbolsFromProtobufDescriptors (Set <Object > protobufDescriptors , @ Nullable String containerClassFqn , String moduleName ) {
196
201
Set <Symbol > symbols = new HashSet <>();
197
202
for (Object descriptor : protobufDescriptors ) {
198
203
if (descriptor instanceof SymbolsProtos .ClassSymbol ) {
199
204
symbols .add (new ClassSymbolImpl (((SymbolsProtos .ClassSymbol ) descriptor ), moduleName ));
200
205
}
201
206
if (descriptor instanceof SymbolsProtos .FunctionSymbol ) {
202
- symbols .add (new FunctionSymbolImpl (((SymbolsProtos .FunctionSymbol ) descriptor ), isInsideClass , moduleName ));
207
+ symbols .add (new FunctionSymbolImpl (((SymbolsProtos .FunctionSymbol ) descriptor ), containerClassFqn , moduleName ));
203
208
}
204
209
if (descriptor instanceof OverloadedFunctionSymbol ) {
205
210
if (((OverloadedFunctionSymbol ) descriptor ).getDefinitionsList ().size () < 2 ) {
206
211
throw new IllegalStateException ("Overloaded function symbols should have at least two definitions." );
207
212
}
208
- symbols .add (fromOverloadedFunction (((OverloadedFunctionSymbol ) descriptor ), isInsideClass , moduleName ));
213
+ symbols .add (fromOverloadedFunction (((OverloadedFunctionSymbol ) descriptor ), containerClassFqn , moduleName ));
209
214
}
210
215
if (descriptor instanceof SymbolsProtos .VarSymbol ) {
211
216
SymbolsProtos .VarSymbol varSymbol = (SymbolsProtos .VarSymbol ) descriptor ;
@@ -220,6 +225,24 @@ public static Set<Symbol> symbolsFromProtobufDescriptors(Set<Object> protobufDes
220
225
return symbols ;
221
226
}
222
227
228
+
229
+ @ CheckForNull
230
+ public static SymbolsProtos .ClassSymbol classDescriptorWithFQN (String fullyQualifiedName ) {
231
+ String [] fqnSplitByDot = fullyQualifiedName .split ("\\ ." );
232
+ String symbolLocalNameFromFqn = fqnSplitByDot [fqnSplitByDot .length - 1 ];
233
+ String moduleName = Arrays .stream (fqnSplitByDot , 0 , fqnSplitByDot .length - 1 ).collect (Collectors .joining ("." ));
234
+ InputStream resource = TypeShed .class .getResourceAsStream (PROTOBUF + moduleName + ".protobuf" );
235
+ if (resource == null ) return null ;
236
+ ModuleSymbol moduleSymbol = deserializedModule (moduleName , resource );
237
+ if (moduleSymbol == null ) return null ;
238
+ for (SymbolsProtos .ClassSymbol classSymbol : moduleSymbol .getClassesList ()) {
239
+ if (classSymbol .getName ().equals (symbolLocalNameFromFqn )) {
240
+ return classSymbol ;
241
+ }
242
+ }
243
+ return null ;
244
+ }
245
+
223
246
//================================================================================
224
247
// Private methods
225
248
//================================================================================
@@ -260,7 +283,7 @@ private static Map<String, Symbol> searchTypeShedForModule(String moduleName) {
260
283
* This method sort ambiguous symbol by python version and returns the one which is valid for
261
284
* the most recent Python version.
262
285
*/
263
- private static Symbol disambiguateWithLatestPythonSymbol (Set <Symbol > alternatives ) {
286
+ static Symbol disambiguateWithLatestPythonSymbol (Set <Symbol > alternatives ) {
264
287
int max = Integer .MIN_VALUE ;
265
288
Symbol latestPythonSymbol = null ;
266
289
for (Symbol alternative : alternatives ) {
@@ -322,7 +345,7 @@ static Map<String, Symbol> getSymbolsFromProtobufModule(@Nullable ModuleSymbol m
322
345
323
346
for (Map .Entry <String , Set <Object >> entry : descriptorsByName .entrySet ()) {
324
347
String name = entry .getKey ();
325
- Set <Symbol > symbols = symbolsFromProtobufDescriptors (entry .getValue (), false , moduleSymbol .getFullyQualifiedName ());
348
+ Set <Symbol > symbols = symbolsFromProtobufDescriptors (entry .getValue (), null , moduleSymbol .getFullyQualifiedName ());
326
349
Symbol disambiguatedSymbol = disambiguateSymbolsWithSameName (name , symbols , moduleSymbol .getFullyQualifiedName ());
327
350
deserializedSymbols .put (name , disambiguatedSymbol );
328
351
}
@@ -356,9 +379,9 @@ private static boolean haveAllTheSameFqn(Set<Symbol> symbols) {
356
379
return firstFqn != null && symbols .stream ().map (Symbol ::fullyQualifiedName ).allMatch (firstFqn ::equals );
357
380
}
358
381
359
- private static AmbiguousSymbol fromOverloadedFunction (OverloadedFunctionSymbol overloadedFunctionSymbol , boolean isInsideClass , String moduleName ) {
382
+ private static AmbiguousSymbol fromOverloadedFunction (OverloadedFunctionSymbol overloadedFunctionSymbol , @ Nullable String containerClassFqn , String moduleName ) {
360
383
Set <Symbol > overloadedSymbols = overloadedFunctionSymbol .getDefinitionsList ().stream ()
361
- .map (def -> new FunctionSymbolImpl (def , isInsideClass , overloadedFunctionSymbol .getValidForList (), moduleName ))
384
+ .map (def -> new FunctionSymbolImpl (def , containerClassFqn , overloadedFunctionSymbol .getValidForList (), moduleName ))
362
385
.collect (Collectors .toSet ());
363
386
return AmbiguousSymbolImpl .create (overloadedSymbols );
364
387
}
0 commit comments