Skip to content

Commit

Permalink
Augment @Inject
Browse files Browse the repository at this point in the history
  • Loading branch information
matan129 committed Jun 30, 2017
1 parent e4eea86 commit 8115059
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 36 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cache:
directories:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
- $HOME/.m2
10 changes: 5 additions & 5 deletions artwork/factory.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 27 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
buildscript {
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
}
}

plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.2.13'
id 'net.ltgt.apt' version '0.10'
}

apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
mavenLocal()
mavenCentral()
jcenter()
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

def isCi = System.env.CI != null

intellij {
version 'IC-2017.1'
pluginName 'Google AutoFactory Support'
downloadSources !isCi
updateSinceUntilBuild false
}

group 'mr.intellij.plugin.autofactory'
version '0.0.1'

repositories {
mavenLocal()
mavenCentral()
jcenter()
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.16.16'
apt 'org.projectlombok:lombok:1.16.16'
apt 'org.projectlombok:lombok:1.16.16'

compile 'com.google.auto.factory:auto-factory:1.0-beta5'
compile 'com.google.guava:guava:22.0'
compile group: 'com.google.inject', name: 'guice', version: '4.1.0'

compile 'com.google.auto.factory:auto-factory:1.0-beta5'
apt 'com.google.auto.factory:auto-factory:1.0-beta5'
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'org.mockito:mockito-core:2.8.47'
testCompile "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junitVersion=5.0.0-M4
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tue Jun 27 00:16:44 IDT 2017
#Fri Jun 30 14:35:14 IDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = 'autofactory-intellij-plugin'

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mr.intellij.plugin.autofactory.augment;

import com.intellij.psi.PsiElement;
import com.intellij.psi.augment.PsiAugmentProvider;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class GuardedPsiAugmentProvider<T extends PsiElement> extends PsiAugmentProvider {

private boolean suppress = false;

private final Class<T> supportedType;

@NotNull
@Override
@SneakyThrows
@SuppressWarnings("unchecked")
protected final <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element,
@NotNull Class<Psi> type) {

if (suppress || supportedType != type) {
return super.getAugments(element, type);
}

try (Suppressor ignored = new Suppressor()) {
return (List<Psi>) doGetAugments(element);
}
}

protected abstract List<T> doGetAugments(@NotNull PsiElement element);

private class Suppressor implements AutoCloseable {

public Suppressor() {
suppress = true;
}

@Override
public void close() throws Exception {
suppress = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package mr.intellij.plugin.autofactory.augment;

import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierList;
import mr.intellij.plugin.autofactory.utils.AnnotationUtils;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
import java.util.Collections;
import java.util.List;

public class InjectAugmentProvider extends GuardedPsiAugmentProvider<PsiAnnotation> {

protected InjectAugmentProvider() {
super(PsiAnnotation.class);
}

@NotNull
@Override
protected List<PsiAnnotation> doGetAugments(@NotNull PsiElement element) {
if (element instanceof PsiModifierList) {
PsiModifierList modifierList = (PsiModifierList) element;

if ((AnnotationUtils.isAnnotationPresent(modifierList, Inject.class, com.google.inject.Inject.class))) {
return Collections.emptyList();
}

PsiElement context = element.getContext();

if (!(context instanceof PsiMethod)) {
return Collections.emptyList();
}

PsiMethod psiMethod = (PsiMethod) context;

if (!(psiMethod.isConstructor()) || !AnnotationUtils.hasAutoFactory(psiMethod, true)) {
return Collections.emptyList();
}

return Collections.singletonList(AnnotationUtils.createAnnotation(element.getProject(), Inject.class));
}

return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package mr.intellij.plugin.autofactory.inspections.conflicting.constructor;

import com.google.auto.factory.AutoFactory;
import com.google.common.collect.Lists;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.*;
import lombok.RequiredArgsConstructor;
import mr.intellij.plugin.autofactory.utils.AnnotationUtils;
import mr.intellij.plugin.autofactory.utils.MethodUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mr.intellij.plugin.autofactory.utils;

import com.google.auto.factory.AutoFactory;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,9 +28,24 @@ public static Optional<PsiAnnotation> findAutoFactory(@Nullable PsiModifierListO
return Optional.empty();
}

@SafeVarargs
public static boolean isAnnotationPresent(@Nullable PsiModifierList psiModifierList,
@NotNull Class<? extends Annotation> annotationClass) {
@NotNull Class<? extends Annotation>... annotationClasses) {

return psiModifierList != null && psiModifierList.findAnnotation(annotationClass.getName()) != null;
for (Class<? extends Annotation> annotationClass : annotationClasses) {
if (psiModifierList != null && psiModifierList.findAnnotation(annotationClass.getName()) != null) {
return true;
}
}

return false;
}

@NotNull
public static PsiAnnotation createAnnotation(@NotNull Project project,
@NotNull Class<? extends Annotation> annotationClass) {

return JavaPsiFacade.getElementFactory(project)
.createAnnotationFromText("@" + annotationClass.getName(), null);
}
}
26 changes: 14 additions & 12 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<idea-plugin>
<id>mr.intellij.plugin.autofactory</id>
<name>Google Autofactory Support</name>
<version>0.0.1</version>
<vendor email="[email protected]">Matan Rosenberg</vendor>
<id>mr.intellij.plugin.autofactory</id>
<name>Google Autofactory Support</name>
<version>0.0.1</version>
<vendor email="[email protected]">Matan Rosenberg</vendor>

<description>Provides support for Google's AutoFactory.</description>
<description>Provides support for Google's AutoFactory.</description>

<change-notes/>
<idea-version since-build="162.0"/>
<change-notes/>
<idea-version since-build="162.0"/>

<extensions defaultExtensionNs="com.intellij">
<inspectionToolProvider implementation="mr.intellij.plugin.autofactory.inspections.AutoFactoryInspectionsProvider"/>
<codeInsight.lineMarkerProvider language="JAVA"
implementationClass="mr.intellij.plugin.autofactory.line.markers.AutoFactoryLineMarkerProvider"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">
<inspectionToolProvider
implementation="mr.intellij.plugin.autofactory.inspections.AutoFactoryInspectionsProvider"/>
<codeInsight.lineMarkerProvider language="JAVA"
implementationClass="mr.intellij.plugin.autofactory.line.markers.AutoFactoryLineMarkerProvider"/>
<lang.psiAugmentProvider implementation="mr.intellij.plugin.autofactory.augment.InjectAugmentProvider"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<p>When using @Provided, two or more constructors have the same effective parameters.</p>
</body>
</html>

0 comments on commit 8115059

Please sign in to comment.