import java.io.File File nball = new File('/space/src/nb/core-main') for (File module : nball.listFiles().sort()) { if (module.name == 'openide.util' || module.name == 'visualweb.libs.batik') { continue } for (File root : ['src', 'test/unit/src', 'test/qa-functional/src'].collect{new File(module, it)}) { if (root.isDirectory()) { for (File provider : new File(root, 'META-INF/services').listFiles()?.sort()) { fixup(module, root, provider) } } } } void fixup(File module, File root, File provider) { List impls = [] Map> impl2Masked = [:] Map impl2Position = [:] String currImpl for (String line : provider.readLines()) { if (line =~ /^#-/) { masked = line.replaceFirst(/^#-/, '') impl2Masked[currImpl] = impl2Masked[currImpl] ?: [] impl2Masked[currImpl] += masked } else if (line =~ /^#position=/) { position = line.replaceFirst(/^#position=/, '') impl2Position[currImpl] = position } else if (line =~ /^#/) { println "$provider: COMMENT: $line" } else if (line =~ /^\S/) { impl = line currImpl = impl impls += impl } } if (currImpl) { if (impl2Masked.containsKey(null)) { impl2Masked[currImpl] = impl2Masked.remove(null) } boolean ok = true impls.each { ok &= register(module, provider, root, it, provider.name, impl2Position[it], impl2Masked[it]) } if (ok) { provider.delete() } else { println "$provider: not removing" } } else if (impl2Masked.size() > 0 || impl2Position.size() > 0 || impl2Position.containsKey(null)) { println "$provider: WARNING: inconvertible registrations; impl2Masked=$impl2Masked; impl2Position=$impl2Position" } else { println "$provider: WARNING: no registrations" provider.delete() } } boolean register(File module, File provider, File root, String impl, String xface, String position, List masked) { File src = new File(root, impl.replaceFirst(/\$.+/, '').replace('.', '/') + '.java') if (!src.file) { println "$provider: WARNING: no such file $src" return false } String txt = src.text // XXX trying to create import statements proved to be too unreliable String ann = "@org.openide.util.lookup.ServiceProvider(service=${xface.replace('$', '.')}.class" if (position != null) { ann += ", position=$position" } if (masked != null) { ann += ", supersedes=" if (masked.size() == 1) { ann += "\"${masked[0]}\"" } else { ann =+ '{' + masked.collect{"\"$it\""}.join(', ') + '}' } } ann += ')' String implshort = impl.replaceFirst(/.+[.$]/, '') java.util.regex.Matcher m = txt =~ /(?m)(^[ \t]*)(?:(?:public|static|final)\s+)*class\s+$implshort\s+/ if (!m.find()) { println "$src: WARNING: could not find definition of $implshort" return false } txt = txt.substring(0, m.start()) + m.group(1) + ann + '\n' + txt.substring(m.start()) txt = txt.replaceAll(/(@org\.openide\.util\.lookup\.ServiceProvider\([^)]+\))\s+(@org\.openide\.util\.lookup\.ServiceProvider\([^)]+\))/, '@org.openide.util.lookup.ServiceProviders({$1, $2})') txt = txt.replaceAll(/(@org\.openide\.util\.lookup\.ServiceProviders\(\{[^\n]+)\}\)\s+(@org\.openide\.util\.lookup\.ServiceProvider\([^)]+\))/, '$1, $2})') src.write(txt) File projectXml = new File(module, 'nbproject/project.xml') txt = projectXml.text if (!txt.contains('org.openide.util')) { projectXml.write(txt.replaceFirst('', ''' org.openide.util ''')) } File projectProps = new File(module, 'nbproject/project.properties') txt = projectProps.text if (!txt.contains('javac.source=1.5')) { projectProps.write(txt + '\njavac.source=1.5') } return true }