Skip to content

Commit

Permalink
IDEA-147072 StackOverflowException on importing gradle build using an…
Browse files Browse the repository at this point in the history
…t's ExpandProperties filter
  • Loading branch information
vladsoroka committed Oct 30, 2015
1 parent bcab2bc commit 15341ce
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.jetbrains.jps.gradle.compiler;

import com.intellij.openapi.util.Ref;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.filters.ExpandProperties;
import org.gradle.api.Transformer;
import org.gradle.util.ConfigureUtil;
import org.jetbrains.jps.gradle.model.impl.ResourceRootFilter;
Expand All @@ -27,10 +29,7 @@
import java.io.FilterReader;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;

/**
Expand Down Expand Up @@ -70,7 +69,7 @@ public Reader transform(Reader original) {
}

private Reader doTransform(ResourceRootFilter filter, Reader original) {
if ("RenamingCopyFilter" .equals(filter.filterType)) {
if ("RenamingCopyFilter".equals(filter.filterType)) {
final Matcher matcher = (Matcher)filter.getProperties().get("matcher");
final String replacement = (String)filter.getProperties().get("replacement");
if (matcher == null || replacement == null) return original;
Expand All @@ -95,6 +94,15 @@ private Reader doTransform(ResourceRootFilter filter, Reader original) {
FilterReader result = (FilterReader)constructor.newInstance(original);
final Map<Object, Object> properties = filter.getProperties();
if (!properties.isEmpty()) {
if (ExpandProperties.class.getName().equals(filter.filterType)) {
final Map<Object, Object> antProps = new HashMap<Object, Object>(properties);
final Project project = new Project();
for (Map.Entry<Object, Object> entry : antProps.entrySet()) {
project.setProperty(entry.getKey().toString(), entry.getValue().toString());
}
properties.clear();
properties.put("project", project);
}
ConfigureUtil.configureByMap(properties, result);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.intellij.util.xmlb.annotations.Tag;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -69,6 +70,9 @@ public Map<Object, Object> getProperties() {
} catch (JsonParseException e) {
throw new RuntimeException("Unsupported filter: " + properties , e);
}
if(propertiesMap == null) {
propertiesMap = new HashMap<Object, Object>();
}
}
return propertiesMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ public void testRenameFilter() throws Exception {
assertCopied("build/resources/main/dir/file001.txt");
}

@Test
public void testExpandPropertiesFilter() throws Exception {
createProjectSubFile(
"src/main/resources/dir/file.txt", "some text ${myProp} another text");
importProject(
"apply plugin: 'java'\n" +
"\n" +
"import org.apache.tools.ant.filters.*\n" +
"ant.project.setProperty('myProp', 'myPropValue')\n" +
"processResources {\n" +
" filter (ExpandProperties, project: ant.project)\n" +
"}"
);
assertModules("project", "project_main", "project_test");
compileModules("project_main");

assertCopied("build/resources/main/dir/file.txt", "some text myPropValue another text");
}

@Test
public void testFiltersChain() throws Exception {
createProjectSubFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ class ExternalProjectBuilderImpl implements ModelBuilderService {
def filter = [filterType: filterType] as DefaultExternalFilter
def props = action?.val$properties
if (props) {
filter.propertiesAsJsonMap = new GsonBuilder().create().toJson(props);
if ('org.apache.tools.ant.filters.ExpandProperties'.equals(filterType) && props['project']) {
if (props['project']) filter.propertiesAsJsonMap = new GsonBuilder().create().toJson(props['project'].properties);
}
else {
filter.propertiesAsJsonMap = new GsonBuilder().create().toJson(props);
}
}
filterReaders << filter
}
Expand Down

0 comments on commit 15341ce

Please sign in to comment.