Skip to content

Commit

Permalink
Definition from application.properties to Java field which have Config*
Browse files Browse the repository at this point in the history
annotation

Fixes #4

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Oct 1, 2019
1 parent 75c048d commit cbdf66b
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 32 deletions.
3 changes: 2 additions & 1 deletion quarkus.jdt/com.redhat.quarkus.jdt.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources,
org.eclipse.m2e.core,
org.eclipse.m2e.maven.runtime,
org.eclipse.jdt.ls.core;resolution:=optional
org.eclipse.jdt.ls.core;resolution:=optional,
org.eclipse.lsp4j
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Export-Package: com.redhat.quarkus.commons,
Expand Down
1 change: 1 addition & 0 deletions quarkus.jdt/com.redhat.quarkus.jdt.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<extension point="org.eclipse.jdt.ls.core.delegateCommandHandler">
<delegateCommandHandler class="com.redhat.quarkus.jdt.internal.core.ls.QuarkusDelegateCommandHandler">
<command id="quarkus.java.projectInfo"/>
<command id="quarkus.java.propertyDefinition"/>
</delegateCommandHandler>
</extension>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.quarkus.commons;

/**
* Quarkus property definition parameters to retrieve the definition of the
* Quarkus property in Java class field.
*
* @author Angelo ZERR
*
*/
public class QuarkusPropertyDefinitionParams {

private String uri;

private String propertySource;

/**
* Returns the application.properties URI.
*
* @return the application.properties URI
*/
public String getUri() {
return uri;
}

/**
* Set the application.properties URI
*
* @param uri the application.properties URI
*/
public void setUri(String uri) {
this.uri = uri;
}

/**
* Returns the Quarkus property source. This source contains the class name and
* field name where Quarkus property is defined like
* <code>io.quarkus.deployment.ApplicationConfig#name</code>.
*
* @return the Quarkus property source.
*/
public String getPropertySource() {
return propertySource;
}

/**
* Set the Quarkus property source. This source contains the class name and
* field name where Quarkus property is defined like
* <code>io.quarkus.deployment.ApplicationConfig#name</code>.
*
* @param propertySource the Quarkus property source.
*/
public void setPropertySource(String propertySource) {
this.propertySource = propertySource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.redhat.quarkus.commons.ExtendedConfigDescriptionBuildItem;
import com.redhat.quarkus.commons.QuarkusProjectInfo;
import com.redhat.quarkus.commons.QuarkusPropertiesScope;
import com.redhat.quarkus.jdt.internal.core.QuarkusDeploymentJavaProject;
import com.redhat.quarkus.jdt.internal.core.utils.JDTQuarkusSearchUtils;
import com.redhat.quarkus.jdt.internal.core.utils.JDTQuarkusUtils;

Expand Down Expand Up @@ -90,6 +91,51 @@ private JDTQuarkusManager() {

}

/**
* Returns the Java field from the given property source
*
* @param file the application.properties file
* @param propertySource the property source to find
* @param progress the progress monitor.
* @return the Java field from the given property source
* @throws JavaModelException
* @throws CoreException
*/
public IField findDeclaredQuarkusProperty(IFile file, String propertySource, IProgressMonitor progress)
throws JavaModelException, CoreException {
String projectName = file.getProject().getName();
IJavaProject javaProject = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(projectName);
return findDeclaredQuarkusProperty(javaProject, propertySource, progress);
}

/**
* Returns the Java field from the given property source
*
* @param javaProject the Java project
* @param propertySource the property source to find
* @param progress the progress monitor.
* @return the Java field from the given property sources
* @throws JavaModelException
*/
public IField findDeclaredQuarkusProperty(IJavaProject javaProject, String propertySource,
IProgressMonitor progress) throws JavaModelException {
int index = propertySource.indexOf('#');
if (index == -1) {
return null;
}
String className = propertySource.substring(0, index);
String fieldName = propertySource.substring(index + 1, propertySource.length());
// Try to find type with standard classpath
IType type = javaProject.findType(className, progress);
if (type == null) {
// Not found, type could be included in deployment JAR which is not in classpath
// Try to find type from deployment JAR
type = new QuarkusDeploymentJavaProject(javaProject, QuarkusDeploymentJavaProject.MAVEN_ARTIFACT_RESOLVER,
false).findType(className, progress);
}
return type.getField(fieldName);
}

public QuarkusProjectInfo getQuarkusProjectInfo(IFile file, QuarkusPropertiesScope propertiesScope,
DocumentationConverter converter, IProgressMonitor progress) throws JavaModelException, CoreException {
String projectName = file.getProject().getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.quarkus.jdt.internal.core;

import java.io.File;

import com.redhat.quarkus.jdt.internal.core.QuarkusDeploymentJavaProject.ArtifactResolver;
import com.redhat.quarkus.jdt.internal.core.utils.DependencyUtil;

/**
* Maven artifact resolver used to download JAR and JAR sources with maven.
*
* @author Angelo ZERR
*
*/
public class MavenArtifactResolver implements ArtifactResolver {

@Override
public String getArtifact(String groupId, String artifactId, String version) {
File jarFile = null;
try {
jarFile = DependencyUtil.getArtifact(groupId, artifactId, version, null);
} catch (Exception e) {
return null;
}
return jarFile != null ? jarFile.toString() : null;
}

@Override
public String getSources(String groupId, String artifactId, String version) {
File jarFile = null;
try {
jarFile = DependencyUtil.getSources(groupId, artifactId, version);
} catch (Exception e) {
return null;
}
return jarFile != null ? jarFile.toString() : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import static com.redhat.quarkus.jdt.internal.core.QuarkusConstants.QUARKUS_EXTENSION_PROPERTIES;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJarEntryResource;
Expand All @@ -27,7 +27,6 @@
import org.eclipse.jdt.internal.core.ExternalJavaProject;

import com.redhat.quarkus.commons.QuarkusPropertiesScope;
import com.redhat.quarkus.jdt.internal.core.utils.DependencyUtil;
import com.redhat.quarkus.jdt.internal.core.utils.JDTQuarkusSearchUtils;

/**
Expand All @@ -42,26 +41,19 @@ public class QuarkusDeploymentJavaProject extends ExternalJavaProject {
* Artifact resolver API
*
*/
@FunctionalInterface
public static interface ArtifactResolver {

String resolve(String groupId, String artifactId, String version);
String getArtifact(String groupId, String artifactId, String version);

String getSources(String groupId, String artifactId, String version);
}

public static final ArtifactResolver MAVEN_ARTIFACT_RESOLVER = (groupId, artifactId, version) -> {
File jarFile = null;
try {
jarFile = DependencyUtil.getArtifact(groupId, artifactId, version, null);
} catch (Exception e) {
return null;
}
return jarFile != null ? jarFile.toString() : null;
};
public static final ArtifactResolver MAVEN_ARTIFACT_RESOLVER = new MavenArtifactResolver();

private final IJavaProject rootProject;

public QuarkusDeploymentJavaProject(IJavaProject rootProject, ArtifactResolver artifactResolver, boolean excludeTestCode)
throws JavaModelException {
public QuarkusDeploymentJavaProject(IJavaProject rootProject, ArtifactResolver artifactResolver,
boolean excludeTestCode) throws JavaModelException {
super(createDeploymentClasspath(rootProject, artifactResolver, excludeTestCode));
this.rootProject = rootProject;
}
Expand All @@ -72,12 +64,13 @@ public QuarkusDeploymentJavaProject(IJavaProject rootProject, ArtifactResolver a
* @param project the quarkus project
* @param artifactResolver the artifact resolver to use to download deployment
* JARs.
* @param excludeTestCode
* @param excludeTestCode
* @param downloadSources
* @return the classpath of deployment JARs.
* @throws JavaModelException
*/
private static IClasspathEntry[] createDeploymentClasspath(IJavaProject project, ArtifactResolver artifactResolver, boolean excludeTestCode)
throws JavaModelException {
private static IClasspathEntry[] createDeploymentClasspath(IJavaProject project, ArtifactResolver artifactResolver,
boolean excludeTestCode) throws JavaModelException {
List<IClasspathEntry> externalJarEntries = new ArrayList<>();

IClasspathEntry[] entries = project.getResolvedClasspath(true);
Expand All @@ -104,9 +97,17 @@ private static IClasspathEntry[] createDeploymentClasspath(IJavaProject project,
String groupId = result[0];
String artifactId = result[1];
String version = result[2];
String jarFile = artifactResolver.resolve(groupId, artifactId, version);
// Get or download deployment JAR
String jarFile = artifactResolver.getArtifact(groupId, artifactId, version);
if (jarFile != null) {
externalJarEntries.add(JavaCore.newLibraryEntry(new Path(jarFile), null, null));
IPath sourceAttachmentPath = null;
// Get or download deployment sources JAR
String sourceJarFile = artifactResolver.getSources(groupId, artifactId, version);
if (sourceJarFile != null) {
sourceAttachmentPath = new Path(sourceJarFile);
}
externalJarEntries
.add(JavaCore.newLibraryEntry(new Path(jarFile), sourceAttachmentPath, null));
}
}
}
Expand Down Expand Up @@ -153,5 +154,4 @@ public IJavaElement[] getElementsToSearch(QuarkusPropertiesScope propertiesScope
}
return elements;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.managers.IBuildSupport;
import org.eclipse.lsp4j.Location;
import org.eclipse.m2e.core.embedder.ArtifactKey;

import com.redhat.quarkus.commons.QuarkusPropertiesScope;
import com.redhat.quarkus.jdt.core.DocumentationConverter;
Expand All @@ -37,6 +46,8 @@ public class QuarkusDelegateCommandHandler implements IDelegateCommandHandler {

public static final String PROJECT_INFO_COMMAND_ID = "quarkus.java.projectInfo";

public static final String PROPERTY_DEFINITION_COMMAND_ID = "quarkus.java.propertyDefinition";

private static final IQuarkusPropertiesChangedListener LISTENER = (event) -> {
JavaLanguageServerPlugin.getInstance().getClientConnection()
.executeClientCommand(QUARKUS_PROPERTIES_CHANGED_COMMAND, event);
Expand All @@ -57,8 +68,9 @@ public QuarkusDelegateCommandHandler() {
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
if (PROJECT_INFO_COMMAND_ID.equals(commandId)) {
return getQuarkusProjectInfo(arguments, commandId, progress);
} else if (PROPERTY_DEFINITION_COMMAND_ID.equals(commandId)) {
return findDeclaredQuarkusProperty(arguments, commandId, progress);
}

throw new UnsupportedOperationException(String.format("Unsupported command '%s'!", commandId));
}

Expand Down Expand Up @@ -118,4 +130,45 @@ private static DocumentationConverter getDocumentationConverter(String[] documen
return DocumentationConverter.DEFAULT_CONVERTER;
}

private static Location findDeclaredQuarkusProperty(List<Object> arguments, String commandId,
IProgressMonitor progress) throws CoreException {
Map<String, Object> obj = arguments.size() > 0 ? (Map<String, Object>) arguments.get(0) : null;
if (obj == null) {
throw new UnsupportedOperationException(String
.format("Command '%s' must be call with one QuarkusPropertyDefinitionParams argument!", commandId));
}
// Get project name from the application.properties URI
String applicationPropertiesUri = (String) obj.get("uri");
if (applicationPropertiesUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be call with required QuarkusPropertyDefinitionParams.uri (application.properties URI)!",
commandId));
}
IFile file = JDTUtils.findFile(applicationPropertiesUri);
if (file == null) {
throw new UnsupportedOperationException(
String.format("Cannot find IFile for '%s'", applicationPropertiesUri));
}
String propertySource = (String) obj.get("propertySource");
if (propertySource == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be call with required QuarkusPropertyDefinitionParams.propertySource!",
commandId));
}
IField field = JDTQuarkusManager.getInstance().findDeclaredQuarkusProperty(file, propertySource, progress);
if (field != null) {
IClassFile classFile = field.getClassFile();
if (classFile != null) {
// Try to download source
Optional<IBuildSupport> bs = JavaLanguageServerPlugin.getProjectsManager()
.getBuildSupport(file.getProject());
if (bs.isPresent()) {
bs.get().discoverSource(classFile, progress);
}
}
return JDTUtils.toLocation(field);
}
return null;
}

}
Loading

0 comments on commit cbdf66b

Please sign in to comment.