Skip to content

Commit

Permalink
fixed #367 added validation for @DaTa with parent class without defau…
Browse files Browse the repository at this point in the history
…lt constructor required by lombok
  • Loading branch information
mplushnikov committed Aug 22, 2017
1 parent 127c8b1 commit b422f3e
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 126 deletions.
Expand Up @@ -23,6 +23,7 @@
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -55,6 +56,11 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiCla
equalsAndHashCodeProcessor.validateCallSuperParamExtern(psiAnnotation, psiClass, builder); equalsAndHashCodeProcessor.validateCallSuperParamExtern(psiAnnotation, psiClass, builder);
} }


final String staticName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "staticConstructor");
if (shouldGenerateRequiredArgsConstructor(psiClass, staticName)) {
requiredArgsConstructorProcessor.validateBaseClassConstructor(psiClass, builder);
}

return validateAnnotationOnRightType(psiClass, builder); return validateAnnotationOnRightType(psiClass, builder);
} }


Expand All @@ -80,6 +86,15 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot
if (PsiAnnotationSearchUtil.isNotAnnotatedWith(psiClass, ToString.class)) { if (PsiAnnotationSearchUtil.isNotAnnotatedWith(psiClass, ToString.class)) {
target.addAll(toStringProcessor.createToStringMethod(psiClass, psiAnnotation)); target.addAll(toStringProcessor.createToStringMethod(psiClass, psiAnnotation));
} }

final String staticName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "staticConstructor");
if (shouldGenerateRequiredArgsConstructor(psiClass, staticName)) {
target.addAll(requiredArgsConstructorProcessor.createRequiredArgsConstructor(psiClass, PsiModifier.PUBLIC, psiAnnotation, staticName));
}
}

private boolean shouldGenerateRequiredArgsConstructor(@NotNull PsiClass psiClass, @Nullable String staticName) {
boolean result = false;
// create required constructor only if there are no other constructor annotations // create required constructor only if there are no other constructor annotations
if (PsiAnnotationSearchUtil.isNotAnnotatedWith(psiClass, NoArgsConstructor.class, RequiredArgsConstructor.class, AllArgsConstructor.class, if (PsiAnnotationSearchUtil.isNotAnnotatedWith(psiClass, NoArgsConstructor.class, RequiredArgsConstructor.class, AllArgsConstructor.class,
Builder.class, lombok.experimental.Builder.class)) { Builder.class, lombok.experimental.Builder.class)) {
Expand All @@ -88,15 +103,13 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot


// and only if there are no any other constructors! // and only if there are no any other constructors!
if (definedConstructors.isEmpty()) { if (definedConstructors.isEmpty()) {
final String staticName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "staticConstructor");
final Collection<PsiField> requiredFields = requiredArgsConstructorProcessor.getRequiredFields(psiClass); final Collection<PsiField> requiredFields = requiredArgsConstructorProcessor.getRequiredFields(psiClass);


if (requiredArgsConstructorProcessor.validateIsConstructorDefined(psiClass, staticName, requiredFields, ProblemEmptyBuilder.getInstance())) { result = requiredArgsConstructorProcessor.validateIsConstructorNotDefined(
target.addAll(requiredArgsConstructorProcessor.createRequiredArgsConstructor( psiClass, staticName, requiredFields, ProblemEmptyBuilder.getInstance());
psiClass, PsiModifier.PUBLIC, psiAnnotation, staticName));
}
} }
} }
return result;
} }


@Override @Override
Expand Down
Expand Up @@ -87,7 +87,7 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot
final String staticName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "staticConstructor"); final String staticName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "staticConstructor");
final Collection<PsiField> requiredFields = allArgsConstructorProcessor.getAllFields(psiClass); final Collection<PsiField> requiredFields = allArgsConstructorProcessor.getAllFields(psiClass);


if (allArgsConstructorProcessor.validateIsConstructorDefined(psiClass, staticName, requiredFields, ProblemEmptyBuilder.getInstance())) { if (allArgsConstructorProcessor.validateIsConstructorNotDefined(psiClass, staticName, requiredFields, ProblemEmptyBuilder.getInstance())) {
target.addAll(allArgsConstructorProcessor.createAllArgsConstructor(psiClass, PsiModifier.PUBLIC, psiAnnotation, staticName, requiredFields)); target.addAll(allArgsConstructorProcessor.createAllArgsConstructor(psiClass, PsiModifier.PUBLIC, psiAnnotation, staticName, requiredFields));
} }
} }
Expand Down
Expand Up @@ -91,7 +91,7 @@ private boolean validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNu
return result; return result;
} }


