summaryrefslogtreecommitdiff
path: root/compilerCommon
diff options
context:
space:
mode:
authorYigit Boyar <yboyar@google.com>2015-06-24 01:41:52 -0700
committerYigit Boyar <yboyar@google.com>2015-06-24 01:50:09 -0700
commit0c2ed0cbaee2f206e926bfc780b05e9f1e52b551 (patch)
tree1ad9539db3014ed952e0695213ad5aa1665104d4 /compilerCommon
parent72a6debc95561b817660c3b23ae081aa7912a308 (diff)
downloaddata-binding-0c2ed0cbaee2f206e926bfc780b05e9f1e52b551.tar.gz
Remove guava dependency from the plugin
This CL is the first step in getting rid of guava dependency. It removes guava from compilerCommon which in return removes the dependency for the gradle plugin. This CL also fixes compiler tests which were broken by the listener CL. Bug: 22047836 Change-Id: I43f87885c5291174f5bc694487fc6c075b480194
Diffstat (limited to 'compilerCommon')
-rw-r--r--compilerCommon/build.gradle1
-rw-r--r--compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java15
-rw-r--r--compilerCommon/src/main/java/android/databinding/tool/store/LayoutFileParser.java14
-rw-r--r--compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java56
-rw-r--r--compilerCommon/src/main/java/android/databinding/tool/util/XmlEditor.java131
5 files changed, 110 insertions, 107 deletions
diff --git a/compilerCommon/build.gradle b/compilerCommon/build.gradle
index 2531c026..4e3c5d38 100644
--- a/compilerCommon/build.gradle
+++ b/compilerCommon/build.gradle
@@ -38,7 +38,6 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile project(':baseLibrary')
compile 'org.apache.commons:commons-lang3:3.3.2'
- compile 'com.google.guava:guava:17.0'
compile 'com.tunnelvisionlabs:antlr4:4.5'
compile 'commons-io:commons-io:2.4'
}
diff --git a/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java b/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java
index 503b3cfd..f48c0022 100644
--- a/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java
+++ b/compilerCommon/src/main/java/android/databinding/tool/LayoutXmlProcessor.java
@@ -13,9 +13,6 @@
package android.databinding.tool;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-
import org.apache.commons.lang3.StringEscapeUtils;
import org.xml.sax.SAXException;
@@ -69,7 +66,10 @@ public class LayoutXmlProcessor {
public static List<File> getLayoutFiles(List<File> resources) {
List<File> result = new ArrayList<File>();
- for (File resource : Iterables.filter(resources, fileExists)) {
+ for (File resource : resources) {
+ if (!resource.exists() || !resource.canRead()) {
+ continue;
+ }
if (resource.isDirectory()) {
for (File layoutFolder : resource.listFiles(layoutFolderFilter)) {
for (File xmlFile : layoutFolder.listFiles(xmlFileFilter)) {
@@ -187,13 +187,6 @@ public class LayoutXmlProcessor {
mFileWriter.writeToFile(RESOURCE_BUNDLE_PACKAGE + "." + CLASS_NAME, classString);
}
- private static final Predicate<File> fileExists = new Predicate<File>() {
- @Override
- public boolean apply(File input) {
- return input.exists() && input.canRead();
- }
- };
-
private static final FilenameFilter layoutFolderFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
diff --git a/compilerCommon/src/main/java/android/databinding/tool/store/LayoutFileParser.java b/compilerCommon/src/main/java/android/databinding/tool/store/LayoutFileParser.java
index 3c3c8e99..4c3a64dd 100644
--- a/compilerCommon/src/main/java/android/databinding/tool/store/LayoutFileParser.java
+++ b/compilerCommon/src/main/java/android/databinding/tool/store/LayoutFileParser.java
@@ -13,8 +13,6 @@
package android.databinding.tool.store;
-import com.google.common.base.Preconditions;
-
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
@@ -137,9 +135,14 @@ public class LayoutFileParser {
if ("include".equals(nodeName)) {
// get the layout attribute
final Node includedLayout = attributes.getNamedItem("layout");
- Preconditions.checkNotNull(includedLayout, "must include a layout");
+ if (includedLayout == null) {
+ L.e("%s must include a layout", xml.getAbsolutePath());
+ }
final String includeValue = includedLayout.getNodeValue();
- Preconditions.checkArgument(includeValue.startsWith(LAYOUT_PREFIX));
+ if (!includeValue.startsWith(LAYOUT_PREFIX)) {
+ L.e("included value in %s must start with %s.",
+ xml.getAbsolutePath(), LAYOUT_PREFIX);
+ }
// if user is binding something there, there MUST be a layout file to be
// generated.
String layoutName = includeValue.substring(LAYOUT_PREFIX.length());
@@ -159,7 +162,8 @@ public class LayoutFileParser {
}
final ResourceBundle.BindingTargetBundle bindingTargetBundle =
bundle.createBindingTarget(id == null ? null : id.getNodeValue(),
- viewName, true, tag, originalTag == null ? null : originalTag.getNodeValue());
+ viewName, true, tag,
+ originalTag == null ? null : originalTag.getNodeValue());
nodeTagMap.put(parent, tag);
bindingTargetBundle.setIncludedLayout(includedLayoutName);
diff --git a/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java b/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java
index b426170c..7e0df096 100644
--- a/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java
+++ b/compilerCommon/src/main/java/android/databinding/tool/store/ResourceBundle.java
@@ -13,10 +13,7 @@
package android.databinding.tool.store;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-
+import org.antlr.v4.runtime.misc.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import android.databinding.tool.util.L;
@@ -55,7 +52,10 @@ public class ResourceBundle implements Serializable {
}
public void addLayoutBundle(LayoutFileBundle bundle) {
- Preconditions.checkArgument(bundle.mFileName != null, "File bundle must have a name");
+ if (bundle.mFileName == null) {
+ L.e("File bundle must have a name. %s does not have one.", bundle);
+ return;
+ }
if (!mLayoutBundles.containsKey(bundle.mFileName)) {
mLayoutBundles.put(bundle.mFileName, new ArrayList<LayoutFileBundle>());
}
@@ -96,16 +96,10 @@ public class ResourceBundle implements Serializable {
}
}
- final Iterable<Map.Entry<String, List<LayoutFileBundle>>> multiResLayouts = Iterables
- .filter(mLayoutBundles.entrySet(),
- new Predicate<Map.Entry<String, List<LayoutFileBundle>>>() {
- @Override
- public boolean apply(Map.Entry<String, List<LayoutFileBundle>> input) {
- return input.getValue().size() > 1;
- }
- });
-
- for (Map.Entry<String, List<LayoutFileBundle>> bundles : multiResLayouts) {
+ for (Map.Entry<String, List<LayoutFileBundle>> bundles : mLayoutBundles.entrySet()) {
+ if (bundles.getValue().size() < 2) {
+ continue;
+ }
// validate all ids are in correct view types
// and all variables have the same name
Map<String, String> variableTypes = new HashMap<String, String>();
@@ -126,18 +120,20 @@ public class ResourceBundle implements Serializable {
}
for (Map.Entry<String, String> variable : bundle.mVariables.entrySet()) {
String existing = variableTypes.get(variable.getKey());
- Preconditions
- .checkState(existing == null || existing.equals(variable.getValue()),
- "inconsistent variable types for %s for layout %s",
- variable.getKey(), bundle.mFileName);
+ if (existing != null && !existing.equals(variable.getValue())) {
+ L.e("inconsistent variable types for %s for layout %s",
+ variable.getKey(), bundle.mFileName);
+ continue;
+ }
variableTypes.put(variable.getKey(), variable.getValue());
}
for (Map.Entry<String, String> userImport : bundle.mImports.entrySet()) {
String existing = importTypes.get(userImport.getKey());
- Preconditions
- .checkState(existing == null || existing.equals(userImport.getValue()),
- "inconsistent variable types for %s for layout %s",
- userImport.getKey(), bundle.mFileName);
+ if (existing != null && !existing.equals(userImport.getValue())) {
+ L.e("inconsistent variable types for %s for layout %s",
+ userImport.getKey(), bundle.mFileName);
+ continue;
+ }
importTypes.put(userImport.getKey(), userImport.getValue());
}
}
@@ -173,14 +169,16 @@ public class ResourceBundle implements Serializable {
target.isBinder());
if (target.mId != null) {
if (target.isBinder()) {
- Preconditions.checkState(!viewBindingIds.contains(target.getFullClassName()),
- "Cannot use the same id for a View and an include tag. Error " +
- "in file %s / %s", bundle.mFileName, bundle.mConfigName);
+ if (viewBindingIds.contains(target.getFullClassName())) {
+ L.e("Cannot use the same id for a View and an include tag. Error " +
+ "in file %s / %s", bundle.mFileName, bundle.mConfigName);
+ }
includeBindingIds.add(target.getFullClassName());
} else {
- Preconditions.checkState(!includeBindingIds.contains(target.getFullClassName()),
- "Cannot use the same id for a View and an include tag. Error in "
- + "file %s / %s", bundle.mFileName, bundle.mConfigName);
+ if (includeBindingIds.contains(target.getFullClassName())) {
+ L.e("Cannot use the same id for a View and an include tag. Error in"
+ + " file %s / %s", bundle.mFileName, bundle.mConfigName);
+ }
viewBindingIds.add(target.getFullClassName());
}
String existingType = viewTypes.get(target.mId);
diff --git a/compilerCommon/src/main/java/android/databinding/tool/util/XmlEditor.java b/compilerCommon/src/main/java/android/databinding/tool/util/XmlEditor.java
index b80b9475..4ae393f1 100644
--- a/compilerCommon/src/main/java/android/databinding/tool/util/XmlEditor.java
+++ b/compilerCommon/src/main/java/android/databinding/tool/util/XmlEditor.java
@@ -16,12 +16,6 @@
package android.databinding.tool.util;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
@@ -65,36 +59,31 @@ public class XmlEditor {
return null; // not a binding layout
}
- Iterable<? extends XMLParser.ElementContext> dataNodes = Iterables
- .filter(elements(root), new Predicate<XMLParser.ElementContext>() {
- @Override
- public boolean apply(XMLParser.ElementContext input) {
- return "data".equals(nodeName(input));
- }
- });
- Preconditions.checkState(Iterables.size(dataNodes) < 2,
- "Multiple binding data tags. Expecting a maximum of one.");
+ List<? extends ElementContext> childrenOfRoot = elements(root);
+ List<? extends XMLParser.ElementContext> dataNodes = filterNodesByName("data",
+ childrenOfRoot);
+ if (dataNodes.size() > 1) {
+ L.e("Multiple binding data tags in %s. Expecting a maximum of one.",
+ f.getAbsolutePath());
+ }
- ArrayList<String> lines = Lists.newArrayList();
+ ArrayList<String> lines = new ArrayList<>();
lines.addAll(FileUtils.readLines(f, "utf-8"));
for (android.databinding.parser.XMLParser.ElementContext it : dataNodes) {
replace(lines, toPosition(it.getStart()), toEndPosition(it.getStop()), "");
}
- Iterable<? extends XMLParser.ElementContext> layoutNodes = Iterables
- .filter(elements(root), new Predicate<XMLParser.ElementContext>() {
- @Override
- public boolean apply(XMLParser.ElementContext input) {
- return !"data".equals(nodeName(input));
- }
- });
- Preconditions.checkState(Iterables.size(layoutNodes) == 1,
- "Only one layout element and one data element are allowed");
+ List<? extends XMLParser.ElementContext> layoutNodes =
+ excludeNodesByName("data", childrenOfRoot);
+ if (layoutNodes.size() != 1) {
+ L.e("Only one layout element and one data element are allowed. %s has %d",
+ f.getAbsolutePath(), layoutNodes.size());
+ }
- final XMLParser.ElementContext layoutNode = Iterables.getFirst(layoutNodes, null);
+ final XMLParser.ElementContext layoutNode = layoutNodes.get(0);
- ArrayList<Pair<String, android.databinding.parser.XMLParser.ElementContext>> noTag = Lists
- .newArrayList();
+ ArrayList<Pair<String, android.databinding.parser.XMLParser.ElementContext>> noTag =
+ new ArrayList<>();
recurseReplace(layoutNode, lines, noTag, newTag, 0);
@@ -111,15 +100,14 @@ public class XmlEditor {
for (AttributeContext attr : attributes(root)) {
rootAttributes.append(' ').append(attr.getText());
}
- Optional<Pair<String, XMLParser.ElementContext>> pairOptional = Iterables
- .tryFind(noTag, new Predicate<Pair<String, XMLParser.ElementContext>>() {
- @Override
- public boolean apply(Pair<String, XMLParser.ElementContext> input) {
- return input.getRight() == layoutNode;
- }
- });
- if (pairOptional.isPresent()) {
- Pair<String, XMLParser.ElementContext> noTagRoot = pairOptional.get();
+ Pair<String, XMLParser.ElementContext> noTagRoot = null;
+ for (Pair<String, XMLParser.ElementContext> pair : noTag) {
+ if (pair.getRight() == layoutNode) {
+ noTagRoot = pair;
+ break;
+ }
+ }
+ if (noTagRoot != null) {
ImmutablePair<String, XMLParser.ElementContext>
newRootTag = new ImmutablePair<>(
noTagRoot.getLeft() + rootAttributes.toString(), layoutNode);
@@ -157,6 +145,28 @@ public class XmlEditor {
return StringUtils.join(lines, System.getProperty("line.separator"));
}
+ private static <T extends XMLParser.ElementContext> List<T>
+ filterNodesByName(String name, Iterable<T> items) {
+ List<T> result = new ArrayList<>();
+ for (T item : items) {
+ if (name.equals(nodeName(item))) {
+ result.add(item);
+ }
+ }
+ return result;
+ }
+
+ private static <T extends XMLParser.ElementContext> List<T>
+ excludeNodesByName(String name, Iterable<T> items) {
+ List<T> result = new ArrayList<>();
+ for (T item : items) {
+ if (!name.equals(nodeName(item))) {
+ result.add(item);
+ }
+ }
+ return result;
+ }
+
private static Position toPosition(Token token) {
return new Position(token.getLine() - 1, token.getCharPositionInLine());
}
@@ -172,37 +182,42 @@ public class XmlEditor {
private static List<? extends AttributeContext> attributes(XMLParser.ElementContext elementContext) {
if (elementContext.attribute() == null) {
- return Lists.newArrayList();
+ return new ArrayList<>();
} else {
return elementContext.attribute();
}
}
- private static Iterable<? extends AttributeContext> expressionAttributes(
+ private static List<? extends AttributeContext> expressionAttributes(
XMLParser.ElementContext elementContext) {
- return Iterables.filter(attributes(elementContext), new Predicate<AttributeContext>() {
- @Override
- public boolean apply(AttributeContext input) {
- String attrName = input.attrName.getText();
- String value = input.attrValue.getText();
- return attrName.equals("android:tag") ||
- (value.startsWith("\"@{") && value.endsWith("}\"")) ||
- (value.startsWith("'@{") && value.endsWith("}'"));
+ List<AttributeContext> result = new ArrayList<>();
+ for (AttributeContext input : attributes(elementContext)) {
+ String attrName = input.attrName.getText();
+ String value = input.attrValue.getText();
+ if (attrName.equals("android:tag") ||
+ (value.startsWith("\"@{") && value.endsWith("}\"")) ||
+ (value.startsWith("'@{") && value.endsWith("}'"))) {
+ result.add(input);
}
- });
+ }
+ return result;
}
private static Position endTagPosition(XMLParser.ElementContext context) {
if (context.content() == null) {
// no content, so just subtract from the "/>"
Position endTag = toEndPosition(context.getStop());
- Preconditions.checkState(endTag.charIndex > 0);
+ if (endTag.charIndex <= 0) {
+ L.e("invalid input in %s", context);
+ }
endTag.charIndex -= 2;
return endTag;
} else {
// tag with no attributes, but with content
Position position = toPosition(context.content().getStart());
- Preconditions.checkState(position.charIndex > 0);
+ if (position.charIndex <= 0) {
+ L.e("invalid input in %s", context);
+ }
position.charIndex--;
return position;
}
@@ -213,7 +228,7 @@ public class XmlEditor {
if (context.content() != null && context.content().element() != null) {
return context.content().element();
}
- return Lists.newArrayList();
+ return new ArrayList<>();
}
private static boolean replace(ArrayList<String> lines, Position start, Position end,
@@ -252,11 +267,11 @@ public class XmlEditor {
}
private static boolean hasExpressionAttributes(XMLParser.ElementContext context) {
- Iterable<? extends AttributeContext> expressions = expressionAttributes(context);
- int size = Iterables.size(expressions);
+ List<? extends AttributeContext> expressions = expressionAttributes(context);
+ int size = expressions.size();
//noinspection ConstantConditions
return size > 1 || (size == 1 &&
- !Iterables.getFirst(expressions, null).attrName.getText().equals("android:tag"));
+ !expressions.get(0).attrName.getText().equals("android:tag"));
}
private static int recurseReplace(XMLParser.ElementContext node, ArrayList<String> lines,
@@ -264,13 +279,7 @@ public class XmlEditor {
String newTag, int bindingIndex) {
int nextBindingIndex = bindingIndex;
boolean isMerge = "merge".equals(nodeName(node));
- final boolean containsInclude = Iterables.tryFind(elements(node),
- new Predicate<ElementContext>() {
- @Override
- public boolean apply(ElementContext input) {
- return "include".equals(nodeName(input));
- }
- }).isPresent();
+ final boolean containsInclude = filterNodesByName("include", elements(node)).size() > 0;
if (!isMerge && (hasExpressionAttributes(node) || newTag != null || containsInclude)) {
String tag = "";
if (newTag != null) {