private int _getJavaClassLineNumber(JavaClass javaClass) { int lineNumber = javaClass.getLineNumber(); Annotation[] annotations = javaClass.getAnnotations(); if (annotations.length == 0) { return lineNumber; } for (Annotation annotation : annotations) { int annotationLineNumber = annotation.getLineNumber(); Map<String, String> propertyMap = annotation.getPropertyMap(); if (propertyMap.isEmpty()) { annotationLineNumber--; } if (annotationLineNumber < lineNumber) { lineNumber = annotationLineNumber; } } return lineNumber; }
private void _updateJavaFromJavadoc(String fileName, String oldContent) throws Exception { File javadocFile = new File(_basedir + fileName + "doc"); if (!javadocFile.exists()) { return; } File file = new File(_basedir + fileName); if (oldContent == null) { oldContent = _fileUtil.read(file); } String[] lines = StringUtil.splitLines(oldContent); JavaClass javaClass = _getJavaClass(fileName, new UnsyncStringReader(oldContent)); Document document = _saxReaderUtil.read(javadocFile); Element rootElement = document.getRootElement(); Map<Integer, String> commentsMap = new TreeMap<Integer, String>(); commentsMap.put(javaClass.getLineNumber(), _getJavaClassComment(rootElement, javaClass)); Map<String, Element> methodElementsMap = new HashMap<String, Element>(); List<Element> methodElements = rootElement.elements("method"); for (Element methodElement : methodElements) { String methodKey = _getMethodKey(methodElement); methodElementsMap.put(methodKey, methodElement); } JavaMethod[] javaMethods = javaClass.getMethods(); for (JavaMethod javaMethod : javaMethods) { if (commentsMap.containsKey(javaMethod.getLineNumber())) { continue; } commentsMap.put( javaMethod.getLineNumber(), _getJavaMethodComment(lines, methodElementsMap, javaMethod)); } Map<String, Element> fieldElementsMap = new HashMap<String, Element>(); List<Element> fieldElements = rootElement.elements("field"); for (Element fieldElement : fieldElements) { String fieldKey = _getFieldKey(fieldElement); fieldElementsMap.put(fieldKey, fieldElement); } JavaField[] javaFields = javaClass.getFields(); for (JavaField javaField : javaFields) { if (commentsMap.containsKey(javaField.getLineNumber())) { continue; } commentsMap.put( javaField.getLineNumber(), _getJavaFieldComment(lines, fieldElementsMap, javaField)); } StringBuilder sb = new StringBuilder(oldContent.length()); for (int lineNumber = 1; lineNumber <= lines.length; lineNumber++) { String line = lines[lineNumber - 1]; String comments = commentsMap.get(lineNumber); if (comments != null) { sb.append(comments); } sb.append(line); sb.append("\n"); } String newContent = sb.toString().trim(); if ((oldContent == null) || !oldContent.equals(newContent)) { _fileUtil.write(file, newContent); System.out.println("Writing " + file); } }
private String _removeJavadocFromJava(String fileName, boolean log) throws Exception { File file = new File(_basedir + fileName); String oldContent = _fileUtil.read(file); String[] lines = StringUtil.splitLines(oldContent); JavaClass javaClass = _getJavaClass(fileName, new UnsyncStringReader(oldContent)); Set<Integer> lineNumbers = new HashSet<Integer>(); lineNumbers.add(javaClass.getLineNumber()); JavaMethod[] javaMethods = javaClass.getMethods(); for (JavaMethod javaMethod : javaMethods) { lineNumbers.add(javaMethod.getLineNumber()); } JavaField[] javaFields = javaClass.getFields(); for (JavaField javaField : javaFields) { lineNumbers.add(javaField.getLineNumber()); } for (int lineNumber : lineNumbers) { int pos = lineNumber - 2; String line = lines[pos].trim(); if (line.endsWith("*/")) { while (true) { lines[pos] = null; if (line.startsWith("/**")) { break; } line = lines[--pos].trim(); } } } StringBuilder sb = new StringBuilder(oldContent.length()); for (String line : lines) { if (line != null) { sb.append(line); sb.append("\n"); } } String newContent = sb.toString().trim(); if ((oldContent == null) || !oldContent.equals(newContent)) { _fileUtil.write(file, newContent); if (log) { System.out.println("Writing " + file); } } return newContent; }