• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jiangxincode / ApkToolBoxGUI / #1148

30 Aug 2025 01:07PM UTC coverage: 3.008% (-0.01%) from 3.018%
#1148

push

jiangxincode
add protolbuf converter with proto

0 of 217 new or added lines in 8 files covered. (0.0%)

1 existing line in 1 file now uncovered.

248 of 8244 relevant lines covered (3.01%)

0.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/jiangxin/apktoolbox/convert/protobuf/supervised/DescriptorCache.java
1
package edu.jiangxin.apktoolbox.convert.protobuf.supervised;
2

3
import com.google.protobuf.DescriptorProtos;
4
import com.google.protobuf.Descriptors;
5
import com.google.protobuf.InvalidProtocolBufferException;
6

7
import java.io.IOException;
8
import java.io.UncheckedIOException;
9
import java.nio.file.Files;
10
import java.nio.file.Path;
11
import java.util.*;
12
import java.util.stream.Stream;
13

14
public final class DescriptorCache {
NEW
15
    private static final Descriptors.FileDescriptor[] DEPENDENCIES = new Descriptors.FileDescriptor[0];
×
16

17
    public static DescriptorCache emptyCache() {
NEW
18
        return new DescriptorCache();
×
19
    }
20

21
    public static DescriptorCache fromDirectory(final Path directory) {
NEW
22
        Objects.requireNonNull(directory);
×
23

NEW
24
        if (!Files.isDirectory(directory)) {
×
NEW
25
            throw new IllegalArgumentException("Path must be a directory: " + directory);
×
26
        }
27

NEW
28
        final DescriptorCache cache = new DescriptorCache();
×
NEW
29
        try (final Stream<Path> walk = Files.walk(directory)) {
×
NEW
30
            walk.filter(Files::isRegularFile)
×
NEW
31
                    .forEach(cache::addDescriptors);
×
NEW
32
        } catch (final IOException e) {
×
NEW
33
            throw new UncheckedIOException(e);
×
NEW
34
        }
×
NEW
35
        return cache;
×
36
    }
37

38
    public static DescriptorCache fromFile(final Path descriptorsFile) {
NEW
39
        Objects.requireNonNull(descriptorsFile);
×
40

NEW
41
        if (!Files.isRegularFile(descriptorsFile)) {
×
NEW
42
            throw new IllegalArgumentException("Path must be a regular file: " + descriptorsFile);
×
43
        }
NEW
44
        final DescriptorCache cache = new DescriptorCache();
×
NEW
45
        cache.addDescriptors(descriptorsFile);
×
NEW
46
        return cache;
×
47
    }
48

NEW
49
    private final Map<String, Descriptors.Descriptor> typeNameToDescriptor = new HashMap<>();
×
50

NEW
51
    private DescriptorCache() {
×
52

NEW
53
    }
×
54

55
    @SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
56
    public Optional<Descriptors.Descriptor> addDescriptor(final Descriptors.Descriptor descriptor) {
NEW
57
        Objects.requireNonNull(descriptor);
×
NEW
58
        final String typeName = Objects.requireNonNull(descriptor.getName());
×
59

NEW
60
        return Optional.ofNullable(typeNameToDescriptor.put(typeName, descriptor));
×
61
    }
62

63
    @SuppressWarnings("WeakerAccess")
64
    public void addDescriptors(final Path descriptorsFile) {
NEW
65
        Objects.requireNonNull(descriptorsFile);
×
66

NEW
67
        if (!Files.isRegularFile(descriptorsFile)) {
×
NEW
68
            throw new IllegalArgumentException("Path must be a regular file: " + descriptorsFile);
×
69
        }
70

71
        try {
NEW
72
            addDescriptors(Files.readAllBytes(descriptorsFile));
×
NEW
73
        } catch (final IOException e) {
×
NEW
74
            throw new UncheckedIOException("While reading: " + descriptorsFile.toAbsolutePath(), e);
×
NEW
75
        }
×
NEW
76
    }
×
77

78
    @SuppressWarnings("WeakerAccess")
79
    public void addDescriptors(final byte[] descriptorsRaw) {
NEW
80
        Objects.requireNonNull(descriptorsRaw);
×
81

82
        try {
NEW
83
            final DescriptorProtos.FileDescriptorSet descriptorSet =
×
NEW
84
                    DescriptorProtos.FileDescriptorSet.parseFrom(descriptorsRaw);
×
NEW
85
            for (final DescriptorProtos.FileDescriptorProto descriptorFile : descriptorSet.getFileList()) {
×
NEW
86
                final Descriptors.FileDescriptor fileDescriptor =
×
NEW
87
                        Descriptors.FileDescriptor.buildFrom(descriptorFile, DescriptorCache.DEPENDENCIES);
×
NEW
88
                for (final Descriptors.Descriptor descriptor : fileDescriptor.getMessageTypes()) {
×
NEW
89
                    addDescriptor(descriptor);
×
NEW
90
                }
×
NEW
91
            }
×
NEW
92
        } catch (final InvalidProtocolBufferException e) {
×
NEW
93
            throw new UncheckedInvalidProtocolBufferException(e);
×
NEW
94
        } catch (final Descriptors.DescriptorValidationException e) {
×
NEW
95
            throw new UncheckedDescriptorValidationException(e);
×
NEW
96
        }
×
NEW
97
    }
×
98

99
    public Optional<Descriptors.Descriptor> getByTypeName(final String typeName) {
NEW
100
        Objects.requireNonNull(typeName);
×
101

NEW
102
        return Optional.ofNullable(typeNameToDescriptor.get(typeName));
×
103
    }
104

105
    public Collection<Descriptors.Descriptor> getDescriptors() {
NEW
106
        return Collections.unmodifiableCollection(typeNameToDescriptor.values());
×
107
    }
108

109
    public Collection<Map.Entry<String, Descriptors.Descriptor>> getEntries() {
NEW
110
        return Collections.unmodifiableCollection(typeNameToDescriptor.entrySet());
×
111
    }
112

113
    public boolean isEmpty() {
NEW
114
        return typeNameToDescriptor.isEmpty();
×
115
    }
116

117
    public int size() {
NEW
118
        return typeNameToDescriptor.size();
×
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc