I need to convert a Tree to a JavaTree. But i am getting ClassCastException, obviously.
public Javadoc(Tree tree) {
if (tree.is(METHOD_KINDS)) {
elementParameters = ((MethodTree) tree).parameters().stream().map(VariableTree::simpleName)
.map(IdentifierTree::name).collect(Collectors.toList());
elementExceptionNames = ((MethodTree) tree).throwsClauses().stream().map(Javadoc::exceptionName)
.filter(Objects::nonNull).collect(Collectors.toList());
} else if (tree.is(CLASS_KINDS)) {
elementParameters = ((ClassTree) tree).typeParameters().stream().map(TypeParameterTree::identifier)
.map(IdentifierTree::name).map(name -> "<" + name + ">").collect(Collectors.toList());
elementExceptionNames = Collections.emptyList();
} else {
elementParameters = Collections.emptyList();
elementExceptionNames = Collections.emptyList();
}
List<String> javadocLines = cleanLines(PublicApiChecker.getApiJavadoc((JavaTree)tree));
mainDescription = getDescription(javadocLines, -1, "");
blockTagDescriptions = extractBlockTags(javadocLines, Arrays.asList(BlockTag.values()));
undocumentedNamedTags = new EnumMap<>(BlockTag.class);
}
It happens in jadocLines variable. I have just a Tree, but i need to set a JavaTree for that getApiJavadoc, because of some methods and classes that it calls forward. I tried to create a javatree object and give my tree data to it. But i couldn't, 'cause JavaTree class is abstract. JavaTree class have other classes inside, but i am not getting this. Can anyone helps me in that part?
Edit 1 : Thanks for answers, but i cannot create another class as simples as that, because i need to give some arguments to the class i need that i do not have(because it dont even exist). The class i need to instantiate is:
public static class ImportTreeImpl extends JavaTree implements ImportTree {
private final boolean isStatic;
private final Tree qualifiedIdentifier;
private final SyntaxToken semicolonToken;
private final SyntaxToken importToken;
private final SyntaxToken staticToken;
public ImportTreeImpl(InternalSyntaxToken importToken, #Nullable InternalSyntaxToken staticToken,
Tree qualifiedIdentifier, InternalSyntaxToken semiColonToken) {
super(Kind.IMPORT);
this.importToken = importToken;
this.staticToken = staticToken;
this.qualifiedIdentifier = qualifiedIdentifier;
this.semicolonToken = semiColonToken;
isStatic = staticToken != null;
}
This class is inside JavaTree class and i need to instatiate it, but i dont have any SyntaxToken, and I cant create a null one to put here just for instantiate it. Can you help with this part?
OBS1: That JavaTree cast is wrong, i know, that was just a test. But Thank you!
EDIT2:I know that getApiChecker need a tree, but the problem is not the getApiChecker, but some methods that are called after. Look:
List<String> javadocLines = cleanLines(PublicApiChecker.getApiJavadoc(tree));
Calls :
#Nullable
public static String getApiJavadoc(Tree tree) {
if (!tree.is(API_KINDS)) {
return null;
}
ModifiersTree modifiersTree = getModifierTrees(tree);
// FIXME token should be retrieved in a much simpler way.
if (modifiersTree != null && !(modifiersTree.modifiers().isEmpty() && modifiersTree.annotations().isEmpty())) {
return getCommentFromTree(modifiersTree);
}
if (tree.is(Tree.Kind.METHOD)) {
MethodTree methodTree = (MethodTree) tree;
return getCommentFromMethod(methodTree);
}
return getCommentFromTree(tree);
}
That calls :
private static String getCommentFromTree(Tree tokenTree) {
return getCommentFromSyntaxToken(FirstSyntaxTokenFinder.firstSyntaxToken(tokenTree));
}
That calls:
#Nullable
public static SyntaxToken firstSyntaxToken(#Nullable Tree tree) {
if (tree == null || tree.is(Tree.Kind.INFERED_TYPE)) {
return null;
} else if (tree.is(Tree.Kind.TOKEN)) {
return (SyntaxToken) tree;
}
for (Tree next : ((JavaTree) tree).children()) {
SyntaxToken syntaxToken = firstSyntaxToken(next);
if (syntaxToken != null) {
return syntaxToken;
}
}
return null;
}
And my problem is :
for (Tree next : ((JavaTree) tree).children()) {
Because this is trying to make that cast, that bugs everything. And i can't change this last one, because this is a sonar code, and i can't change it.
For a project, which generates XML-files based on a XSD-file, I want to automatically generate the documentation. *
In this documentation I list the different elements defined in the XSD.
And for each element I want to show an example of that element.
The problem is, that the XML-example might be quite long and contains a lot of children.
Therefore I want to shorten the example by:
limiting the shown depth
limiting the amount of elements in a list
For the root-element that example might look like the following:
<root>
<elements>
<element>...<element>
<element>...<element>
<element>...<element>
...
</elements>
</root>
My approach:
To generate classes from the XSD and to generate and validate the XML files I use JAXB.
But I could not figure out how to marshal a Non-Root element.
Therefore I am generating my examples with XStream.
To limit the XML-example I am trying to decorate the PrettyPrintWriter, but that seems to be quite cumbersome.
The two decorators can be seen in my answer.
I just did not expect to care about the internals of a library for such a (common?) feature.
Is there an easier way to do this? (I can also use another library than XStream, or none at all.)
*
My approach is influenced by Spring Auto Rest Docs
To limit the shown depth I created the following XStream WriterWrapper. The class can wrap for example a PrettyPrintWriter and ensures that the wrapped writer only receives the nodes above a given depth threshold.
public class RestrictedPrettyPrintWriter extends WriterWrapper {
private final ConverterLookup converterLookup;
private final int maximalDepth;
private int depth;
public RestrictedPrettyPrintWriter(HierarchicalStreamWriter sw, ConverterLookup converterLookup, int maximalDepth) {
super(sw);
this.converterLookup = converterLookup;
this.maximalDepth = maximalDepth;
}
#Override public void startNode(String name, Class clazz) {
Converter converter = this.converterLookup.lookupConverterForType(clazz);
boolean isSimpleType = converter instanceof SingleValueConverter;
_startNode(name, !isSimpleType);
}
#Override public void startNode(String name) {
_startNode(name, false);
}
#Override public void endNode() {
if (_isLessDeepThanMaximalDepth() || _isMaximalDepthReached()) {
super.endNode();
}
depth--;
}
#Override public void addAttribute(String key, String value) {
if (_isLessDeepThanMaximalDepth() || _isMaximalDepthReached()) {
super.addAttribute(key, value);
}
}
#Override public void setValue(String text) {
if (_isLessDeepThanMaximalDepth() || _isMaximalDepthReached()) {
super.setValue(text);
}
}
/**
* #param name name of the new node
* #param isComplexType indicates if the element is complex or contains a single value
*/
private void _startNode(String name, boolean isComplexType) {
depth++;
if (_isLessDeepThanMaximalDepth()) {
super.startNode(name);
} else if (_isMaximalDepthReached()) {
super.startNode(name);
/*
* set the placeholder value now
* setValue() will never be called for complex types
*/
if (isComplexType) {
super.setValue("...");
}
}
}
private boolean _isMaximalDepthReached() {
return depth == maximalDepth;
}
private boolean _isLessDeepThanMaximalDepth() {
return depth < maximalDepth;
}
}
To limit the lists, I tried, in a first attempt, to modify the XStream CollectionConverter. But this approach was not general enough because implicit lists do not use this converter.
Therefore I created another WriterWrapper which counts the consecutive occurrences of elements with the same name.
public class RestrictedCollectionWriter extends WriterWrapper {
private final int maxConsecutiveOccurences;
private int depth;
/** Contains one element per depth.
* More precisely: the current element and its parents.
*/
private Map < Integer, Elements > elements = new HashMap < > ();
public RestrictedCollectionWriter(HierarchicalStreamWriter sw, int maxConsecutiveOccurences) {
super(sw);
this.maxConsecutiveOccurences = maxConsecutiveOccurences;
}
#Override public void startNode(String name, Class clazz) {
_startNode(name);
}
#Override public void startNode(String name) {
_startNode(name);
}
#Override public void endNode() {
if (_isCurrentElementPrintable()) {
super.endNode();
}
depth--;
}
#Override public void addAttribute(String key, String value) {
if (_isCurrentElementPrintable()) {
super.addAttribute(key, value);
}
}
#Override public void setValue(String text) {
if (_isCurrentElementPrintable()) {
super.setValue(text);
}
}
/**
* #param name name of the new node
*/
private void _startNode(String name) {
depth++;
Elements currentElement = this.elements.getOrDefault(depth, new Elements());
this.elements.put(depth, currentElement);
Elements parent = this.elements.get(depth - 1);
boolean parentPrintable = parent == null ? true : parent.isPrintable();
currentElement.setName(name, parentPrintable);
if (currentElement.isPrintable()) {
super.startNode(name);
}
}
private boolean _isCurrentElementPrintable() {
Elements currentElement = this.elements.get(depth);
return currentElement.isPrintable();
}
/**
* Evaluates if an element is printable or not.
* This is based on the concurrent occurences of the element's name
* and if the parent element is printable or not.
*/
private class Elements {
private String name = "";
private int concurrentOccurences = 0;
private boolean parentPrintable;
public void setName(String name, boolean parentPrintable) {
if (this.name.equals(name)) {
concurrentOccurences++;
} else {
concurrentOccurences = 1;
}
this.name = name;
this.parentPrintable = parentPrintable;
}
public boolean isPrintable() {
return parentPrintable && concurrentOccurences <= maxConsecutiveOccurences;
}
}
}
The following listing shows, how the two classes can be used.
XStream xstream = new XStream(new StaxDriver());
StringWriter sw = new StringWriter();
PrettyPrintWriter pw = new PrettyPrintWriter(sw);
RestrictedCollectionWriter cw = new RestrictedCollectionWriter(pw, 3);
xstream.marshal(objectToMarshal, new RestrictedPrettyPrintWriter(cw, xstream.getConverterLookup(), 3));
I realize that there are a few good threads already existing which discuss how to implement a linked list in Java, however I can't seem to find one which does not use a constructor like this,
public LinkList() {
Link = null;
}
or something along these lines. However, I have not done this in my code, and therefore I am having trouble with writing a few of the methods.
I have a class called Link which contains the following code, note without any constructor like the one displayed above:
public class Link {
private Link next = null;
private String value = null;
public Link getNext(){
return next;
}
public void setNext(Link nextLink){
next = nextLink;
}
public String getValue(){
return value;
}
public void setValue(String aValue){
value = aValue;
}
}
and then I have my LinkedList class, which contains the following code:
public class LinkedList {
private Link head = null;
private Link tail = null;
public Link getHead(){
return head;
}
public void setHead(Link aLink){
head = aLink;
}
public Link getTail(){
return tail;
}
public void setTail(Link aLink){
tail = aLink;
}
public boolean isEmpty(){
return(head == null && tail == null);
}
public void addLast(String aString){
Link link = new Link();
link.setValue(aString);
//tail.setNext(link);
if(isEmpty()){
head = link;
}else{
tail.setNext(link);
}
tail = link;
}
public void addFirst(String aString) {
Link link = new Link();
link.setValue(aString);
if(isEmpty()){
tail = link;
}else{
head.setNext(link);
}
head = link;
}
public Link removeFirst(){
Link temp = head;
head = temp.getNext();
temp.setNext(null);
return temp;
}
}
My addFirst Method, and my isEmpty Method both seem to be working but my addLast and removeFirst Methods are not. I have drawn pictures, looked at pseudo code, and searched around the internet, but I just cannot seem to figure this out.
I need to know how to get my addLast and removeFirst methods working without having to add in the constructor at the beginning like all the rest of the example code I keep seeing.
Here are the Tests they need to pass:
#Test
public void testRemoveFirst(){
list.addFirst("three");
list.addFirst("two");
list.addFirst("one");
assertTrue("one".equals(list.removeFirst()));
assertTrue("two".equals(list.removeFirst()));
assertTrue("three".equals(list.removeFirst()));
}
#Test
public void testAddLast(){
list.addFirst("three");
list.addFirst("two");
list.addFirst("one");
assertTrue( "three".equals(list.removeLast()));
assertTrue( "two".equals(list.removeLast()));
assertTrue( "one".equals(list.removeLast()));
assertNull(list.removeLast());
}
One change in your code is needed: you want the new link to point to your old head as "next" (not the other way round) because you want to insert it at the beginning.
public void addFirst(String aString) {
Link link = new Link();
link.setValue(aString);
if (isEmpty()) {
tail = link;
} else {
link.setNext(head);
}
head = link;
}
Also, you need to change your tests as now you compare String to Link instances instead of String to String
assertTrue("one".equals(list.removeFirst().getValue()));
assertTrue("two".equals(list.removeFirst().getValue()));
assertTrue("three".equals(list.removeFirst().getValue()));
After those changes the test passes for me (the first one, you didn't implement removeLast method for the second one).
You do not need a constructor to assign null, 0 and false to object fields. Fields get these values automatically. Simply remove the constructor that only does such assignments and it will be no any difference in execution.
You can also assign other default values like int a = 17 directly in declaration, and you can also include expressions like int b = a + 1 assuming that the field a has been declared before. But you cannot pass parameters that must have different values for different instances. This is what the constructor is for.
You may still have other bugs remaining but it will be no constructor to blame.
Most often developers simply use the ready solution, LinkedList.
I was writing a simple linked list implementation and the corresponding Trigger class like this:
public class Trigger {
public static void main(String[] args) {
LinkedList lin = new LinkedList();
lin.add(3);
lin.add(4);
lin.add(5);
lin.display();
}
}
public class LinkedList {
Item startItem;
LinkedList() {
startItem = new Item();
}
public void add(Integer newValue) {
if(startItem.value == null) {
startItem.value = newValue;
return;
}
Item tempItem = new Item();
tempItem.value = newValue;
while(startItem.next != null) {
startItem = startItem.next;
}
startItem.next = tempItem;
}
public void display() {
while(true) {
System.out.println("\t"+ startItem.value);
if(startItem.next == null) {
break;
} else {
startItem = startItem.next;
}
}
}
class Item {
Integer value;
Item next;
Item() {
this.value = null;
this.next = null;
}
}
}
The problem is, only the last two additions are getting retained and the previous additions are discarded. This is (of course) because i keep changing the object that the reference startItem points to. My question is, given a recursive structure like this, what is the correct looping mechanism? I know that in a linked list, there is no need to get to the end of the list to perform addition. Linked list structure is used as a context to ask about looping through recursive structures. Thanks.
Your basic structure is correct. You just need to add one line before you start looping...
Item currentItem = startItem;
while(currentItem.next != null) {
currentItem = currentItem.next;
}
The only time the field startItem should be changed is in your first check if its value is null. It's reference should never be changed as it should always point to the start of the structure. Consider making it final to enforce this.
Right away i just try to explain my problem:
Using BlazeDS i got the following Javaclasses:
DatabaseService.java:
public class Database {
private Category helpCat = null;
private Category root = new Category(1, "root", null, null);
private List<Article> database;
public Database()
{
// ------------ tree -----------------------------------------------------------------------
List<Category> level_one = new ArrayList<Category>();
List<Category> level_two_computer = new ArrayList<Category>();
List<Category> level_tree_hardware = new ArrayList<Category>();
// Level 1
Category buecher = new Category(2, "buecher", root, null);
Category computer = new Category(3, "computer", root, level_two_computer);
level_one.add(buecher);
level_one.add(computer);
//Level 2
Category hardware = new Category(4, "hardware", computer, level_tree_hardware);
Category software = new Category(5, "software", computer, null);
level_two_computer.add(hardware);
level_two_computer.add(software);
//Level 3
Category graphic = new Category(6, "graphic", hardware, null);
Category sound = new Category(7, "sound", hardware, null);
level_tree_hardware.add(graphic);
level_tree_hardware.add(sound);
// Level 0
root.addChilds(level_one);
// ------ tree end ----------------------------------------------------------------------------
database = new ArrayList<Article>();
try {
add(new Book("Per Anhalter durch die Galaxis", "42", Articletype.BOOK, 795, "Per Anhalter durch die Galaxiss", "Douglas Adams", "Heyne Verlag", "Taschenbuch", "3453146972"), buecher);
add(new Book("Harry Potter und der Stein der Weisen", "descriptionShort", Articletype.BOOK, 1299, "Harry Potter und der Stein der Weisen", "Joanne K. Rowling", "Carlsen Verlag GmbH", "gebunden", "3551551677"), buecher);
add(new Book("Harry Potter und die Kammer des Schreckens", "descriptionShort", Articletype.BOOK, 1499, "Harry Potter und die Kammer des Schreckens", "Joanne K. Rowling", "Carlsen Verlag GmbH", "gebunden", "3551551677"), buecher);
add(new Hardware("nVidia GeForce 8400GS", "Graphikkarte", Articletype.HARDWARE, 2665, "512 GDDR5 Speicher, DVI, 1 GPU", "MSI", "neu"), graphic);
add(new AKW("Biblis C", "Druckwasserreaktor, Preis auf Anfrage, Nur Selbstabholer", Articletype.AKW, -1, "Biblis", 0, 2525, "Siemens", 1, 2012), software);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Category> getCategories(String node) {
if(node.equalsIgnoreCase("root"))
return root.getChildren();
Category baum = null;
baum = get_node_by_name(root, node);
return baum.getChildren();
}
private Category get_node_by_name(Category localroot, String lookfor)
{
helpCat = null;
if(localroot.getChildren() != null)
{
for (int i = 0; i < localroot.getChildren().size(); ++i)
{
if(!(localroot.getChild(i).getName().equals(lookfor)))
{
get_node_by_name(localroot.getChild(i), lookfor);
}
else
{
helpCat = localroot.getChild(i);
helpCat.setParent(null);
}
}
}
return helpCat;
}
public List<Article> search(int artID, String name, Category categorie){
List<Article> ergebnis = new ArrayList<Article>();
if (artID >= 0)
{
for(int i = 0; i< database.size(); ++i){
if(database.get(i).getID() == artID)
{
ergebnis.add(database.get(i));
return ergebnis;
}
}
}
if (name != null){
for(int i = 0; i<database.size(); ++i){
if (database.get(i).getName().equalsIgnoreCase(name))
ergebnis.add(database.get(i));
}
return ergebnis;
}
if (categorie != null){
{
ergebnis.addAll(categorie.getArticles());
}
return ergebnis;
}
return database;
}
public Article add(Article newArticle, Category cat) throws Exception
{
newArticle.addCategory(cat);
if(newArticle.getID() != 0)
{
throw new Exception("Die Artikel ID wird vom DBS festgelegt!");
}
if (database.isEmpty())
{
newArticle.setID(0);
}
else
{
newArticle.setID(database.get(database.size() - 1).getID()+1);
}
database.add(newArticle);
return newArticle;
}
}
And the Category Class:
public class Category {
private int idCat;
private String nameTEST;
private Category parent = null;
private List<Article> articles = new ArrayList<Article>();
private List<Category> children = new ArrayList<Category>();
public Category(int _id, String _name, Category _parent, List<Category> _children)
{
if(_id > 0)
idCat = _id;
if(_name != null)
nameTEST = _name;
if(_parent != null)
parent = _parent;
if(_children != null)
children = _children;
}
public String toString()
{
return nameTEST;
}
void addArticle(Article article){
articles.add(article);
}
public List<Article> getAllArticles(){
List<Article> ergebnis = this.getArticles();
for (int i = 0; i<children.size();++i){
ergebnis.addAll(children.get(i).getAllArticles());
}
return ergebnis;
}
public void setID(int iD) {
idCat = iD;
}
public int getID() {
return idCat;
}
public void setName(String name) {
this.nameTEST = name;
}
public String getName() {
return nameTEST;
}
/**
* #param parent the parent to set
*/
public void setParent(Category parent)
{
this.parent = parent;
}
/**
* #return the articles
*/
public List<Article> getArticles()
{
return articles;
}
public void addChilds(List<Category> _next)
{
for (int i = 0; i < _next.size(); ++i)
{
children.add(_next.get(i));
}
}
public void addChild(Category one_next)
{
children.add(one_next);
}
public Category getChild(int index)
{
return children.get(index);
}
public void removeChild(Article article){
articles.remove(article);
}
public List<Category> getChildren()
{
return this.children;
}
}
also there are of course classes for articles and so on, but thats not important at that point.
the counterpart in flex looks like this:
Category.as
[RemoteClass(alias="PACKAGE.Category")]
public class Category
{
private var idCat:int = -1;
private var nameTEST:String = null;
private var parent:Category = null;
private var articles:ArrayCollection = new ArrayCollection;
private var children:ArrayCollection = new ArrayCollection;
public function Category(id:int, name:String, parent:Category, childlist:ArrayCollection, articles:ArrayCollection = null)
{
this.idCat = id;
this.nameTEST = name;
this.parent = parent;
this.articles = articles;
this.children = childlist;
}
public function setChildren(childList:ArrayCollection):void
{
this.children = childList;
}
public function getChildren():ArrayCollection
{
return this.children;
}
public function getName():String
{
return this.nameTEST;
}
}
Then i got a Flex service class calling BlazeDS and executing the getCategories java method. Since Flash dosn't seem to understand typed arrays, the result from that method which i get back in flex is a simple array of untyped objects (the mapping dosn't seem to work here, even tought the class category exists in flex and has the same properties).
thats the first thing. but however, i'm converting the untyped objects manually into objects of the category.as class.
the second thing is that categories have child-categories within the java object, which are also ArrayLists of the type category.java. the problem about that: my result event object only contains the first level of categories, looking into them the children are allways null. i dunno why they are empty, since they ARE part of the java object category.
and the third thing (the strangest by fast), you maybe noticed i named the properties of the category.java class strange, like idCat and nameTest instead of simply id and name. why that? because the property names of my flex result objects dont seem to change when i change the java objects properties names (result object properties are named "id" and "name" but the java class object properties are named "idCAT" and "nameTEST"). that it REALLY strange, since if i set the properties, like you see at nameTEST = "TESTNAME" it IS recogniced by flex, only the proertyNAMES dont seem to be recognized at all.
is blaze DS saving / caching the mapping configuration somewhere? how do i get it to rebuild the hole mappings IF so?
that could also explain my problem about the untyped objects i get from java, since before i changed the lists into ArrayLists they where vectors ( which blazeDS dosn't support AFAIK), and maybe not only the propertynames, but also the propertytypes are hard-mapped at some wired place and blazeds just dosn't get them refreshed.
i really like checked everything 5 times by now, even redeployed blazeds on the server to make sure no mappings left, but it didnt help at all.
ANY ideas what i could do? (exept changing to another serializer then blazeds (thats what i'm going to do if everything else fails...))
i have the same issues, but if you can warm up the tree before call the method, it will be ok.
what i mean "warm up" is you iterator the arraylist without doing anything. it is flunky why this work!
I have had similar problems with a list returned from a service not including the child elements in the list. I have found that BlazeDS can return a typed list. The two things that fixed this for me were:
a) Ensure that the returned list is of type java.util.List (not java.util.ArrayList for example).
b) Ensure that the class for the elements in the list have both public setters and getters for all entities to be returned.
For example - the following code works for me:
public class OrganisationService {
...
public List<Organisation> getOrganisations() {
List<Organisation> list = new ArrayList<Organisation>();
...
return list;
}
...
}
As mentioned elsewhere, you need to initiailize your AS3 remote objects so that it is included in the SWF during compilation.
Somewhere in your code, add:
var cat:Category = new Category();
var art:Article = new Article();
That should fix your generic object issue. (I add mine all in one spot, in an application start up method).
It turns out that a simple missmatch of the classnames was the cause of all evil. still some problems to solve, but atleast i get sub-arrays returned now, only the objects are still simple AS 3 objects and not the specified "Category" and "Article" objects, but i think thats because i dont have all methods included and mapping is failing because of that.
but thanks for your offer, i appreciate that.