private boolean validateBaseClassConstructor(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) { public boolean validateBaseClassConstructor(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) {
if (psiClass instanceof PsiAnonymousClass || psiClass.isEnum()) { if (psiClass instanceof PsiAnonymousClass || psiClass.isEnum()) {
return true; return true;
} }
Expand All @@ -114,7 +114,7 @@ private boolean validateBaseClassConstructor(@NotNull PsiClass psiClass, @NotNul
return false; return false;
} }


public boolean validateIsConstructorDefined(@NotNull PsiClass psiClass, @Nullable String staticConstructorName, @NotNull Collection<PsiField> params, @NotNull ProblemBuilder builder) { public boolean validateIsConstructorNotDefined(@NotNull PsiClass psiClass, @Nullable String staticConstructorName, @NotNull Collection<PsiField> params, @NotNull ProblemBuilder builder) {
boolean result = true; boolean result = true;


final List<PsiType> paramTypes = new ArrayList<PsiType>(params.size()); final List<PsiType> paramTypes = new ArrayList<PsiType>(params.size());
Expand Down
Expand Up @@ -33,9 +33,8 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiCla


final Collection<PsiField> allNotInitializedNotStaticFields = getAllFields(psiClass); final Collection<PsiField> allNotInitializedNotStaticFields = getAllFields(psiClass);
final String staticConstructorName = getStaticConstructorName(psiAnnotation); final String staticConstructorName = getStaticConstructorName(psiAnnotation);
if (!validateIsConstructorDefined(psiClass, staticConstructorName, allNotInitializedNotStaticFields, builder)) { result &= validateIsConstructorNotDefined(psiClass, staticConstructorName, allNotInitializedNotStaticFields, builder);
result = false;
}
return result; return result;
} }


Expand Down
Expand Up @@ -33,9 +33,8 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiCla
result = super.validate(psiAnnotation, psiClass, builder); result = super.validate(psiAnnotation, psiClass, builder);


final String staticConstructorName = getStaticConstructorName(psiAnnotation); final String staticConstructorName = getStaticConstructorName(psiAnnotation);
if (!validateIsConstructorDefined(psiClass, staticConstructorName, Collections.<PsiField>emptyList(), builder)) { result &= validateIsConstructorNotDefined(psiClass, staticConstructorName, Collections.<PsiField>emptyList(), builder);
result = false;
}
return result; return result;
} }


Expand Down
Expand Up @@ -34,9 +34,8 @@ protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiCla


final Collection<PsiField> allReqFields = getRequiredFields(psiClass); final Collection<PsiField> allReqFields = getRequiredFields(psiClass);
final String staticConstructorName = getStaticConstructorName(psiAnnotation); final String staticConstructorName = getStaticConstructorName(psiAnnotation);
if (!validateIsConstructorDefined(psiClass, staticConstructorName, allReqFields, builder)) { result &= validateIsConstructorNotDefined(psiClass, staticConstructorName, allReqFields, builder);
result = false;
}
return result; return result;
} }


Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Expand Up @@ -268,6 +268,7 @@
<li>Fixed #337: Component name collision: 'UpdateComponent'</li> <li>Fixed #337: Component name collision: 'UpdateComponent'</li>
<li>Fixed #339: Delombok for @ToString should use simpleName for class</li> <li>Fixed #339: Delombok for @ToString should use simpleName for class</li>
<li>Fixed #342: @EqualsAndHashCode/@ToString should treat explicit "of = {}" differently</li> <li>Fixed #342: @EqualsAndHashCode/@ToString should treat explicit "of = {}" differently</li>
<li>Fixed #367: @Data on parent and child should be marked as compilation error</li>
<li>Fixed #393: @Builder with @Singular map produces duplicated variables</li> <li>Fixed #393: @Builder with @Singular map produces duplicated variables</li>
<li>Fixed #399: IndexNotReadyException in FieldFindUsagesHandler</li> <li>Fixed #399: IndexNotReadyException in FieldFindUsagesHandler</li>
</ol> </ol>
Expand Down
Expand Up @@ -29,4 +29,8 @@ public void testClassWithLombokConstructor() throws Exception {
public void testClassWithLombokDefaultConstructor() throws Exception { public void testClassWithLombokDefaultConstructor() throws Exception {
doTest(); doTest();
} }

public void testDataWithParentClassWithoutDefaultConstructor() throws Exception {
doTest();
}
} }
218 changes: 109 additions & 109 deletions test-manual/pom.xml
Expand Up @@ -2,123 +2,123 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.plushnikov.intellij.plugin</groupId> <groupId>de.plushnikov.intellij.plugin</groupId>
<artifactId>manual-lombok-test</artifactId> <artifactId>manual-lombok-test</artifactId>
<version>1.0</version> <version>1.0</version>


<properties> <properties>
<slf4j.version>1.7.14</slf4j.version> <slf4j.version>1.7.14</slf4j.version>
<log4j2.version>2.5</log4j2.version> <log4j2.version>2.5</log4j2.version>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>


<prerequisites> <prerequisites>
<maven>3.0</maven> <maven>3.0</maven>
</prerequisites> </prerequisites>


<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.16.12</version> <version>1.16.18</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>


<dependency> <dependency>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<version>1.2</version> <version>1.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.17</version> <version>1.2.17</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-ext</artifactId> <artifactId>slf4j-ext</artifactId>
<version>${slf4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>


<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId> <artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version> <version>${log4j2.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version> <version>${log4j2.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jboss.logging</groupId> <groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId> <artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version> <version>3.3.0.Final</version>
</dependency> </dependency>


<dependency> <dependency>
<groupId>com.google.code.findbugs</groupId> <groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId> <artifactId>jsr305</artifactId>
<version>3.0.1</version> <version>3.0.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.7.0</version> <version>2.7.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.persistence</groupId> <groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId> <artifactId>persistence-api</artifactId>
<version>1.0.2</version> <version>1.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.validation</groupId> <groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId> <artifactId>validation-api</artifactId>
<version>1.1.0.Final</version> <version>1.1.0.Final</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.modelmapper</groupId> <groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId> <artifactId>modelmapper</artifactId>
<version>0.7.5</version> <version>0.7.5</version>
</dependency> </dependency>


<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>19.0</version> <version>19.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.12</version>
</dependency> </dependency>
</dependencies> </dependencies>




<build> <build>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version> <version>3.5</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
</build> </build>
</project> </project>
@@ -0,0 +1,15 @@
@lombok.Data
class Parent {
private final String a;
}

<error descr="Lombok needs a default constructor in the base class">@lombok.Data</error>
class Child extends Parent {
private final String b;
}

public class DataWithParentClassWithoutDefaultConstructor {
public static void main(String[] args) {
System.out.println(new Child("xxx"));
}
}

0 comments on commit b422f3e

Please sign in to comment.