Categories
discuss

How to specify a target package for ANTLR?

If I call:

java org.antlr.Tool -o outdir sources/com/example/Java5.g

…with antlr-3.1.3 the parser and lexer code will be generated in the directory outdir/sources/com/example. But the generated classes don’t have any package statement. I need them to life in the package com.example.

Is there a way to specify the target package?

Answer

ANTLR provides a header tool which allows you to include package and imports. You include this in your *.g grammar file:

@header {
    package org.xmlcml.cml.converters.antlr;
    import java.util.HashMap;
}

And you may need it in the Lexer as well:

@lexer::header {package org.xmlcml.cml.converters.antlr;}

and in case you need to add some members and code:

@members {
    HashMap<String, Object> objectMap = new HashMap<String, Object>();
    //...

    private void addArrayValue(String content) {
    //... code required by snippets in the grammar

    }
}
Categories
discuss

Is there a Java parser for the Java language?

I’m looking for a java library that allows me to parse a java source file and that gives me an AST representation of the code.

Actually I’m only interested in the class and method definitions with their annotations. I don’t need the AST of the method code.

I’m using this information for code generation. This is why I can’t compile the source file first to get the information from the resulting class file. The code wouldn’t compile without errors until I generate some additional classes.

Answer

I found the project javaparser, which parses a single java file and returns a well formed AST.

Categories
discuss

ConcurrentModificationException despite using synchronized

 public synchronized X getAnotherX(){ 
  if(iterator.hasNext()){
   X b = iterator.next();
   String name = b.getInputFileName();
  ...
   return b;
  }
  else{return null;}
 }

despite the synchronized statement in the declaration header, i still get a ConcurrentModificationException Exception at the line where i use iterator.next(); whats wrong here ?

Answer

ConcurrentModificationException usually has nothing to do with multiple threads. Most of the time it occurs because you are modifying the collection over which it is iterating within the body of the iteration loop. For example, this will cause it:

Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
    Item item = (Item) iterator.next();
    if (item.satisfiesCondition()) {
       collection.remove(item);
    }
}

In this case you must use the iterator.remove() method instead. This occurs equally if you are adding to the collection, in which case there is no general solution. However, the subtype ListIterator can be used if dealing with a list and this has an add() method.

Categories
discuss

Are there any specific examples of backward incompatibilities between Java versions?

Have there been incompatibilities between Java releases where Java source code/Java class files targeting Java version X won’t compile/run under version Y (where Y > X) ?

By “Java release” I mean versions such as:

  • JDK 1.0 (January, 1996)
  • JDK 1.1 (February, 1997)
  • J2SE 1.2 (December, 1998)
  • J2SE 1.3 (May, 2000)
  • J2SE 1.4 (February, 2002)
  • J2SE 5.0 (September, 2004)
  • Java SE 6 (December, 2006)

House rules:

  • Please include references and code examples where possible.
  • Please try to be very specific/concrete in your answer.
  • A class that is being marked as @Deprecated does not count as a backwards incompatibility.

Answer

Compatibility notes for various versions:

The first major hiccup I remember was the introduction of assert in Java 1.4. It affected a lot of JUnit code.

Categories
discuss

spring+hibernate mapping class without xml

in my applicationContext.xml, this is how I map xml to POJO. how to map directory to class file without required to create xml?

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
            <list>
                <value>com/custompackage/custom/spi/hibernate3/HibernateCurrentStep.hbm.xml</value>
                <value>com/custompackage/custom/spi/hibernate3/HibernateHistoryStep.hbm.xml</value>
                 <value>com/custompackage/custom/spi/hibernate3/HibernatecustomEntry.hbm.xml</value>
                  <value>user/custom/hibernate3/PropertySetItemImpl.hbm.xml</value>
                   <value>com/custompackage/user/provider/hibernate3/user/impl/HibernateGroupImpl.hbm.xml</value>
                   <value>com/custompackage/user/provider/hibernate3/user/impl/HibernateUserImpl.hbm.xml</value>
            </list>
        </property>


        <property name="hibernateProperties">
            .....
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

    </bean>

Answer

And you can further simplify things by converting

<property name="annotatedClasses">
     <list>
       <value>com.mycompany.sample.domain.Order</value>
       <value>com.mycompany.sample.domain.LineItem</value>
       ...
     </list>
 </property>

to

<property name="packagesToScan" value="com.mycompany.sample.domain" />

in your AnnotationSessionFactoryBean so now all classes annotated with @Entity in the com.mycompany.sample.domain package will be automatically picked up.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..