summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/jayce/JaycePackageLoader.java
blob: 29f17d3a2da32b5fda16ebb6bc823f6d32c4f120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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.jack.jayce;

import com.android.jack.Jack;
import com.android.jack.JackAbortException;
import com.android.jack.LibraryException;
import com.android.jack.backend.jayce.JayceFileImporter;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JPackage;
import com.android.jack.ir.ast.JPackageLookupException;
import com.android.jack.ir.ast.MissingJTypeLookupException;
import com.android.jack.library.FileType;
import com.android.jack.library.HasInputLibrary;
import com.android.jack.library.InputJackLibrary;
import com.android.jack.library.InputLibrary;
import com.android.jack.library.LibraryReadingException;
import com.android.jack.load.PackageLoader;
import com.android.jack.lookup.JPhantomLookup;
import com.android.jack.reporting.Reporter.Severity;
import com.android.jack.util.collect.UnmodifiableCollections;
import com.android.sched.util.codec.VariableName;
import com.android.sched.util.location.Location;
import com.android.sched.util.log.LoggerFactory;
import com.android.sched.vfs.InputVDir;
import com.android.sched.vfs.InputVElement;
import com.android.sched.vfs.InputVFile;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

/**
 * {@link PackageLoader} for package containing classes defined in Jayce files.
 */
@VariableName("loader")
public class JaycePackageLoader implements PackageLoader, HasInputLibrary {

  @Nonnull
  private static Logger logger = LoggerFactory.getLogger();

  @Nonnull
  private final InputVDir packageVDir;

  @Nonnull
  private final JPhantomLookup lookup;

  @Nonnull
  private final NodeLevel defaultLoadLevel;

  @Nonnull
  private final InputJackLibrary inputJackLibrary;

  @Nonnull
  private final Map<String, InputVDir> vdirCache = new HashMap<String, InputVDir>();

  @Nonnull
  private final Map<String, InputVFile> jayceFileCache = new HashMap<String, InputVFile>();

  @Nonnull
  private final UnmodifiableCollections collections = Jack.getUnmodifiableCollections();

  JaycePackageLoader(@Nonnull InputJackLibrary inputJackLibrary,
      @Nonnull InputVDir packageVDir, @Nonnull JPhantomLookup lookup,
      @Nonnull NodeLevel defaultLoadLevel) {
    assert inputJackLibrary.containsFileType(FileType.JAYCE);
    this.inputJackLibrary = inputJackLibrary;
    this.packageVDir = packageVDir;
    this.lookup = lookup;
    this.defaultLoadLevel = defaultLoadLevel;
    for (InputVElement sub : packageVDir.list()) {
      String name = sub.getName();
      if (sub.isVDir()) {
        vdirCache.put(name, (InputVDir) sub);
      } else if (JayceFileImporter.isJackFileName(name)) {
        jayceFileCache.put(
            name.substring(0, name.length() - JayceFileImporter.JACK_EXTENSION_LENGTH),
            (InputVFile) sub);
      }
    }
  }

  @Override
  @Nonnull
  public JDefinedClassOrInterface loadClassOrInterface(
      @Nonnull JPackage loading, @Nonnull String simpleName) throws MissingJTypeLookupException {
    InputVFile inputVFile = jayceFileCache.get(simpleName);

    if (inputVFile == null) {
      throw new MissingJTypeLookupException(loading, simpleName);
    }

    try {
      return new JayceClassOrInterfaceLoader(inputJackLibrary,
          loading,
          simpleName,
          inputVFile,
          lookup,
          defaultLoadLevel).load();
    } catch (LibraryException e) {
      LibraryReadingException reportable = new LibraryReadingException(e);
      Jack.getSession().getReporter().report(Severity.FATAL, reportable);
      throw new JackAbortException(reportable);
    }
  }

  @Override
  @Nonnull
  public Collection<String> getSubClassNames(@Nonnull JPackage loading) {
    return collections.getUnmodifiableCollection(jayceFileCache.keySet());
  }

  @Nonnull
  @Override
  public PackageLoader getLoaderForSubPackage(@Nonnull JPackage loading,
      @Nonnull String simpleName) throws JPackageLookupException {
    InputVDir input = vdirCache.get(simpleName);

    if (input == null) {
      throw new JPackageLookupException(simpleName, loading);
    }

    return new JaycePackageLoader(inputJackLibrary, input, lookup, defaultLoadLevel);
  }

  @Nonnull
  @Override
  public Collection<String> getSubPackageNames(@Nonnull JPackage loading) {
    return collections.getUnmodifiableCollection(vdirCache.keySet());
  }

  @Override
  @Nonnull
  public Location getLocation(@Nonnull JPackage loaded) {
      return packageVDir.getLocation();
  }

  @Override
  public boolean isOnPath(@Nonnull JPackage loaded) {
    return true;
  }

  @Override
  @Nonnull
  public InputLibrary getInputLibrary() {
    return inputJackLibrary;
  }
}