private void doOCD(ObjectClassDefinition o, Annotation annotation) { if (topLevel) { if (clazz.isInterface()) { if (ocd == null) ocd = new OCDDef(finder); ocd.id = o.id() == null ? name.getFQN() : o.id(); ocd.name = o.name() == null ? space(ocd.id) : o.name(); ocd.description = o.description() == null ? "" : o.description(); ocd.localization = o.localization() == null ? "OSGI-INF/l10n/" + name.getFQN() : o.localization(); if (annotation.get("pid") != null) { String[] pids = o.pid(); designates(pids, false); } if (annotation.get("factoryPid") != null) { String[] pids = o.factoryPid(); designates(pids, true); } if (annotation.get("icon") != null) { Icon[] icons = o.icon(); for (Icon icon : icons) { ocd.icons.add(new IconDef(icon.resource(), icon.size())); } } } else { analyzer.error( "ObjectClassDefinition applied to non-interface, non-annotation class %s", clazz); } } }
private void parseExtends(Clazz clazz) { TypeRef[] inherits = clazz.getInterfaces(); if (inherits != null) { if (analyzed == null) { analyzed = new HashSet<TypeRef>(); } for (TypeRef typeRef : inherits) { if (!typeRef.isJava() && analyzed.add(typeRef)) { try { Clazz inherit = analyzer.findClass(typeRef); if (inherit != null) { inherit.parseClassFileWithCollector(this); parseExtends(inherit); } else { analyzer.error( "Could not obtain super class %s of class %s", typeRef.getFQN(), clazz.getClassName().getFQN()); } } catch (Exception e) { analyzer.error( "Could not obtain super class %s of class %s; exception %s", typeRef.getFQN(), clazz.getClassName().getFQN(), e.getMessage()); } } } } }
private boolean identifiableCollection(String type, boolean intface, boolean topLevel) { try { Clazz clazz = analyzer.findClass(analyzer.getTypeRefFromFQN(type)); if (clazz != null && (!topLevel || !clazz.isAbstract()) && ((intface && clazz.isInterface()) ^ clazz.hasPublicNoArgsConstructor())) { TypeRef[] intfs = clazz.getInterfaces(); if (intfs != null) { for (TypeRef intf : intfs) { if (COLLECTION.matcher(intf.getFQN()).matches() || identifiableCollection(intf.getFQN(), true, false)) { return true; } } } TypeRef ext = clazz.getSuper(); return ext != null && identifiableCollection(ext.getFQN(), false, false); } } catch (Exception e) { return false; } return false; }
/** * Called to prepare. If will look for any errors or inconsistencies in the setup. * * @param analyzer the analyzer to report errors and create references * @throws Exception */ void prepare(Analyzer analyzer) throws Exception { prepareVersion(analyzer); if (implementation == null) { analyzer.error("No Implementation defined for component " + name); return; } analyzer.referTo(implementation); if (name == null) name = implementation.getFQN(); if (service != null && service.length > 0) { for (TypeRef interfaceName : service) analyzer.referTo(interfaceName); } else if (scope != null && scope != ServiceScope.BUNDLE) analyzer.warning( "The servicefactory:=true directive is set but no service is provided, ignoring it"); for (Map.Entry<String, List<String>> kvs : property.entrySet()) { Tag property = new Tag("property"); String name = kvs.getKey(); String type = propertyType.get(name); property.addAttribute("name", name); if (type != null) { property.addAttribute("type", type); } if (kvs.getValue().size() == 1) { String value = kvs.getValue().get(0); value = check(type, value, analyzer); property.addAttribute("value", value); } else { StringBuilder sb = new StringBuilder(); String del = ""; for (String v : kvs.getValue()) { if (v == MARKER) { continue; } sb.append(del); v = check(type, v, analyzer); sb.append(v); del = "\n"; } property.addContent(sb.toString()); } propertyTags.add(property); } }
private void doMethods() throws Exception { for (Map.Entry<MethodDef, ADDef> entry : methods.entrySet()) { MethodDef defined = entry.getKey(); if (defined.isConstructor()) { analyzer.error( "Constructor %s for %s.%s found; only interfaces and annotations allowed for OCDs", defined.getName(), clazz.getClassName().getFQN(), defined.getName()); } if (defined.getPrototype().length > 0) { analyzer.error( "Element %s for %s.%s has parameters; only no-parameter elements in an OCD interface allowed", defined.getName(), clazz.getClassName().getFQN(), defined.getName()); continue; } ADDef ad = entry.getValue(); ocd.attributes.add(ad); ad.id = fixup(defined.getName()); ad.name = space(defined.getName()); String rtype = defined.getGenericReturnType(); if (rtype.endsWith("[]")) { ad.cardinality = Integer.MAX_VALUE; rtype = rtype.substring(0, rtype.length() - 2); } Matcher m = GENERIC.matcher(rtype); if (m.matches()) { boolean knownCollection = m.group(2) != null; boolean collection = knownCollection || identifiableCollection(m.group(3), false, true); if (collection) { if (ad.cardinality != 0) analyzer.error( "AD for %s.%s uses an array of collections in return type (%s), Metatype allows either Vector or array", clazz.getClassName().getFQN(), defined.getName(), defined.getType().getFQN()); rtype = Clazz.objectDescriptorToFQN(m.group(4)); ad.cardinality = Integer.MIN_VALUE; } } if (rtype.indexOf('<') > 0) { rtype = rtype.substring(0, rtype.indexOf('<')); } ad.type = getType(rtype); ad.required = true; TypeRef typeRef = analyzer.getTypeRefFromFQN(rtype); try { Clazz c = analyzer.findClass(typeRef); if (c != null && c.isEnum()) { parseOptionValues(c, ad.options); } } catch (Exception e) { analyzer.error( "AD for %s.%s Can not parse option values from type (%s), %s", clazz.getClassName().getFQN(), defined.getName(), defined.getType().getFQN(), e.getMessage()); } if (ad.ad != null) { doAD(ad); } if (ad.defaults == null && clazz.isAnnotation() && defined.getConstant() != null) { // defaults from annotation default Object value = defined.getConstant(); boolean isClass = false; TypeRef type = defined.getType().getClassRef(); if (!type.isPrimitive()) { if (Class.class.getName().equals(type.getFQN())) { isClass = true; } else { try { Clazz r = analyzer.findClass(type); if (r.isAnnotation()) { analyzer.warning( "Nested annotation type found in field % s, %s", defined.getName(), type.getFQN()); return; } } catch (Exception e) { analyzer.error( "Exception looking at annotation type default for element with descriptor %s, type %s", e, defined, type); } } } if (value != null) { if (value.getClass().isArray()) { // add element individually ad.defaults = new String[Array.getLength(value)]; for (int i = 0; i < Array.getLength(value); i++) { Object element = Array.get(value, i); ad.defaults[i] = valueToProperty(element, isClass); } } else { ad.defaults = new String[] {valueToProperty(value, isClass)}; } } } } }
String effectiveName() { if (name != null) return name; if (implementation != null) return implementation.getFQN(); return "<name not yet determined>"; }
/** * Returns a tag describing the component element. * * @return a component element */ Tag getTag() { String xmlns = this.xmlns; if (xmlns == null && version != AnnotationReader.V1_0) xmlns = NAMESPACE_STEM + "/v" + version; Tag component = new Tag(xmlns == null ? "component" : "scr:component"); Namespaces namespaces = null; if (xmlns != null) { namespaces = new Namespaces(); namespaces.registerNamespace("scr", xmlns); addNamespaces(namespaces, xmlns); for (ReferenceDef ref : references.values()) ref.addNamespaces(namespaces, xmlns); namespaces.addNamespaces(component); } component.addAttribute("name", name); if (configurationPolicy != null) component.addAttribute("configuration-policy", configurationPolicy.toString().toLowerCase()); if (enabled != null) component.addAttribute("enabled", enabled); if (immediate != null) component.addAttribute("immediate", immediate); if (factory != null) component.addAttribute("factory", factory); if (activate != null && version != AnnotationReader.V1_0) component.addAttribute("activate", activate); if (deactivate != null && version != AnnotationReader.V1_0) component.addAttribute("deactivate", deactivate); if (modified != null) component.addAttribute("modified", modified); if (configurationPid != null) { StringBuilder b = new StringBuilder(); String space = ""; for (String pid : configurationPid) { if ("$".equals(pid)) pid = name; b.append(space).append(pid); space = " "; } component.addAttribute("configuration-pid", b.toString()); } addAttributes(component, namespaces); Tag impl = new Tag(component, "implementation"); impl.addAttribute("class", implementation.getFQN()); if (service != null && service.length != 0) { Tag s = new Tag(component, "service"); if (scope != null) { // TODO check for DEFAULT??? if (AnnotationReader.V1_3.compareTo(version) > 0) { if (scope == ServiceScope.PROTOTYPE) { throw new IllegalStateException( "verification failed, pre 1.3 component with scope PROTOTYPE"); } s.addAttribute("servicefactory", scope == ServiceScope.BUNDLE); } else { s.addAttribute("scope", scope.toString().toLowerCase()); } } for (TypeRef ss : service) { Tag provide = new Tag(s, "provide"); provide.addAttribute("interface", ss.getFQN()); } } for (ReferenceDef ref : references.values()) { Tag refTag = ref.getTag(namespaces); component.addContent(refTag); } for (Tag tag : propertyTags) component.addContent(tag); for (String entry : properties) { Tag properties = new Tag(component, "properties"); properties.addAttribute("entry", entry); } return component; }