kwin commented on code in PR #120:
URL: https://github.com/apache/sling-whiteboard/pull/120#discussion_r3212974513
##########
skills/osgi-scr-migrator/scripts/migrate_annotations.py:
##########
@@ -746,9 +749,121 @@ def _find_service_class_for_component(self,
component_idx: int) -> Optional[str]
service_match =
re.search(r'@Service\s*\(\s*(?:value\s*=\s*)?([^)]+)\)', line)
if service_match:
return service_match.group(1).strip()
- # If no explicit class, return empty to signal service
registration
- return ""
+ # If @Service is present but no class specified, we will infer
from interfaces
+ else:
+ return
self._get_transitive_interfaces_from_class(component_idx)
+
+ # No @Service annotation found don't auto register as service
return None
+
+ def _get_transitive_interfaces_from_class(self, component_idx: int) ->
Optional[str]:
+ """Extract all transitive interfaces from the class declaration.
+
+ Looks for the class declaration after the @Component annotation and
extracts
+ all interfaces it implements. Also resolves transitive interfaces
(interfaces
+ that the declared interfaces extend).
+
+ Returns:
+ - A single interface name if exactly one interface is found
+ - A list of interfaces formatted as "{ Interface1.class,
Interface2.class, ... }"
+ - Empty string "" if interfaces are found but unable to resolve
+ - None if no class declaration or interfaces are found
+ """
+ # Find the class declaration after @Component
+ class_line = None
+ for i in range(component_idx + 1, min(component_idx + 10,
len(self.lines))):
+ line = self.lines[i]
+ if
re.search(r'(public|private|protected)?\s*(abstract\s+)?class\s+\w+', line):
+ class_line = i
+ break
+
+ if class_line is None:
+ return None
+
+ # Extract the class declaration line(s) - may span multiple lines
+ class_decl = self.lines[class_line]
+ paren_count = class_decl.count('(') - class_decl.count(')')
+ j = class_line + 1
+
+ # Collect until we find opening brace or complete the declaration
+ while j < len(self.lines) and paren_count >= 0 and '{' not in
class_decl:
+ class_decl += ' ' + self.lines[j].strip()
+ paren_count += self.lines[j].count('(') - self.lines[j].count(')')
+ j += 1
+
+ # Extract interfaces from "implements Interface1, Interface2, ..."
Review Comment:
Also needs a broader search for the base class/interface declaration
(without necessarily evaluating the full Maven classpath....). Maybe some
commons base classes can be added here like `SlingSafeMethodsServlet` or
`javax.servlet.GenericServlet` with their implemented interfaces.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]