Codebase list jd-gui / 0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream services / src / main / java / org / jd / gui / service / type / JavaFileTypeFactoryProvider.java
0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream

Tree @0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream (Download .tar.gz)

JavaFileTypeFactoryProvider.java @0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstreamraw · history · blame

  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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (c) 2008-2019 Emmanuel Dupuy.
 * This project is distributed under the GPLv3 license.
 * This is a Copyleft license that gives the user the right to use,
 * copy and modify the code freely for non-commercial purposes.
 */

package org.jd.gui.service.type;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jd.gui.api.API;
import org.jd.gui.api.model.Container;
import org.jd.gui.api.model.Type;
import org.jd.gui.util.exception.ExceptionUtil;
import org.jd.gui.util.parser.antlr.ANTLRJavaParser;
import org.jd.gui.util.parser.antlr.AbstractJavaListener;
import org.jd.gui.util.parser.antlr.JavaParser;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.*;

public class JavaFileTypeFactoryProvider extends AbstractTypeFactoryProvider {

    static {
        // Early class loading
        ANTLRJavaParser.parse(new ANTLRInputStream("class EarlyLoading{}"), new Listener(null));
    }

    // Create cache
    protected Cache<URI, Listener> cache = new Cache<>();

    @Override public String[] getSelectors() { return appendSelectors("*:file:*.java"); }

    @Override
    public Collection<Type> make(API api, Container.Entry entry) {
        Listener listener = getListener(entry);

        if (listener == null) {
            return Collections.emptyList();
        } else {
            return listener.getRootTypes();
        }
    }

    @Override
    public Type make(API api, Container.Entry entry, String fragment) {
        Listener listener = getListener(entry);

        if (listener == null) {
            return null;
        } else {
            if ((fragment != null) && (fragment.length() > 0)) {
                // Search type name in fragment. URI format : see jd.gui.api.feature.UriOpener
                int index = fragment.indexOf('-');

                if (index != -1) {
                    // Keep type name only
                    fragment = fragment.substring(0, index);
                }

                return listener.getType(fragment);
            } else {
                return listener.getMainType();
            }
        }
    }

    protected Listener getListener(Container.Entry entry) {
        URI key = entry.getUri();

        if (cache.containsKey(key)) {
            return cache.get(key);
        } else {
            Listener listener;

            try (InputStream inputStream = entry.getInputStream()) {
                ANTLRJavaParser.parse(new ANTLRInputStream(inputStream), listener = new Listener(entry));
            } catch (IOException e) {
                assert ExceptionUtil.printStackTrace(e);
                listener = null;
            }

            cache.put(key, listener);
            return listener;
        }
    }

    protected static class JavaType implements Type {
        protected int access;
        protected String name;
        protected String superName;
        protected String outerName;

        protected String displayTypeName;
        protected String displayInnerTypeName;
        protected String displayPackageName;

        protected List<Type> innerTypes = new ArrayList<>();
        protected List<Field> fields = new ArrayList<>();
        protected List<Method> methods = new ArrayList<>();

        protected JavaType outerType;

        public JavaType(
                int access, String name, String superName, String outerName,
                String displayTypeName, String displayInnerTypeName, String displayPackageName,
                JavaType outerType) {

            this.access = access;
            this.name = name;
            this.superName = superName;
            this.outerName = outerName;
            this.displayTypeName = displayTypeName;
            this.displayInnerTypeName = displayInnerTypeName;
            this.displayPackageName = displayPackageName;
            this.outerType = outerType;
        }

        public int getFlags() { return access; }
        public String getName() { return name; }
        public String getSuperName() { return superName; }
        public String getOuterName() { return outerName; }
        public String getDisplayTypeName() { return displayTypeName; }
        public String getDisplayInnerTypeName() { return displayInnerTypeName; }
        public String getDisplayPackageName() { return displayPackageName; }
        public Icon getIcon() { return getTypeIcon(access); }
        public JavaType getOuterType() { return outerType; }
        public Collection<Type> getInnerTypes() { return innerTypes; }
        public Collection<Field> getFields() { return fields; }
        public Collection<Method> getMethods() { return methods; }
    }

    protected static class JavaField implements Type.Field {
        protected int access;
        protected String name;
        protected String descriptor;

        public JavaField(int access, String name, String descriptor) {
            this.access = access;
            this.name = name;
            this.descriptor = descriptor;
        }

        public int getFlags() { return access; }
        public String getName() { return name; }
        public String getDescriptor() { return descriptor; }
        public Icon getIcon() { return getFieldIcon(access); }

        public String getDisplayName() {
            StringBuilder sb = new StringBuilder();
            sb.append(name).append(" : ");
            writeSignature(sb, descriptor, descriptor.length(), 0, false);
            return sb.toString();
        }
    }

    protected static class JavaMethod implements Type.Method {
        protected JavaType type;
        protected int access;
        protected String name;
        protected String descriptor;

        public JavaMethod(JavaType type, int access, String name, String descriptor) {
            this.type = type;
            this.access = access;
            this.name = name;
            this.descriptor = descriptor;
        }

        public int getFlags() { return access; }
        public String getName() { return name; }
        public String getDescriptor() { return descriptor; }
        public Icon getIcon() { return getMethodIcon(access); }

        public String getDisplayName() {
            String constructorName = type.getDisplayInnerTypeName();
            boolean isInnerClass = (constructorName != null);

            if (constructorName == null)
                constructorName = type.getDisplayTypeName();

            StringBuilder sb = new StringBuilder();
            writeMethodSignature(sb, access, access, isInnerClass, constructorName, name, descriptor);
            return sb.toString();
        }
    }

    protected static class Listener extends AbstractJavaListener {

        protected String displayPackageName = "";

        protected JavaType mainType = null;
        protected JavaType currentType = null;
        protected ArrayList<Type> rootTypes = new ArrayList<>();
        protected HashMap<String, Type> types = new HashMap<>();

        public Listener(Container.Entry entry) {
            super(entry);
        }

        public Type getMainType() {
            return mainType;
        }
        public Type getType(String typeName) {
            return types.get(typeName);
        }
        public ArrayList<Type> getRootTypes() {
            return rootTypes;
        }

        // --- ANTLR Listener --- //

        public void enterPackageDeclaration(JavaParser.PackageDeclarationContext ctx) {
            super.enterPackageDeclaration(ctx);
            displayPackageName = packageName.replace('/', '.');
        }

        public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) { enterTypeDeclaration(ctx, 0); }
        public void exitClassDeclaration(JavaParser.ClassDeclarationContext ctx) { exitTypeDeclaration(); }

        public void enterEnumDeclaration(JavaParser.EnumDeclarationContext ctx) { enterTypeDeclaration(ctx, JavaType.FLAG_ENUM); }
        public void exitEnumDeclaration(JavaParser.EnumDeclarationContext ctx) { exitTypeDeclaration(); }

        public void enterInterfaceDeclaration(JavaParser.InterfaceDeclarationContext ctx) { enterTypeDeclaration(ctx, JavaType.FLAG_INTERFACE); }
        public void exitInterfaceDeclaration(JavaParser.InterfaceDeclarationContext ctx) { exitTypeDeclaration(); }

        public void enterAnnotationTypeDeclaration(JavaParser.AnnotationTypeDeclarationContext ctx) { enterTypeDeclaration(ctx, JavaType.FLAG_ANNOTATION); }
        public void exitAnnotationTypeDeclaration(JavaParser.AnnotationTypeDeclarationContext ctx) { exitTypeDeclaration(); }

        protected void enterTypeDeclaration(ParserRuleContext ctx, int access) {
            String name = ctx.getToken(JavaParser.Identifier, 0).getText();

            JavaParser.TypeContext superType = ctx.getRuleContext(JavaParser.TypeContext.class, 0);
            String superQualifiedTypeName;

            if (superType == null) {
                superQualifiedTypeName = ((access & JavaType.FLAG_INTERFACE) == 0) ? "java/lang/Object" : "";
            } else {
                superQualifiedTypeName = resolveInternalTypeName(superType.classOrInterfaceType().Identifier());
            }

            ParserRuleContext parent = ctx.getParent();

            if (parent instanceof JavaParser.TypeDeclarationContext)
                access += getTypeDeclarationContextAccessFlag(parent);
            else if (parent instanceof JavaParser.MemberDeclarationContext)
                access += getMemberDeclarationContextAccessFlag(parent.getParent());

            if (currentType == null) {
                String internalTypeName = packageName.isEmpty() ? name : packageName + "/" + name;
                String outerName = null;
                String displayTypeName = name;
                String displayInnerTypeName = null;

                currentType = new JavaType(access, internalTypeName, superQualifiedTypeName, outerName, displayTypeName, displayInnerTypeName, displayPackageName, null);
                types.put(internalTypeName, currentType);
                rootTypes.add(currentType);
                nameToInternalTypeName.put(name, internalTypeName);

                if (mainType == null) {
                    mainType = currentType;
                } else {
                    // Multi class definitions in the same file
                    String path = entry.getPath();
                    int index = path.lastIndexOf('/') + 1;

                    if (path.substring(index).startsWith(name + '.')) {
                        // Select the correct root type
                        mainType = currentType;
                    }
                }
            } else {
                String internalTypeName = currentType.getName() + '$' + name;
                String outerName = currentType.getName();
                String displayTypeName = currentType.getDisplayTypeName() + '.' + name;
                String displayInnerTypeName = name;
                JavaType subType = new JavaType(access, internalTypeName, superQualifiedTypeName, outerName, displayTypeName, displayInnerTypeName, displayPackageName, currentType);

                currentType.getInnerTypes().add(subType);
                currentType = subType;
                types.put(internalTypeName, currentType);
                nameToInternalTypeName.put(name, internalTypeName);
            }
        }

        protected void exitTypeDeclaration() {
            currentType = currentType.getOuterType();
        }

        public void enterClassBodyDeclaration(JavaParser.ClassBodyDeclarationContext ctx) {
            if (ctx.getChildCount() == 2) {
                ParseTree first = ctx.getChild(0);

                if (first instanceof TerminalNode) {
                    if (((TerminalNode)first).getSymbol().getType() == JavaParser.STATIC) {
                        currentType.getMethods().add(new JavaMethod(currentType, JavaType.FLAG_STATIC, "<clinit>", "()V"));
                    }
                }
            }
        }

        public void enterConstDeclaration(JavaParser.ConstDeclarationContext ctx) {
            JavaParser.TypeContext typeContext = ctx.type();
            int access = getClassBodyDeclarationAccessFlag(ctx.getParent().getParent());

            for (JavaParser.ConstantDeclaratorContext constantDeclaratorContext : ctx.constantDeclarator()) {
                TerminalNode identifier = constantDeclaratorContext.Identifier();
                String name = identifier.getText();
                int dimensionOnVariable = countDimension(constantDeclaratorContext.children);
                String descriptor = createDescriptor(typeContext, dimensionOnVariable);

                currentType.getFields().add(new JavaField(access, name, descriptor));
            }
        }

        public void enterFieldDeclaration(JavaParser.FieldDeclarationContext ctx) {
            JavaParser.TypeContext typeContext = ctx.type();
            int access = getClassBodyDeclarationAccessFlag(ctx.getParent().getParent());

            for (JavaParser.VariableDeclaratorContext declaration : ctx.variableDeclarators().variableDeclarator()) {
                JavaParser.VariableDeclaratorIdContext variableDeclaratorId = declaration.variableDeclaratorId();
                TerminalNode identifier = variableDeclaratorId.Identifier();
                String name = identifier.getText();
                int dimensionOnVariable = countDimension(variableDeclaratorId.children);
                String descriptor = createDescriptor(typeContext, dimensionOnVariable);

                currentType.getFields().add(new JavaField(access, name, descriptor));
            }
        }

        public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
            enterMethodDeclaration(ctx, ctx.Identifier(), ctx.formalParameters(), ctx.type());
        }

        public void enterInterfaceMethodDeclaration(JavaParser.InterfaceMethodDeclarationContext ctx) {
            enterMethodDeclaration(ctx, ctx.Identifier(), ctx.formalParameters(), ctx.type());
        }

        public void enterMethodDeclaration(
                ParserRuleContext ctx, TerminalNode identifier,
                JavaParser.FormalParametersContext formalParameters, JavaParser.TypeContext returnType) {

            int access = getClassBodyDeclarationAccessFlag(ctx.getParent().getParent());
            String name = identifier.getText();
            String paramDescriptors = createParamDescriptors(formalParameters.formalParameterList());
            String returnDescriptor = createDescriptor(returnType, 0);
            String descriptor = paramDescriptors + returnDescriptor;

            currentType.getMethods().add(new JavaMethod(currentType, access, name, descriptor));
        }

        public void enterConstructorDeclaration(JavaParser.ConstructorDeclarationContext ctx) {
            int access = getClassBodyDeclarationAccessFlag(ctx.getParent().getParent());
            String paramDescriptors = createParamDescriptors(ctx.formalParameters().formalParameterList());
            String descriptor = paramDescriptors + "V";

            currentType.getMethods().add(new JavaMethod(currentType, access, "<init>", descriptor));
        }

        protected String createParamDescriptors(JavaParser.FormalParameterListContext formalParameterList) {
            StringBuilder paramDescriptors = null;

            if (formalParameterList != null) {
                List<JavaParser.FormalParameterContext> formalParameters = formalParameterList.formalParameter();
                paramDescriptors = new StringBuilder("(");

                for (JavaParser.FormalParameterContext formalParameter : formalParameters) {
                    int dimensionOnParameter = countDimension(formalParameter.variableDeclaratorId().children);
                    paramDescriptors.append(createDescriptor(formalParameter.type(), dimensionOnParameter));
                }
            }

            return (paramDescriptors == null) ? "()" : paramDescriptors.append(')').toString();
        }

        protected int getTypeDeclarationContextAccessFlag(ParserRuleContext ctx) {
            int access = 0;

            for (JavaParser.ClassOrInterfaceModifierContext coiModifierContext : ctx.getRuleContexts(JavaParser.ClassOrInterfaceModifierContext.class)) {
                access += getAccessFlag(coiModifierContext);
            }

            return access;
        }

        protected int getMemberDeclarationContextAccessFlag(ParserRuleContext ctx) {
            int access = 0;

            for (JavaParser.ModifierContext modifierContext : ctx.getRuleContexts(JavaParser.ModifierContext.class)) {
                JavaParser.ClassOrInterfaceModifierContext coiModifierContext = modifierContext.classOrInterfaceModifier();
                if (coiModifierContext != null) {
                    access += getAccessFlag(coiModifierContext);
                }
            }

            return access;
        }

        protected int getClassBodyDeclarationAccessFlag(ParserRuleContext ctx) {
            if ((currentType.access & JavaType.FLAG_INTERFACE) == 0) {
                int access = 0;

                for (JavaParser.ModifierContext modifierContext : ctx.getRuleContexts(JavaParser.ModifierContext.class)) {
                    JavaParser.ClassOrInterfaceModifierContext coimc = modifierContext.classOrInterfaceModifier();

                    if (coimc != null) {
                        access += getAccessFlag(coimc);
                    }
                }

                return access;
            } else {
                return JavaType.FLAG_PUBLIC;
            }
        }

        protected int getAccessFlag(JavaParser.ClassOrInterfaceModifierContext ctx) {
            if (ctx.getChildCount() == 1) {
                ParseTree first = ctx.getChild(0);

                if (first instanceof TerminalNode) {
                    switch (((TerminalNode)first).getSymbol().getType()) {
                        case JavaParser.STATIC:    return JavaType.FLAG_STATIC;
                        case JavaParser.FINAL:     return JavaType.FLAG_FINAL;
                        case JavaParser.ABSTRACT:  return JavaType.FLAG_ABSTRACT;
                        case JavaParser.PUBLIC:    return JavaType.FLAG_PUBLIC;
                        case JavaParser.PROTECTED: return JavaType.FLAG_PROTECTED;
                        case JavaParser.PRIVATE:   return JavaType.FLAG_PRIVATE;
                    }
                }
            }

            return 0;
        }
    }
}