下面列出了com.google.common.collect.ImmutableList#indexOf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void sanitizeCycle(ImmutableList<String> cycleAcls) {
// Remove previous ACL from referencing ACLs
int aclIndex = cycleAcls.indexOf(_acl.getName());
int cycleSize = cycleAcls.size();
// Remove next ACL from dependencies, and record line numbers that reference dependency
String nextAclName = cycleAcls.get((aclIndex + 1) % cycleSize);
int dependencyIndex = 0;
while (!_dependencies.get(dependencyIndex).dependency.getName().equals(nextAclName)) {
dependencyIndex++;
}
for (int lineNum : _dependencies.remove(dependencyIndex).lineNums) {
_linesInCycles.add(lineNum);
markLineUnmatchable(lineNum);
}
}
@Test
public void createsJsDir() {
JsBundleGenrule genrule = setup.genrule();
BuildContext context = FakeBuildContext.withSourcePathResolver(sourcePathResolver());
FakeBuildableContext buildableContext = new FakeBuildableContext();
ImmutableList<Step> buildSteps =
ImmutableList.copyOf(genrule.getBuildSteps(context, buildableContext));
MkdirStep expectedStep =
MkdirStep.of(
BuildCellRelativePath.fromCellRelativePath(
context.getBuildCellRootPath(),
genrule.getProjectFilesystem(),
context.getSourcePathResolver().getRelativePath(genrule.getSourcePathToOutput())));
assertThat(buildSteps, hasItem(expectedStep));
int mkJsDirIdx = buildSteps.indexOf(expectedStep);
assertThat(buildSteps.subList(mkJsDirIdx, buildSteps.size()), not(hasItem(any(RmStep.class))));
}
@Test
public void createsSourcemapDir() {
setUpWithRewriteSourceMap();
JsBundleGenrule genrule = setup.genrule();
BuildContext context = FakeBuildContext.withSourcePathResolver(sourcePathResolver());
FakeBuildableContext buildableContext = new FakeBuildableContext();
ImmutableList<Step> buildSteps =
ImmutableList.copyOf(genrule.getBuildSteps(context, buildableContext));
MkdirStep expectedStep =
MkdirStep.of(
BuildCellRelativePath.fromCellRelativePath(
context.getBuildCellRootPath(),
genrule.getProjectFilesystem(),
context
.getSourcePathResolver()
.getRelativePath(genrule.getSourcePathToSourceMap())
.getParent()));
assertThat(buildSteps, hasItem(expectedStep));
int mkSourceMapDirIdx = buildSteps.indexOf(expectedStep);
assertThat(
buildSteps.subList(mkSourceMapDirIdx, buildSteps.size()), not(hasItem(any(RmStep.class))));
}
@Test
public void createsMiscDir() {
setUpWithRewriteMiscDir();
JsBundleGenrule genrule = setup.genrule();
BuildContext context = FakeBuildContext.withSourcePathResolver(sourcePathResolver());
FakeBuildableContext buildableContext = new FakeBuildableContext();
ImmutableList<Step> buildSteps =
ImmutableList.copyOf(genrule.getBuildSteps(context, buildableContext));
MkdirStep expectedStep =
MkdirStep.of(
BuildCellRelativePath.fromCellRelativePath(
context.getBuildCellRootPath(),
genrule.getProjectFilesystem(),
context.getSourcePathResolver().getRelativePath(genrule.getSourcePathToMisc())));
assertThat(buildSteps, hasItem(expectedStep));
int mkMiscDirIdx = buildSteps.indexOf(expectedStep);
assertThat(
buildSteps.subList(mkMiscDirIdx, buildSteps.size()), not(hasItem(any(RmStep.class))));
}
private void assertResourcePathOrdering(ImmutableList<String> command, String... paths) {
String errorMessage = String.format("Full command was: %s", Joiner.on(" ").join(command));
assertThat(
errorMessage,
command.stream().filter(s -> "-S".equals(s)).collect(ImmutableList.toImmutableList()),
Matchers.hasSize(paths.length));
int firstResourceFolderArgument = command.indexOf("-S");
List<Matcher<? super String>> expectedSubslice = new ArrayList<>();
for (String path : paths) {
BuildTarget buildTarget = BuildTargetFactory.newInstance("//:" + path);
expectedSubslice.add(Matchers.is("-S"));
expectedSubslice.add(
Matchers.is(
BuildTargetPaths.getGenPath(filesystem, buildTarget, "%s")
+ "/res#resources-symlink-tree/res"));
}
assertThat(
errorMessage,
command.subList(
firstResourceFolderArgument, firstResourceFolderArgument + expectedSubslice.size()),
Matchers.contains(expectedSubslice));
}
private void reorderProperties(TypeElement type, List<RetroFacebookProcessor.Property> properties) {
PropertyOrderer propertyOrderer = getPropertyOrderer(type);
if (propertyOrderer == null) {
return;
}
final ImmutableList<String> order;
try {
order = propertyOrderer.determinePropertyOrder();
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, e.toString());
return;
}
// We expect that all the properties will be found, but if not then we won't try reordering.
boolean allFound = true;
for (RetroFacebookProcessor.Property property : properties) {
allFound &= order.contains(property.getGetter());
}
if (allFound) {
// We successfully found the abstract methods corresponding to all the properties, so now
// reorder the List<Property> to reflect the order of the methods.
Comparator<RetroFacebookProcessor.Property> comparator = new Comparator<RetroFacebookProcessor.Property>() {
@Override
public int compare(RetroFacebookProcessor.Property a, RetroFacebookProcessor.Property b) {
String aName = a.getGetter();
String bName = b.getGetter();
return order.indexOf(aName) - order.indexOf(bName);
}
};
Collections.sort(properties, comparator);
}
}
private void reorderProperties(TypeElement type, List<RetroFacebookProcessor.Property> properties) {
PropertyOrderer propertyOrderer = getPropertyOrderer(type);
if (propertyOrderer == null) {
return;
}
final ImmutableList<String> order;
try {
order = propertyOrderer.determinePropertyOrder();
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, e.toString());
return;
}
// We expect that all the properties will be found, but if not then we won't try reordering.
boolean allFound = true;
for (RetroFacebookProcessor.Property property : properties) {
allFound &= order.contains(property.getGetter());
}
if (allFound) {
// We successfully found the abstract methods corresponding to all the properties, so now
// reorder the List<Property> to reflect the order of the methods.
Comparator<RetroFacebookProcessor.Property> comparator = new Comparator<RetroFacebookProcessor.Property>() {
@Override
public int compare(RetroFacebookProcessor.Property a, RetroFacebookProcessor.Property b) {
String aName = a.getGetter();
String bName = b.getGetter();
return order.indexOf(aName) - order.indexOf(bName);
}
};
Collections.sort(properties, comparator);
}
}
private void reorderProperties(TypeElement type, List<RetroWeiboProcessor.Property> properties) {
PropertyOrderer propertyOrderer = getPropertyOrderer(type);
if (propertyOrderer == null) {
return;
}
final ImmutableList<String> order;
try {
order = propertyOrderer.determinePropertyOrder();
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, e.toString());
return;
}
// We expect that all the properties will be found, but if not then we won't try reordering.
boolean allFound = true;
for (RetroWeiboProcessor.Property property : properties) {
allFound &= order.contains(property.getGetter());
}
if (allFound) {
// We successfully found the abstract methods corresponding to all the properties, so now
// reorder the List<Property> to reflect the order of the methods.
Comparator<RetroWeiboProcessor.Property> comparator = new Comparator<RetroWeiboProcessor.Property>() {
@Override
public int compare(RetroWeiboProcessor.Property a, RetroWeiboProcessor.Property b) {
String aName = a.getGetter();
String bName = b.getGetter();
return order.indexOf(aName) - order.indexOf(bName);
}
};
Collections.sort(properties, comparator);
}
}
/**
* Marks a non-empty list representing a cycle of unique values as being seen and returns true
* iff the cycle hasn't been seen before, accounting for logical equivalence of cycles.
*
* For example, the cycle 'a' -> 'b' -> 'c' -> 'a' is represented by the list ['a', 'b', 'c']
* and is logically equivalent to the cycle represented by the list ['b', 'c', 'a'].
*/
public boolean seen(ImmutableList<T> cycle) {
ImmutableSet<T> cycleMembers = ImmutableSet.copyOf(cycle);
Preconditions.checkState(!cycle.isEmpty());
Preconditions.checkState(cycle.size() == cycleMembers.size(),
"cycle doesn't have unique members: " + cycle);
if (knownCyclesByMembers.containsEntry(cycleMembers, cycle)) {
return false;
}
// Of the C cycles, suppose there are D cycles that have the same members (but are in an
// incompatible order). This code path takes O(D * L) time. The common case is that D is
// very small.
boolean found = false;
for (ImmutableList<T> candidateCycle : knownCyclesByMembers.get(cycleMembers)) {
int startPos = candidateCycle.indexOf(cycle.get(0));
// The use of a multimap keyed by cycle members guarantees that the first element of 'cycle'
// is present in 'candidateCycle'.
Preconditions.checkState(startPos >= 0);
if (equalsWithSingleLoopFrom(cycle, candidateCycle, startPos)) {
found = true;
break;
}
}
// We add the cycle even if it's a duplicate so that future exact copies of this can be
// processed in O(L) time. We are already using O(CL) memory, and this optimization doesn't
// change that.
knownCyclesByMembers.put(cycleMembers, cycle);
return !found;
}
@Test
public void testAdditionalLibraryJarsParameterFormatting() {
Path cwd = Paths.get("root");
ImmutableList.Builder<Step> steps = ImmutableList.builder();
ProGuardObfuscateStep.create(
androidPlatformTarget,
JavaCompilationConstants.DEFAULT_JAVA_COMMAND_PREFIX,
new FakeProjectFilesystem(),
/* proguardJarOverride */ Optional.empty(),
"1024M",
Optional.empty(),
/* customProguardConfigs */ ImmutableSet.of(),
ProGuardObfuscateStep.SdkProguardType.DEFAULT,
/* optimizationPasses */ ProGuardObfuscateStep.DEFAULT_OPTIMIZATION_PASSES,
/* proguardJvmArgs */ Optional.empty(),
/* inputAndOutputEntries */ ImmutableMap.of(),
/* additionalLibraryJarsForProguard */ ImmutableSet.of(
Paths.get("myfavorite.jar"), Paths.get("another.jar")),
Paths.get("proguard-directory"),
new FakeBuildableContext(),
FakeBuildContext.NOOP_CONTEXT,
false,
steps);
ProGuardObfuscateStep.CommandLineHelperStep commandLineHelperStep =
(ProGuardObfuscateStep.CommandLineHelperStep) steps.build().get(2);
ImmutableList<String> parameters = commandLineHelperStep.getParameters(cwd);
int libraryJarsArgIndex = parameters.indexOf("-libraryjars");
int libraryJarsValueIndex =
parameters.indexOf("myfavorite.jar" + File.pathSeparatorChar + "another.jar");
assertNotEquals(-1, libraryJarsArgIndex);
assertEquals(libraryJarsValueIndex, libraryJarsArgIndex + 1);
}
private static <T> int position(final T key, final ImmutableMap<T,?> map) {
final ImmutableList<T> keyList = map.keySet().asList();
return keyList.indexOf(key);
}