Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Lombok #92

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The MapStruct plugin requires Java 11 or later

## Building from Source

Since the project has been migrated to the Gradle and [Gradle IntelliJ plugin][gradle-intellij-plugin],
Since the project has been migrated to the Gradle and [Gradle IntelliJ plugin](https://github.com/JetBrains/gradle-intellij-plugin),
the build process is much simpler. The only thing to build the plugin is to run:

./gradlew build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.intellij.util.LombokUtil;
import org.mapstruct.intellij.util.MapstructUtil;

import java.util.Objects;
Expand Down Expand Up @@ -68,7 +69,7 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
methods = psiClass.findMethodsByName( "is" + MapstructUtil.capitalize( value ), true );
}
if ( methods.length > 0 && isPublicNonStatic( methods[0] ) ) {
return methods[0];
return LombokUtil.resolvePsiElementForMethod( methods[0], value, psiClass );
}

PsiField field = psiClass.findFieldByName( value, true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.intellij.util.LombokUtil;
import org.mapstruct.intellij.util.MapStructVersion;
import org.mapstruct.intellij.util.MapstructUtil;
import org.mapstruct.intellij.util.TargetType;
Expand Down Expand Up @@ -86,22 +87,22 @@ builderSupportPresent && isBuilderEnabled( getMappingMethod() )
if ( constructor != null && constructor.hasParameters() ) {
for ( PsiParameter parameter : constructor.getParameterList().getParameters() ) {
if ( value.equals( parameter.getName() ) ) {
return parameter;
return LombokUtil.resolvePsiElement( constructor, parameter, value, psiClass );
}
}
}
}

PsiMethod[] methods = psiClass.findMethodsByName( "set" + MapstructUtil.capitalize( value ), true );
if ( methods.length != 0 && isPublicNonStatic( methods[0] ) ) {
return methods[0];
return LombokUtil.resolvePsiElementForMethod( methods[0], value, psiClass );
}

if ( builderSupportPresent ) {
for ( PsiMethod method : psiClass.findMethodsByName( value, true ) ) {
if ( method.getParameterList().getParametersCount() == 1 &&
MapstructUtil.isFluentSetter( method, typeToUse ) ) {
return method;
return LombokUtil.resolvePsiElementForMethod( method, value, psiClass );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
else if ( resolved instanceof PsiParameter ) {
targetType = ( (PsiParameter) resolved ).getType();
}
else if ( resolved instanceof PsiField) {
else if ( resolved instanceof PsiField ) {
targetType = ( (PsiField) resolved ).getType();
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/mapstruct/intellij/util/LombokUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.intellij.util;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;

/**
* @author Filip Hrisafov
*/
public final class LombokUtil {

private static final Class<?> LOMBOK_LIGHT_METHOD;

static {
Class<?> lombokLightMethod;
try {
lombokLightMethod = Class.forName( "de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder" );
}
catch ( ClassNotFoundException e ) {
lombokLightMethod = null;
}
LOMBOK_LIGHT_METHOD = lombokLightMethod;
}

private LombokUtil() {
}

public static boolean isLombokLightMethod(PsiMethod method) {
if ( LOMBOK_LIGHT_METHOD != null ) {
return LOMBOK_LIGHT_METHOD.isInstance( method );
}
return false;
}

public static PsiElement resolvePsiElementForMethod(PsiMethod method, String value, PsiClass psiClass) {
return resolvePsiElement( method, method, value, psiClass );
}

public static PsiElement resolvePsiElement(PsiMethod method, PsiElement currentResolved, String value,
PsiClass psiClass) {
if ( isLombokLightMethod( method ) ) {
PsiField field = psiClass.findFieldByName( value, true );
if ( field != null ) {
return field;
}
}

return currentResolved;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/mapstruct/intellij/util/TargetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ public static PsiMethod resolveMappingConstructor(@NotNull PsiClass psiClass) {
List<PsiMethod> accessibleConstructors = new ArrayList<>(constructors.length);

for ( PsiMethod constructor : constructors ) {
if ( constructor.hasModifier( JvmModifier.PRIVATE ) ) {
// private constructors are ignored
if ( constructor.hasModifier( JvmModifier.PRIVATE ) || constructor.hasModifier( JvmModifier.PROTECTED ) ) {
// private and protected constructors are ignored
continue;
}
if ( !constructor.hasParameters() ) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.java</depends>
<depends optional="true" config-file="withKotlin.xml">org.jetbrains.kotlin</depends>
<depends optional="true" config-file="org.mapstruct.intellij-withKotlin.xml">org.jetbrains.kotlin</depends>

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
Expand Down