Fixing bugs

#819518 : AST writes out method return types incorrectly
#820241 : VariableDeclaration doesn't show variable modifiers


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2354 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2003-10-30 23:36:29 +00:00
parent 703c59215b
commit 9c5b0f1987

View File

@ -1,29 +1,30 @@
/*
* <copyright>
* Copyright 1997-2003 PMD for Eclipse Development team
* under sponsorship of the Defense Advanced Research Projects
* Agency (DARPA).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Cougaar Open Source License as published by
* DARPA on the Cougaar Open Source Website (www.cougaar.org).
*
* THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
* PROVIDED "AS IS" WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
* IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
* ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
* TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THE COUGAAR SOFTWARE.
*
/*
* <copyright> Copyright 1997-2003 PMD for Eclipse Development team under
* sponsorship of the Defense Advanced Research Projects Agency (DARPA).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the Cougaar Open Source License as published by DARPA on
* the Cougaar Open Source Website (www.cougaar.org).
*
* THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS PROVIDED "AS
* IS" WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING
* (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE, AND WITHOUT ANY WARRANTIES AS TO NON-INFRINGEMENT.
* IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF
* DATA OR PROFITS, TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THE COUGAAR SOFTWARE.
*
* </copyright>
*/
*/
package net.sourceforge.pmd.eclipse;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -31,9 +32,11 @@ import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xml.serialize.DOMSerializer;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
@ -48,15 +51,21 @@ import org.w3c.dom.Element;
* @version $Revision$
*
* $Log$
* Revision 1.1 2003/10/27 20:14:13 phherlin
* Revision 1.2 2003/10/30 23:36:29 phherlin
* Fixing bugs
* #819518 : AST writes out method return types incorrectly
* #820241 : VariableDeclaration doesn't show variable modifiers
* Revision 1.1 2003/10/27 20:14:13 phherlin
* Refactoring AST generation. Using a ASTWriter.
*
*
*/
public class ASTWriterImpl implements ASTWriter {
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.ASTWriterImpl");
/**
* @see net.sourceforge.pmd.eclipse.ASTWriter#write(java.io.Writer, net.sourceforge.pmd.ast.ASTCompilationUnit)
*/
* @see net.sourceforge.pmd.eclipse.ASTWriter#write(java.io.Writer,
* net.sourceforge.pmd.ast.ASTCompilationUnit)
*/
public void write(Writer writer, ASTCompilationUnit compilationUnit) throws PMDEclipseException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@ -80,46 +89,59 @@ public class ASTWriterImpl implements ASTWriter {
throw new PMDEclipseException(e);
}
}
/**
* Return a default node element
* @param doc the generated document
* @param simpleNode a simple node
* @return a default node element
*/
* Transform a ast node to a xml element
*
* @param doc
* the generated document
* @param simpleNode
* a ast node
* @return a xml element
*/
private Element getElement(Document doc, SimpleNode simpleNode) {
log.debug("creating element " + simpleNode);
Element simpleNodeElement = doc.createElement(simpleNode.toString());
simpleNodeElement.setAttribute("beginColumn", String.valueOf(simpleNode.getBeginColumn()));
simpleNodeElement.setAttribute("beginLine", String.valueOf(simpleNode.getBeginLine()));
simpleNodeElement.setAttribute("endColumn", String.valueOf(simpleNode.getEndColumn()));
simpleNodeElement.setAttribute("endLine", String.valueOf(simpleNode.getEndLine()));
if (simpleNode.getImage() != null) {
simpleNodeElement.setAttribute("image", simpleNode.getImage());
}
addAttributes(simpleNodeElement, simpleNode);
for (int i = 0; i < simpleNode.jjtGetNumChildren(); i++) {
Element element = getElement(doc, (SimpleNode) simpleNode.jjtGetChild(i));
Node child = simpleNode.jjtGetChild(i);
Element element = getElement(doc, (SimpleNode) child);
simpleNodeElement.appendChild(element);
}
return simpleNodeElement;
}
/**
* Return a type element
* @param doc the generated document
* @param type a type node
* @return a type element
*/
private Element getElement(Document doc, ASTType type) {
Element typeElement = getElement(doc, (SimpleNode) type);
if (type.isArray()) {
typeElement.setAttribute("isArray", "true");
typeElement.setAttribute("dimensions", String.valueOf(type.getDimensions()));
* Add attributes to element by introspecting the node. This way, the
* abstract tree can evolve indepently from the way it is persisted
*
* @param element
* a xml element
* @param simpleNode
* a ast node
*/
private void addAttributes(Element element, SimpleNode simpleNode) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(simpleNode.getClass());
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < descriptors.length; i++) {
String attributeName = descriptors[i].getName();
if (!attributeName.equals("class") && !attributeName.equals("scope")) {
log.debug(" processing attribute " + descriptors[i].getName());
Method getter = descriptors[i].getReadMethod();
Object result = getter.invoke(simpleNode, null);
if (result != null) {
log.debug(" added");
element.setAttribute(descriptors[i].getName(), result.toString());
}
}
}
} catch (Exception e) {
log.debug("Error introspecting properties. Ignored", e);
}
return typeElement;
}
}