I have defined the following live template in IntelliJ:
private static final Logger log = LoggerFactory.getLogger($CLASS_NAME$.class);
I use it to insert logger variable to a class.
Is it possible to define so that template also adds
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
to the file if these definitions are still absent?
Define it fully in the Live template:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);
and IntelliJ should auto reformat the expression to an import. (Assuming you already have the lib JAR downloaded and configured with IntelliJ).
Edit: As comment says: the Shorten FQ Names check-box should be checked (which it is by default)
Tested with IntelliJ IDEA 15.0.4
Now its possible to add live templates with static imports:
You have to check static import in Options
#org.junit.Test
public void should$EXPR$when$CONDITION$() {
org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue()));
}
Related
I am trying to migrate an existing project from direct log4j to slf4j with log4j binding.
Also I am upgrading the the version of log4j from 1.2 to 2.6
Some of the common code change are :-
1.
import org.apache.log4j.Logger;
.
.
.
private final static Logger log = Logger.getLogger(SearchXYZ.class);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.
.
.
private final static Logger log = LoggerFactory.getLogger(SearchXYZ.class);
2.
import org.apache.log4j.Logger;
.
.
.
private static final Logger logger = Logger.getLogger(XYZ.class);
.
.
.
logger.fatal("FAILURE", throwableObject);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
.
.
.
private static final Logger logger = LoggerFactory.getLogger(XYZ.class);
private static Marker fatal = MarkerFactory.getMarker("FATAL");
.
.
.
logger.error(fatal, "FAILURE", throwableObject);
Removed Appenders.
and so on.
One place I'm stuck is Configurator file.
AppConfigLog4jConfigurator.configureForBootstrap();
It gives compile time error saying :-
class file for org.apache.log4j.spi.Configurator not found
What does this function do? What is a possible replacement for this?
First, I am not really sure why you are switching to the SLF4J API since the Log4j 2 API supports everything SLF4J does and much more. In my own code I have found that switching only requires changing the imports and LoggerFactory to LogManager.
Configurator is a class in Log4j 1 that is used to configure Log4j. It is similar to the Configurator class in Log4j 2. You probably want to call one of the initialize methods.
I'm new in java, please help me to understand this.
I can see there is ReadHtml class and defined with one public method. But when i put this code in ecplise, it shows red mark under WebClient with tag that "this cannot resolved to a type". May I know what does it mean? Gone through all about method definition but couldn't find any remedy to understand this.
Can I get any help ?
public class ReadHtml {
public static LinkedList<String> readJacksonCounty(String urlName, String pStartDate,String pFinishDate)
{
LinkedList<String> xmlListReturn=new LinkedList<String>();
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");
final WebClient webClient1 = new WebClient(BrowserVersion.CHROME);
webClient1.setJavaScriptTimeout(60000);
webClient1.getCookieManager().setCookiesEnabled(true);//enable cookies
webClient1.getCache().clear();
You are missing an import of this library:
import com.gargoylesoftware.htmlunit.WebClient;
Add this to the top of your file (and read dsp_user's comment for future reference).
Basically "...cannot be resolved to a type" means that type is not available on the class path. If you're just using eclipse refere to How to import a jar in Eclipse.
If you already added the needed jar onto your class path, you are missing the import statement. Imports just make it so that you dont have to use a class's fully qualified name. (you can type
MyClass myClass;
as opposed to
com.some.package.MyClass myClass;
if you add
import com.some.package.MyClass;
at the top of your file.
Note that if you want to build a jar from your project you'll need some kind of build tool. If you choose to use Maven, which is very common, just read any tutorial on how to get started and manage dependencies.
In Eclipse (Java), how do I automatically add code to every class I create. Suppose I create a class called Foo, I want this code to automatically go in the preamble/state:
private final Logger log = LoggerFactory.getLogger(this.getClass());
and the appropriate slf4j import to be automatically imported. Similarly, I would like the constructor to automatically show up. Full example of what I would like to see after I click the create button:
package test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public Foo() {
}
}
This should help. You can modify whichever template suits your purpose.
You could change the New Java Files and Class body templates to get what you want.
In the Preferences, under Java-> Code Style -> Code Templates, there is New Java Files where you would add the imports, at the appropriate place.
Change the Class body template like this
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void ${type_name}() {
}
to add the logger and a default, public constructor.
Making those 2 changes will automatically add what you want when you create a new Java class file with Eclipse.
in test classes I have the following import
import static org.junit.Assert.*;
when I do organize import via ctrl + shift + o then it automatically changes to following
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
How can I configure eclipse not to do it ?
PS: I only want junit imports not be handled in that way
=============
I added a save action to remove unused imports. [properties -> java editor -> save actions]
so everytime I save unused imports are removed (since I used ctrl + shift + o mainly to remove unused imports this looks like a way forward..)
Change the number of static imports to 1
Under Window, Preferences, Java, Code Style, Organize Imports there's an option called "Number of static imports needed for .*" - set that to 1. (Another way to find it quickly is just to type "static" into the search box in preferences.)
Note that this will mean that hitting Ctrl-Shift-O will always turn any static imports into an static import-on-demand form, which may not be what you want. If you have separate projects for test and non-test code, you could configure it on a per project basis.
Personally I'd just live with the explicit imports - I usually end up with static imports by starting off with the class-qualified call, and then hitting Ctrl-Shift-M on the method name to import it statically.
My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:
static import only from classes and interfaces
Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?
Update:
per #Ted's request, the constant declaration in the referenced file:
public static final String NULL = "<NULL>";
and the (bowdlerized) reference in the referring file:
import static my.path.MyClass.NULL;
My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:
import static pkg.Class.staticField;
do this:
import static pkg.Class.*;
Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...
I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue
I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.
I had the following:
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
I removed the import static org.junit.Assert.fail; (no idea how I managed to get that in there in the first place) and all is now working.
I accidentally set test directory as source. And Test sources were considered as source files.
sourceSets.main.java.srcDirs 'src'
| -- src
| -- main
| -- test
Fix:
sourceSets.main.java.srcDirs 'src/main'
Some how same solution mentioned by #m-watson
I have replaced
import static org.junit.Assert.assertThrows;
With
import static org.junit.jupiter.api.Assertions.assertThrows;
and it worked