Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
Merge branch 'backport_intellij_11' into backport_intellij_10
Browse files Browse the repository at this point in the history
Conflicts:
	lombok-plugin/src/main/resources/META-INF/plugin.xml
  • Loading branch information
Michail Plushnikov committed Mar 9, 2014
2 parents 90a72e0 + ad8cb07 commit 99fbf12
Show file tree
Hide file tree
Showing 50 changed files with 455 additions and 113 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Provides support for lombok annotations to write great Java code with IntelliJ I
Tested and supports IntelliJ versions: 10.5.4, 11.1.5, 12.1.6, 13.0.2

With this plugin your IntelliJ can recognize all of generated getters, setters and some other things from lombok project, so that you get code completion and are able to work without errors stating the methods don't exists.



[![Donate](https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3F9HXD7A2SMCN)
1 change: 0 additions & 1 deletion lombok-api/lombok-api.iml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<excludeFolder url="file://$MODULE_DIR$/target/maven-archiver" />
<excludeFolder url="file://$MODULE_DIR$/target/maven-status" />
<excludeFolder url="file://$MODULE_DIR$/target/surefire" />
<excludeFolder url="file://$MODULE_DIR$/target/test-classes" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void addReadUsageFor(@NotNull Collection<? extends UserDataHolder>
}
}

private static final long PRESENT_TIME = TimeUnit.SECONDS.toNanos(1);
private static final long PRESENT_TIME = TimeUnit.SECONDS.toNanos(10);

