📄 bundleinfotask.java
字号:
if(!"".equals(importsProperty)) { proj.setNewProperty(importsProperty, toString(importSet, ",")); } if(!"".equals(exportsProperty)) { proj.setNewProperty(exportsProperty, toString(exportSet, ",")); } // Try to be a bit clever when writing back bundle activator if(!"".equals(activatorProperty)) { switch(activatorSet.size()) { case 0: System.out.println("info - no class implementing " + "BundleActivator found"); break; case 1: { String clazz = (String)activatorSet.iterator().next(); String clazz0 = proj.getProperty(activatorProperty); if(clazz0 == null || "".equals(clazz0)) { // System.out.println("set activator " + activatorProperty + "=" + clazz); } else { if(!clazz.equals(clazz0)) { System.out.println("*** Warning - the class found implementing " + " BundleActivator '" + clazz + "' " + " does not match the one set as " + activatorProperty + "=" + clazz0); } else { if(bDebug) { System.out.println("correct activator " + activatorProperty + "=" + clazz0); } } } proj.setNewProperty(activatorProperty, clazz); } break; default: System.out.println("*** Warning - more than one class " + "implementing BundleActivator found:"); for(Iterator it = activatorSet.iterator(); it.hasNext();) { System.out.println(" " + it.next()); } break; } } Set foundationMissing = new TreeSet(); Set minimumMissing = new TreeSet(); Set smfMissing = new TreeSet(); for(Iterator it = classSet.iterator(); it.hasNext();) { String s = (String)it.next(); if(s.endsWith("[]")) { } else { if(!ownClasses.contains(s)) { if(bPrintClasses) { System.out.println(s); } if(!EE.isFoundation(s)) { if(!isImported(s)) { foundationMissing.add(s); } } if(!EE.isMinimum(s)) { if(!isImported(s)) { minimumMissing.add(s); } } if(!EE.isSMF(s)) { if(!isImported(s)) { smfMissing.add(s); } } } } } if(bCheckFoundationEE) { if(foundationMissing.size() > 0) { System.out.println("Missing " + foundationMissing.size() + " classes from foundation profile"); } else { System.out.println("Passes foundation EE"); } for(Iterator it = foundationMissing.iterator(); it.hasNext();) { String s = (String)it.next(); System.out.println("Not in foundation: " + s); } } if(bCheckMinimumEE) { if(minimumMissing.size() > 0) { System.out.println("Missing " + minimumMissing.size() + " classes from minimum profile"); } else { System.out.println("Passes minimum EE"); } for(Iterator it = minimumMissing.iterator(); it.hasNext();) { String s = (String)it.next(); System.out.println("Not in minimum: " + s); } } if(bCheckSMFEE) { if(smfMissing.size() > 0) { System.out.println("Missing " + smfMissing.size() + " classes from SMF profile"); } else { System.out.println("Passes SMF EE"); } for(Iterator it = smfMissing.iterator(); it.hasNext();) { String s = (String)it.next(); System.out.println("Not in SMF: " + s); } } } /** * Analyze a file by checking its suffix and delegate to * <tt>analyzeClass</tt>, <tt>analyzeJava</tt> etc */ protected void analyze(File file) throws BuildException { if(file.getName().endsWith(".class")) { analyzeClass(file); } else if(file.getName().endsWith(".java")) { analyzeJava(file); } else { // Just ignore all other files } } protected void addExportedPackageString(String name) { if(name == null || "".equals(name)) { return; } if(bDebug) { System.out.println(" package " + name); } exportSet.add(name); } protected void addActivatorString(String s) { activatorSet.add(s); } /** * Add a type's package name to the list of imported packages. * * @param t Type of an object. */ protected void addImportedType(Type t) { if(t instanceof BasicType) { // Ignore all basic types } else { addImportedString(t.toString()); } } /** * Add a class' package name to the list of imported packages. * * @param className Class name of an object. The class name is stripped * from the part after the last '.' and added to set * of imported packages, if its not one of the standard * packages. Primitive class names are ignore. */ protected void addImportedString(String className) { if(className == null) { return; } String name = packageName(className); if("".equals(name)) { return; } // only add packages defined outside this set of files if(!exportSet.contains(name)) { // ...and only add non-std packages if(!isStdImport(name)) { importSet.add(name); } } classSet.add(className); } boolean isImported(String className) { for(Iterator it = importSet.iterator(); it.hasNext(); ) { String pkg = (String)it.next(); if(className.startsWith(pkg)) { String rest = className.substring(pkg.length() + 1); if(-1 == rest.indexOf(".")) { return true; } } } return false; } /** * Check if package is included in prefix list of standard packages. * * @return <tt>true</tt> if name is prefixed with any of the elements in * <tt>stdImports</tt> set, <tt>false</tt> otherwise. */ protected boolean isStdImport(String name) { for(Iterator it = stdImports.iterator(); it.hasNext();) { String s = (String)it.next(); if(name.startsWith(s)) { return true; } } return false; } protected void analyzeJar(File file) throws BuildException { throw new BuildException("Jar file analyzing not yet supported"); } protected void analyzeClass(File file) throws BuildException { if(bDebug) { System.out.println("Analyze class file " + file.getAbsolutePath()); } try { ClassParser parser = new ClassParser(file.getAbsolutePath()); JavaClass clazz = parser.parse(); ConstantPool constant_pool = clazz.getConstantPool(); ownClasses.add(clazz.getClassName()); addExportedPackageString(clazz.getPackageName()); // Scan all implemented interfaces to find // candidates for the activator AND to find // all referenced packages. String[] interfaces = clazz.getInterfaceNames(); for(int i = 0; i < interfaces.length; i++) { if("org.osgi.framework.BundleActivator".equals(interfaces[i])) { addActivatorString(clazz.getClassName()); break; } } Constant[] constants = constant_pool.getConstantPool(); for (int i = 0; i < constants.length; i++) { Constant constant = constants[i]; if (constant instanceof ConstantClass) { ConstantClass constantClass = (ConstantClass) constant; String referencedClass = constantClass.getBytes(constant_pool); if (referencedClass.charAt(0) == '[') { referencedClass = Utility.signatureToString(referencedClass, false); } else { referencedClass = Utility.compactClassName(referencedClass, false); } addImportedString(referencedClass); } } } catch (Exception e) { e.printStackTrace(); throw new BuildException("Failed to parse .class file " + file + ", exception=" + e); } } /** * Analyze java source file by reading line by line and looking * for keywords such as "import", "package". * * <p> * <b>Note</b>: This code does not attempt to find any class implementing * <tt>BundleActivator</tt> */ protected void analyzeJava(File file) throws BuildException { if(bDebug) { System.out.println("Analyze java file " + file.getAbsolutePath()); } BufferedReader reader = null; try { String line; int lineNo = 0; reader = new BufferedReader(new FileReader(file)); while(null != (line = reader.readLine())) { lineNo++; line = line.replace(';', ' ').replace('\t', ' ').trim(); if(line.startsWith("package")) { Vector v = StringUtils.split(line, ' '); if(v.size() > 1 && "package".equals(v.elementAt(0))) { String name = (String)v.elementAt(1); addExportedPackageString(name); } } if(line.startsWith("import")) { Vector v = StringUtils.split(line, ' '); if(v.size() > 1 && "import".equals(v.elementAt(0))) { String name = (String)v.elementAt(1); addImportedString(name); } } } } catch (Exception e) { throw new BuildException("Failed to scan " + file + ", err=" + e); } finally { if(reader != null) { try { reader.close(); } catch (Exception ignored) { } } } } /** * Get package name of class string representation. */ static String packageName(String s) { s = s.trim(); int ix = s.lastIndexOf('.'); if(ix != -1) { s = s.substring(0, ix); } else { s = ""; } return s; } /** * Convert Set elements to a string. * * @param separator String to use as sperator between elements. */ static protected String toString(Set set, String separator) { StringBuffer sb = new StringBuffer(); for(Iterator it = set.iterator(); it.hasNext(); ) { String name = (String)it.next(); sb.append(name); if(it.hasNext()) { sb.append(separator); } } return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -