summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/struct/StructContext.java
blob: ecca95a8e943a5fe3fc49f7fad0ebcb6dee85c1a (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 org.jetbrains.java.decompiler.struct;

import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IDecompilatSaver;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class StructContext {

  // *****************************************************************************
  // private fields
  // *****************************************************************************

  private LazyLoader loader;

  private HashMap<String, StructClass> classes = new HashMap<String, StructClass>();

  private HashMap<String, ContextUnit> units = new HashMap<String, ContextUnit>();

  private IDecompilatSaver saver;

  private IDecompiledData decdata;

  public StructContext(IDecompilatSaver saver, IDecompiledData decdata, LazyLoader loader) {

    this.saver = saver;
    this.decdata = decdata;
    this.loader = loader;

    ContextUnit defaultUnit = new ContextUnit(ContextUnit.TYPE_FOLDER, null, "", true, saver, decdata);
    units.put("", defaultUnit);
  }

  // *****************************************************************************
  // public methods
  // *****************************************************************************

  public StructClass getClass(String name) {
    return classes.get(name);
  }

  public void reloadContext() throws IOException {

    for (ContextUnit unit : units.values()) {

      for (StructClass cl : unit.getClasses()) {
        classes.remove(cl.qualifiedName);
      }

      unit.reload(loader);

      // adjust lobal class collection
      for (StructClass cl : unit.getClasses()) {
        classes.put(cl.qualifiedName, cl);
      }
    }
  }

  public void saveContext() {

    for (ContextUnit unit : units.values()) {
      if (unit.isOwn()) {
        unit.save();
      }
    }
  }

  public void addSpace(File file, boolean isOwn) throws IOException {
    addSpace("", file, isOwn);
  }

  private void addSpace(String path, File file, boolean isOwn) throws IOException {

    if (file.isDirectory()) {

      File[] files = file.listFiles();
      path += "/" + (path.length() == 0 ? "" : file.getName());

      for (int i = files.length - 1; i >= 0; i--) {
        addSpace(path, files[i], isOwn);
      }
    }
    else {

      String filename = file.getName();

      boolean isArchive = false;

      try {
        if (filename.endsWith(".jar")) {
          addArchive(path, file, ContextUnit.TYPE_JAR, isOwn);
          isArchive = true;
        }
        else if (filename.endsWith(".zip")) {
          addArchive(path, file, ContextUnit.TYPE_ZIP, isOwn);
          isArchive = true;
        }
      }
      catch (IOException ex) {
        DecompilerContext.getLogger()
          .writeMessage("Invalid archive file: " + (path.length() > 0 ? path + "/" : "") + filename, IFernflowerLogger.ERROR);
      }

      if (!isArchive) {
        ContextUnit unit = units.get(path);
        if (unit == null) {
          unit = new ContextUnit(ContextUnit.TYPE_FOLDER, null, path, isOwn, saver, decdata);
          units.put(path, unit);
        }

        boolean isClass = false;

        if (filename.endsWith(".class")) {
          try {
            StructClass cl = new StructClass(loader.getClassStream(file.getAbsolutePath(), null), isOwn, loader);

            classes.put(cl.qualifiedName, cl);
            unit.addClass(cl, filename);
            loader.addClassLink(cl.qualifiedName, new LazyLoader.Link(LazyLoader.Link.CLASS, file.getAbsolutePath(), null));

            isClass = true;
          }
          catch (IOException ex) {
            DecompilerContext.getLogger()
              .writeMessage("Invalid class file: " + (path.length() > 0 ? path + "/" : "") + filename, IFernflowerLogger.ERROR);
          }
        }

        if (!isClass) {
          unit.addOtherEntry(file.getAbsolutePath(), filename);
        }
      }
    }
  }


  private void addArchive(String path, File file, int type, boolean isOwn) throws IOException {

    ZipFile archive;

    if (type == ContextUnit.TYPE_JAR) {  // jar
      archive = new JarFile(file);
    }
    else {  // zip
      archive = new ZipFile(file);
    }

    Enumeration<? extends ZipEntry> en = archive.entries();
    while (en.hasMoreElements()) {
      ZipEntry entr = en.nextElement();

      ContextUnit unit = units.get(path + "/" + file.getName());
      if (unit == null) {
        unit = new ContextUnit(type, path, file.getName(), isOwn, saver, decdata);
        if (type == ContextUnit.TYPE_JAR) {
          unit.setManifest(((JarFile)archive).getManifest());
        }
        units.put(path + "/" + file.getName(), unit);
      }

      String name = entr.getName();
      if (!entr.isDirectory()) {
        if (name.endsWith(".class")) {
          StructClass cl = new StructClass(archive.getInputStream(entr), isOwn, loader);
          classes.put(cl.qualifiedName, cl);

          unit.addClass(cl, name);

          if (loader != null) {
            loader.addClassLink(cl.qualifiedName, new LazyLoader.Link(LazyLoader.Link.ENTRY, file.getAbsolutePath(), name));
          }
        }
        else {
          unit.addOtherEntry(file.getAbsolutePath(), name);
        }
      }
      else if (entr.isDirectory()) {
        unit.addDirEntry(name);
      }
    }
  }

  // *****************************************************************************
  // getter and setter methods
  // *****************************************************************************

  public HashMap<String, StructClass> getClasses() {
    return classes;
  }
}