From d6ccb85505c0d7a0bd6544d67f6d43e40775a76b Mon Sep 17 00:00:00 2001 From: Yohann Roussel Date: Thu, 18 Dec 2014 12:17:52 +0100 Subject: Jack support for annotation processor. Does not include any support for including class files generated by annotation processors. Change-Id: Id4006301d588875f822ff84fa90db10fd616ab00 --- .../src/com/android/sched/util/RunnableHooks.java | 10 -- .../util/codec/InputFileOrDirectoryCodec.java | 84 +++++++++++ .../com/android/sched/util/codec/ListCodec.java | 7 +- .../sched/util/codec/ListParsingException.java | 2 +- .../com/android/sched/util/codec/PairCodec.java | 167 +++++++++++++++++++++ .../util/codec/PairListToMapCodecConverter.java | 60 ++++++++ .../android/sched/util/codec/StringValueCodec.java | 69 +++++++++ .../sched/util/config/id/EnumPropertyId.java | 8 + .../sched/util/config/id/ListPropertyId.java | 2 +- 9 files changed, 396 insertions(+), 13 deletions(-) create mode 100644 sched/src/com/android/sched/util/codec/InputFileOrDirectoryCodec.java create mode 100644 sched/src/com/android/sched/util/codec/PairCodec.java create mode 100644 sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java create mode 100644 sched/src/com/android/sched/util/codec/StringValueCodec.java (limited to 'sched') diff --git a/sched/src/com/android/sched/util/RunnableHooks.java b/sched/src/com/android/sched/util/RunnableHooks.java index c7143ab..07430fc 100644 --- a/sched/src/com/android/sched/util/RunnableHooks.java +++ b/sched/src/com/android/sched/util/RunnableHooks.java @@ -43,15 +43,6 @@ public class RunnableHooks { @Nonnull private final List hooks = new ArrayList(); - public RunnableHooks() { - Runtime.getRuntime().addShutdownHook(new Thread(){ - @Override - public void run() { - runHooks(); - } - }); - } - public synchronized void addHook(@Nonnull Runnable hook) { assert !hooks.contains(hook); @@ -65,7 +56,6 @@ public class RunnableHooks { } public synchronized void runHooks() { - for (Runnable hook : Lists.reverse(hooks)) { hook.run(); } diff --git a/sched/src/com/android/sched/util/codec/InputFileOrDirectoryCodec.java b/sched/src/com/android/sched/util/codec/InputFileOrDirectoryCodec.java new file mode 100644 index 0000000..b735e50 --- /dev/null +++ b/sched/src/com/android/sched/util/codec/InputFileOrDirectoryCodec.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * 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.android.sched.util.codec; + +import com.android.sched.util.config.ConfigurationError; +import com.android.sched.util.file.Directory; +import com.android.sched.util.file.FileOrDirectory; +import com.android.sched.util.file.FileOrDirectory.Existence; +import com.android.sched.util.file.FileOrDirectory.Permission; +import com.android.sched.util.file.InputStreamFile; + +import java.io.File; +import java.io.IOException; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * This {@link StringCodec} is used to create an instance of {@link FileOrDirectory}. + */ +public class InputFileOrDirectoryCodec extends FileOrDirCodec { + + public InputFileOrDirectoryCodec() { + super(Existence.MUST_EXIST, Permission.READ); + } + + @Override + @Nonnull + public FileOrDirectory parseString(@Nonnull CodecContext context, @Nonnull String string) { + try { + return checkString(context, string); + } catch (ParsingException e) { + throw new ConfigurationError(e); + } + } + + @Override + @CheckForNull + public FileOrDirectory checkString(@Nonnull CodecContext context, @Nonnull String string) + throws ParsingException { + File file = new File(string); + try { + if (file.isFile()) { + return new InputStreamFile(string); + } else { + return new Directory(string, context.getRunnableHooks(), existence, permissions, change); + } + } catch (IOException e) { + throw new ParsingException(e.getMessage(), e); + } + + } + + @Override + @Nonnull + public String getUsage() { + return "a path to a file or directory (" + getUsageDetails() + ")"; + } + + @Override + @Nonnull + public String formatValue(@Nonnull FileOrDirectory data) { + return data.getPath(); + } + + @Override + public void checkValue(@Nonnull CodecContext context, @Nonnull FileOrDirectory data) { + } + +} diff --git a/sched/src/com/android/sched/util/codec/ListCodec.java b/sched/src/com/android/sched/util/codec/ListCodec.java index 25f5c75..4544573 100644 --- a/sched/src/com/android/sched/util/codec/ListCodec.java +++ b/sched/src/com/android/sched/util/codec/ListCodec.java @@ -56,13 +56,18 @@ public class ListCodec implements StringCodec> { this.var = var; } - public ListCodec setSeperator(@Nonnull String separator) { + public ListCodec setSeparator(@Nonnull String separator) { this.separator = separator; this.regexp = Pattern.quote(separator); return this; } + @Nonnull + public String getSeparator() { + return separator; + } + public ListCodec setMin(@Nonnegative int min) { assert min < max; assert min >= 0; diff --git a/sched/src/com/android/sched/util/codec/ListParsingException.java b/sched/src/com/android/sched/util/codec/ListParsingException.java index 83d988d..048b07b 100644 --- a/sched/src/com/android/sched/util/codec/ListParsingException.java +++ b/sched/src/com/android/sched/util/codec/ListParsingException.java @@ -30,7 +30,7 @@ public class ListParsingException extends ParsingException { private final int index; public ListParsingException(@Nonnegative int index, @Nonnull String message) { - super(message); + super("element #" + (index + 1) + ": " + message); this.index = index; } diff --git a/sched/src/com/android/sched/util/codec/PairCodec.java b/sched/src/com/android/sched/util/codec/PairCodec.java new file mode 100644 index 0000000..4032bf8 --- /dev/null +++ b/sched/src/com/android/sched/util/codec/PairCodec.java @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * 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.android.sched.util.codec; + +import com.android.sched.util.config.ChainedException.ChainedExceptionBuilder; +import com.android.sched.util.config.ConfigurationError; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * This {@link StringCodec} is used to create an instance of {@link Entry} + */ +// STOPSHIP see https://android-review.googlesource.com/#/c/120553/17 +public class PairCodec implements StringCodec>{ + @Nonnull + private final StringCodec keyParser; + @Nonnull + private final StringCodec valueParser; + + @Nonnull + private String separator = "="; + + public PairCodec(@Nonnull StringCodec keyParser, @Nonnull StringCodec valueParser) { + this.keyParser = keyParser; + this.valueParser = valueParser; + } + + @Nonnull + public PairCodec on(@Nonnull String separator) { + this.separator = separator; + + return this; + } + + @Nonnull + public String getSeparator() { + return separator; + } + + @Override + @Nonnull + public Entry parseString(@Nonnull CodecContext context, + @Nonnull String string) { + int endKey = string.indexOf(separator); + if (endKey == -1) { + throw new ConfigurationError("Missing '" + + separator + "' in '" + string + "'"); + } + String key = string.substring(0, endKey); + String valueString = string.substring(endKey + separator.length()); + return new AbstractMap.SimpleEntry(keyParser.parseString(context, key), + valueParser.parseString(context, valueString)); + } + + @Override + @CheckForNull + public Entry checkString(@Nonnull CodecContext context, + @Nonnull String string) + throws ParsingException { + + ChainedExceptionBuilder exceptions = + new ChainedExceptionBuilder(); + + T keyElement = null; + U valueElement = null; + int endKey = string.indexOf(separator); + if (endKey == -1) { + exceptions.appendException(new ParsingException("Missing '" + + separator + "' in '" + string + "'")); + } else { + String key = string.substring(0, endKey); + String valueString = string.substring(endKey + separator.length()); + try { + keyElement = keyParser.checkString(context, key); + } catch (ParsingException e) { + exceptions.appendException(e); + } + try { + valueElement = valueParser.checkString(context, valueString); + } catch (ParsingException e) { + exceptions.appendException(e); + } + } + + exceptions.throwIfNecessary(); + // If one element is null, do not compute the pair + if (keyElement == null || valueElement == null) { + return null; + } else { + return new AbstractMap.SimpleEntry(keyElement, valueElement); + } + } + + @Override + @Nonnull + public String getUsage() { + StringBuilder sb = new StringBuilder(); + sb.append("" + separator + ""); + sb.append(" where is "); + sb.append(keyParser.getUsage()); + sb.append(" and where is "); + sb.append(valueParser.getUsage()); + + return sb.toString(); + } + + @Override + @Nonnull + public List getValueDescriptions() { + List list = + new ArrayList(); + list.addAll(keyParser.getValueDescriptions()); + list.addAll(valueParser.getValueDescriptions()); + return list; + } + + @Override + @Nonnull + public String formatValue(@Nonnull Entry data) { + StringBuilder sb = new StringBuilder(); + + sb.append(keyParser.formatValue(data.getKey())); + sb.append(separator); + sb.append(valueParser.formatValue(data.getValue())); + return sb.toString(); + } + + @Override + public void checkValue(@Nonnull CodecContext context, @Nonnull Entry data) + throws CheckingException { + ChainedExceptionBuilder exceptions = + new ChainedExceptionBuilder(); + try { + keyParser.checkValue(context, data.getKey()); + } catch (CheckingException e) { + exceptions.appendException(e); + } + try { + valueParser.checkValue(context, data.getValue()); + } catch (CheckingException e) { + exceptions.appendException(e); + } + + exceptions.throwIfNecessary(); + } + +} diff --git a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java new file mode 100644 index 0000000..b9c9e86 --- /dev/null +++ b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * 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.android.sched.util.codec; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import javax.annotation.Nonnull; + +/** + * A {@link ConvertCodec} allowing to convert list of {@link Entry} into a map. + */ +public class PairListToMapCodecConverter extends + ConvertCodec>, Map> { + public PairListToMapCodecConverter(StringCodec>> codec) { + super(codec); + } + + @Override + @Nonnull + public Map parseString(@Nonnull CodecContext context, + @Nonnull String string) { + List> list = codec.parseString(context, string); + Map map = new HashMap(Math.round(list.size() / 0.75f), + 0.75f); + for (Entry entry : list) { + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + + @Override + @Nonnull + public String formatValue(@Nonnull Map data) { + return codec.formatValue(new ArrayList>(data.entrySet())); + } + + @Override + public void checkValue(@Nonnull CodecContext context, @Nonnull Map data) + throws CheckingException { + codec.checkValue(context, new ArrayList>(data.entrySet())); + } +} \ No newline at end of file diff --git a/sched/src/com/android/sched/util/codec/StringValueCodec.java b/sched/src/com/android/sched/util/codec/StringValueCodec.java new file mode 100644 index 0000000..19e13be --- /dev/null +++ b/sched/src/com/android/sched/util/codec/StringValueCodec.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * 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.android.sched.util.codec; + +import java.util.Collections; +import java.util.List; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * A {@link StringCodec} performing no check nor conversion, just providing a usage. + */ +public class StringValueCodec implements StringCodec { + @Nonnull + private final String usage; + + public StringValueCodec(@Nonnull String usage) { + this.usage = usage; + } + + @Override + @Nonnull + public String parseString(@Nonnull CodecContext context, @Nonnull String string) { + return string; + } + + @Override + @CheckForNull + public String checkString(@Nonnull CodecContext context, @Nonnull String string) { + return string; + } + + @Override + @Nonnull + public String getUsage() { + return usage; + } + + @Override + @Nonnull + public List getValueDescriptions() { + return Collections. emptyList(); + } + + @Override + @Nonnull + public String formatValue(@Nonnull String data) { + return data; + } + + @Override + public void checkValue(@Nonnull CodecContext context, @Nonnull String data) { + } +} \ No newline at end of file diff --git a/sched/src/com/android/sched/util/config/id/EnumPropertyId.java b/sched/src/com/android/sched/util/config/id/EnumPropertyId.java index c209e3c..d98baef 100644 --- a/sched/src/com/android/sched/util/config/id/EnumPropertyId.java +++ b/sched/src/com/android/sched/util/config/id/EnumPropertyId.java @@ -20,6 +20,7 @@ import com.android.sched.util.codec.EnumCodec; import com.android.sched.util.config.ConfigChecker; import com.android.sched.util.config.MissingPropertyException; import com.android.sched.util.config.PropertyIdException; +import com.android.sched.util.config.category.Category; import com.android.sched.util.config.expression.BooleanExpression; import javax.annotation.Nonnull; @@ -85,6 +86,13 @@ public class EnumPropertyId> extends PropertyId { return this; } + @Override + @Nonnull + public EnumPropertyId withCategory(@Nonnull Category category) { + super.withCategory(category); + return this; + } + @Nonnull public BooleanExpression is(@Nonnull final T enumValue) { return new BooleanExpression() { diff --git a/sched/src/com/android/sched/util/config/id/ListPropertyId.java b/sched/src/com/android/sched/util/config/id/ListPropertyId.java index 075fa00..fe4e032 100644 --- a/sched/src/com/android/sched/util/config/id/ListPropertyId.java +++ b/sched/src/com/android/sched/util/config/id/ListPropertyId.java @@ -80,7 +80,7 @@ public class ListPropertyId extends PropertyId> { @Nonnull public ListPropertyId on(@Nonnull String separator) { - getCodec().setSeperator(separator); + getCodec().setSeparator(separator); return this; } -- cgit v1.1