Java tutorial
/* * Copyright 2016-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package com.facebook.buck.query; import com.facebook.buck.model.HasBuildTarget; import com.facebook.buck.rules.BuildTargetSourcePath; import com.facebook.buck.rules.ParamInfo; import com.facebook.buck.rules.PathSourcePath; import com.facebook.buck.rules.SourcePath; import com.facebook.buck.rules.TargetNode; import com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory; import com.facebook.buck.rules.coercer.TypeCoercerFactory; import com.facebook.buck.util.HumanReadableException; import com.facebook.buck.util.ObjectMappers; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import java.lang.reflect.Field; import java.nio.file.Path; public class QueryTargetAccessor { private static final TypeCoercerFactory typeCoercerFactory = new DefaultTypeCoercerFactory( ObjectMappers.newDefaultInstance()); private QueryTargetAccessor() { } public static <T> ImmutableSet<QueryTarget> getTargetsInAttribute(TargetNode<T, ?> node, String attribute) { try { final ImmutableSet.Builder<QueryTarget> builder = ImmutableSortedSet.naturalOrder(); Class<?> constructorArgClass = node.getConstructorArg().getClass(); Field field = constructorArgClass.getField(attribute); ParamInfo info = new ParamInfo(typeCoercerFactory, constructorArgClass, field); info.traverse(value -> { if (value instanceof Path) { builder.add(QueryFileTarget.of((Path) value)); } else if (value instanceof SourcePath) { builder.add(extractSourcePath((SourcePath) value)); } else if (value instanceof HasBuildTarget) { builder.add(extractBuildTargetContainer((HasBuildTarget) value)); } }, node.getConstructorArg()); return builder.build(); } catch (NoSuchFieldException e) { // Ignore if the field does not exist in this rule. return ImmutableSet.of(); } } /** * Filters the objects in the given attribute that satisfy the given predicate. */ public static <T> ImmutableSet<Object> filterAttributeContents(TargetNode<T, ?> node, String attribute, final Predicate<Object> predicate) { try { final ImmutableSet.Builder<Object> builder = ImmutableSet.builder(); Class<?> constructorArgClass = node.getConstructorArg().getClass(); Field field = constructorArgClass.getField(attribute); ParamInfo info = new ParamInfo(typeCoercerFactory, constructorArgClass, field); info.traverse(value -> { if (predicate.apply(value)) { builder.add(value); } }, node.getConstructorArg()); return builder.build(); } catch (NoSuchFieldException e) { // Ignore if the field does not exist in this rule. return ImmutableSet.of(); } } public static QueryTarget extractSourcePath(SourcePath sourcePath) { if (sourcePath instanceof PathSourcePath) { return QueryFileTarget.of(((PathSourcePath) sourcePath).getRelativePath()); } else if (sourcePath instanceof BuildTargetSourcePath) { return QueryBuildTarget.of(((BuildTargetSourcePath) sourcePath).getTarget()); } throw new HumanReadableException("Unsupported source path type: %s", sourcePath.getClass()); } public static QueryTarget extractBuildTargetContainer(HasBuildTarget buildTargetContainer) { return QueryBuildTarget.of(buildTargetContainer.getBuildTarget()); } }