private static class LombokPresentData {
private final boolean present;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.plushnikov.intellij.plugin.processor.clazz;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
Expand Down Expand Up @@ -50,11 +52,24 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiCla
* Validation process is the same, but in case it fails, errors where added twice.
* There is a @param shouldAddErrors to avoid this duplication.
*/
protected boolean validateInternal(PsiAnnotation psiAnnotation, PsiClass psiClass, ProblemBuilder builder, boolean shouldAddErrors) {
return validateAnnotationOnRightType(psiClass, builder, shouldAddErrors)
protected boolean validateInternal(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiClass psiClass, @NotNull ProblemBuilder builder, boolean shouldAddErrors) {
return validateBuilderClassName(psiAnnotation, builder, shouldAddErrors) &&
validateAnnotationOnRightType(psiClass, builder, shouldAddErrors)
&& validateExistingInnerClass(psiClass, psiAnnotation, builder, shouldAddErrors);
}

protected boolean validateBuilderClassName(PsiAnnotation psiAnnotation, ProblemBuilder builder, boolean shouldAddErrors) {
final String builderClassName = BuilderUtil.getBuilderClassName(psiAnnotation);
if (StringUtil.isNotEmpty(builderClassName) &&
!JavaPsiFacade.getInstance(psiAnnotation.getProject()).getNameHelper().isIdentifier(builderClassName)) {
if (shouldAddErrors) {
builder.addError("%s ist not a valid identifier", builderClassName);
}
return false;
}
return true;
}

protected boolean validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder, boolean shouldAddErrors) {
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum()) {
if (shouldAddErrors) {
Expand All @@ -70,7 +85,7 @@ protected boolean validateExistingInnerClass(@NotNull PsiClass psiClass, @NotNul
final PsiClass innerClassByName = PsiClassUtil.getInnerClassInternByName(psiClass, innerClassSimpleName);
if (innerClassByName != null) {
if (shouldAddErrors) {
builder.addError("Not generated '%s' class: A class with same name already exists. This feature is not implemented and it's not planned.", innerClassSimpleName);
builder.addError("Not generated '%s' class: A class with same name already exists.", innerClassSimpleName);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.plushnikov.intellij.plugin.processor.clazz.log;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
Expand All @@ -14,6 +15,7 @@
import de.plushnikov.intellij.plugin.problem.ProblemBuilder;
import de.plushnikov.intellij.plugin.processor.clazz.AbstractClassProcessor;
import de.plushnikov.intellij.plugin.psi.LombokLightFieldBuilder;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import de.plushnikov.intellij.plugin.util.PsiClassUtil;
import org.jetbrains.annotations.NotNull;

Expand All @@ -31,11 +33,13 @@ public abstract class AbstractLogProcessor extends AbstractClassProcessor {
private final static String loggerName = "log";
private final String loggerType;
private final String loggerInitializer;
private final String loggerCategory;

protected AbstractLogProcessor(@NotNull Class<? extends Annotation> supportedAnnotationClass, @NotNull String loggerType, @NotNull String loggerInitializer) {
protected AbstractLogProcessor(@NotNull Class<? extends Annotation> supportedAnnotationClass, @NotNull String loggerType, @NotNull String loggerInitializer, @NotNull String loggerCategory) {
super(supportedAnnotationClass, PsiField.class);
this.loggerType = loggerType;
this.loggerInitializer = loggerInitializer;
this.loggerCategory = loggerCategory;
}

public static String getLoggerName() {
Expand Down Expand Up @@ -64,7 +68,7 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot
target.add(createLoggerField(psiClass, psiAnnotation));
}

private LombokLightFieldBuilder createLoggerField(PsiClass psiClass, PsiAnnotation psiAnnotation) {
private LombokLightFieldBuilder createLoggerField(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
final Project project = psiClass.getProject();
final PsiManager manager = psiClass.getContainingFile().getManager();

Expand All @@ -75,13 +79,24 @@ private LombokLightFieldBuilder createLoggerField(PsiClass psiClass, PsiAnnotati
.withModifier(PsiModifier.FINAL).withModifier(PsiModifier.STATIC).withModifier(PsiModifier.PRIVATE)
.withNavigationElement(psiAnnotation);

final String classQualifiedName = psiClass.getQualifiedName();
final String className = null != classQualifiedName ? classQualifiedName : psiClass.getName();
PsiExpression initializer = psiElementFactory.createExpressionFromText(String.format(loggerInitializer, className), psiClass);
final String loggerInitializerParameter = createLoggerInitializeParameter(psiClass, psiAnnotation);
final PsiExpression initializer = psiElementFactory.createExpressionFromText(String.format(loggerInitializer, loggerInitializerParameter), psiClass);
loggerField.setInitializer(initializer);
return loggerField;
}

@NotNull
private String createLoggerInitializeParameter(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
final String loggerInitializerParameter;
final String topic = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "topic", String.class);
if (StringUtil.isEmptyOrSpaces(topic)) {
loggerInitializerParameter = String.format(loggerCategory, psiClass.getName());
} else {
loggerInitializerParameter = '"' + topic + '"';
}
return loggerInitializerParameter;
}

protected boolean hasFieldByName(@NotNull PsiClass psiClass, String... fieldNames) {
final Collection<PsiField> psiFields = PsiClassUtil.collectClassFieldsIntern(psiClass);
for (PsiField psiField : psiFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class CommonsLogProcessor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "org.apache.commons.logging.Log";
private static final String LOGGER_INITIALIZER = "org.apache.commons.logging.LogFactory.getLog(%s.class)";
private static final String LOGGER_CATEGORY = "%s.class";
private static final String LOGGER_INITIALIZER = "org.apache.commons.logging.LogFactory.getLog(%s)";

public CommonsLogProcessor() {
super(CommonsLog.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(CommonsLog.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class Log4j2Processor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "org.apache.logging.log4j.Logger";
private static final String LOGGER_INITIALIZER = "org.apache.logging.log4j.LogManager.getLogger(%s.class)";
private static final String LOGGER_CATEGORY = "%s.class";
private static final String LOGGER_INITIALIZER = "org.apache.logging.log4j.LogManager.getLogger(%s)";

public Log4j2Processor() {
super(Log4j2.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(Log4j2.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class Log4jProcessor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "org.apache.log4j.Logger";
private static final String LOGGER_INITIALIZER = "org.apache.log4j.Logger.getLogger(%s.class)";
private static final String LOGGER_CATEGORY = "%s.class";
private static final String LOGGER_INITIALIZER = "org.apache.log4j.Logger.getLogger(%s)";

public Log4jProcessor() {
super(Log4j.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(Log4j.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class LogProcessor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "java.util.logging.Logger";
private static final String LOGGER_INITIALIZER = "java.util.logging.Logger.getLogger(%s.class.getName())";
private static final String LOGGER_CATEGORY = "%s.class.getName()";
private static final String LOGGER_INITIALIZER = "java.util.logging.Logger.getLogger(%s)";

public LogProcessor() {
super(Log.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(Log.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class Slf4jProcessor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "org.slf4j.Logger";
private static final String LOGGER_INITIALIZER = "org.slf4j.LoggerFactory.getLogger(%s.class)";
private static final String LOGGER_CATEGORY = "%s.class";
private static final String LOGGER_INITIALIZER = "org.slf4j.LoggerFactory.getLogger(%s)";

public Slf4jProcessor() {
super(Slf4j.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(Slf4j.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
public class XSlf4jProcessor extends AbstractLogProcessor {

private static final String LOGGER_TYPE = "org.slf4j.ext.XLogger";
private static final String LOGGER_INITIALIZER = "org.slf4j.ext.XLoggerFactory.getXLogger(%s.class)";
private static final String LOGGER_CATEGORY = "%s.class";
private static final String LOGGER_INITIALIZER = "org.slf4j.ext.XLoggerFactory.getXLogger(%s)";

public XSlf4jProcessor() {
super(XSlf4j.class, LOGGER_TYPE, LOGGER_INITIALIZER);
super(XSlf4j.class, LOGGER_TYPE, LOGGER_INITIALIZER, LOGGER_CATEGORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DelegateFieldProcessor() {
@Override
protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiField psiField, @NotNull ProblemBuilder builder) {
final PsiType psiFieldType = psiField.getType();
return handler.validate(psiFieldType, psiAnnotation, builder);
return handler.validate(psiField, psiFieldType, psiAnnotation, builder);
}

protected void generatePsiElements(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@
*/
public class DelegateHandler {

public boolean validate(@NotNull PsiType psiType, @NotNull PsiAnnotation psiAnnotation, @NotNull ProblemBuilder builder) {
boolean result;
public boolean validate(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull PsiType psiType, @NotNull PsiAnnotation psiAnnotation, @NotNull ProblemBuilder builder) {
boolean result = true;

if (psiModifierListOwner.hasModifierProperty(PsiModifier.STATIC)) {
builder.addError("@Delegate is legal only on instance fields or no-argument instance methods.");
result = false;
}

final Collection<PsiType> types = collectDelegateTypes(psiAnnotation, psiType);
result = validateTypes(types, builder);
result &= validateTypes(types, builder);

final Collection<PsiType> excludes = collectExcludeTypes(psiAnnotation);
result &= validateTypes(excludes, builder);
Expand Down Expand Up @@ -189,7 +194,8 @@ private <T extends PsiModifierListOwner & PsiNamedElement> PsiMethod generateDel
.withModifier(PsiModifier.PUBLIC)
.withMethodReturnType(returnType)
.withContainingClass(psiClass)
.withNavigationElement(psiAnnotation);
//Have to go to original method, or some refactoring action will not work (like extract parameter oder change signature)
.withNavigationElement(psiMethod);

for (PsiTypeParameter typeParameter : psiMethod.getTypeParameters()) {
methodBuilder.withTypeParameter(typeParameter);
Expand All @@ -213,15 +219,13 @@ private <T extends PsiModifierListOwner & PsiNamedElement> PsiMethod generateDel
paramString.append(generatedParameterName).append(',');
}

final boolean isStatic = psiElement.hasModifierProperty(PsiModifier.STATIC);
if (paramString.length() > 0) {
paramString.deleteCharAt(paramString.length() - 1);
}
final boolean isMethodCall = psiElement instanceof PsiMethod;
methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(
String.format("%s%s.%s%s.%s(%s);",
String.format("%sthis.%s%s.%s(%s);",
PsiType.VOID.equals(returnType) ? "" : "return ",
isStatic ? psiClass.getName() : "this",
psiElement.getName(),
isMethodCall ? "()" : "",
psiMethod.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiMet
}

final PsiType returnType = psiMethod.getReturnType();
result &= null != returnType && handler.validate(returnType, psiAnnotation, builder);
result &= null != returnType && handler.validate(psiMethod, returnType, psiAnnotation, builder);

return result;
}
Expand Down
Loading

0 comments on commit 99fbf12

Please sign in to comment.