|
| 1 | +/* |
| 2 | + * Copyright (c) 2008, 2019 Emmanuel Dupuy. |
| 3 | + * This project is distributed under the GPLv3 license. |
| 4 | + * This is a Copyleft license that gives the user the right to use, |
| 5 | + * copy and modify the code freely for non-commercial purposes. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.jd.core.v1.service.converter.classfiletojavasyntax.visitor; |
| 9 | + |
| 10 | +import org.jd.core.v1.model.classfile.ClassFile; |
| 11 | +import org.jd.core.v1.model.classfile.ConstantPool; |
| 12 | +import org.jd.core.v1.model.classfile.Method; |
| 13 | +import org.jd.core.v1.model.classfile.attribute.AttributeCode; |
| 14 | +import org.jd.core.v1.model.classfile.constant.ConstantMemberRef; |
| 15 | +import org.jd.core.v1.model.classfile.constant.ConstantNameAndType; |
| 16 | +import org.jd.core.v1.model.javasyntax.AbstractJavaSyntaxVisitor; |
| 17 | +import org.jd.core.v1.model.javasyntax.declaration.*; |
| 18 | +import org.jd.core.v1.model.javasyntax.type.BaseTypeArgument; |
| 19 | +import org.jd.core.v1.model.javasyntax.type.GenericType; |
| 20 | +import org.jd.core.v1.model.javasyntax.type.TypeArguments; |
| 21 | +import org.jd.core.v1.model.javasyntax.type.TypeParameter; |
| 22 | +import org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.declaration.ClassFileBodyDeclaration; |
| 23 | +import org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.declaration.ClassFileConstructorDeclaration; |
| 24 | +import org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.declaration.ClassFileFieldDeclaration; |
| 25 | +import org.jd.core.v1.service.converter.classfiletojavasyntax.util.TypeMaker; |
| 26 | + |
| 27 | +import static org.jd.core.v1.model.classfile.Constants.ACC_STATIC; |
| 28 | + |
| 29 | +public class UpdateOuterFieldTypeVisitor extends AbstractJavaSyntaxVisitor { |
| 30 | + protected TypeMaker typeMaker; |
| 31 | + protected SearchFieldVisitor searchFieldVisitor = new SearchFieldVisitor(); |
| 32 | + |
| 33 | + public UpdateOuterFieldTypeVisitor(TypeMaker typeMaker) { |
| 34 | + this.typeMaker = typeMaker; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void visit(BodyDeclaration declaration) { |
| 39 | + ClassFileBodyDeclaration bodyDeclaration = (ClassFileBodyDeclaration)declaration; |
| 40 | + boolean genericTypesSupported = (bodyDeclaration.getClassFile().getMajorVersion() >= 49); // (majorVersion >= Java 5) |
| 41 | + |
| 42 | + if (genericTypesSupported) { |
| 43 | + safeAcceptListDeclaration(bodyDeclaration.getMethodDeclarations()); |
| 44 | + safeAcceptListDeclaration(bodyDeclaration.getInnerTypeDeclarations()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void visit(ConstructorDeclaration declaration) { |
| 50 | + ClassFileConstructorDeclaration cfcd = (ClassFileConstructorDeclaration) declaration; |
| 51 | + ClassFile classFile = cfcd.getClassFile(); |
| 52 | + |
| 53 | + if ((classFile.getOuterClassFile() != null) && !classFile.matchAccessFlags(ACC_STATIC)) { |
| 54 | + Method method = cfcd.getMethod(); |
| 55 | + byte[] code = method.<AttributeCode>getAttribute("Code").getCode(); |
| 56 | + int offset = 0; |
| 57 | + int opcode = code[offset] & 255; |
| 58 | + |
| 59 | + if (opcode != 42) { // ALOAD_0 |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + opcode = code[++offset] & 255; |
| 64 | + |
| 65 | + if (opcode != 43) { // ALOAD_1 |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + opcode = code[++offset] & 255; |
| 70 | + |
| 71 | + if (opcode != 181) { // PUTFIELD |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + int index = ((code[++offset] & 255) << 8) | (code[++offset] & 255); |
| 76 | + ConstantPool constants = method.getConstants(); |
| 77 | + ConstantMemberRef constantMemberRef = constants.getConstant(index); |
| 78 | + String typeName = constants.getConstantTypeName(constantMemberRef.getClassIndex()); |
| 79 | + ConstantNameAndType constantNameAndType = constants.getConstant(constantMemberRef.getNameAndTypeIndex()); |
| 80 | + String descriptor = constants.getConstantUtf8(constantNameAndType.getDescriptorIndex()); |
| 81 | + TypeMaker.TypeTypes typeTypes = typeMaker.makeTypeTypes(descriptor.substring(1, descriptor.length()-1)); |
| 82 | + |
| 83 | + if (typeTypes.typeParameters != null) { |
| 84 | + String name = constants.getConstantUtf8(constantNameAndType.getNameIndex()); |
| 85 | + searchFieldVisitor.init(name); |
| 86 | + |
| 87 | + for (ClassFileFieldDeclaration field : cfcd.getBodyDeclaration().getFieldDeclarations()) { |
| 88 | + field.getFieldDeclarators().accept(searchFieldVisitor); |
| 89 | + if (searchFieldVisitor.found()) { |
| 90 | + BaseTypeArgument typeArguments; |
| 91 | + |
| 92 | + if (typeTypes.typeParameters.isList()) { |
| 93 | + TypeArguments tas = new TypeArguments(typeTypes.typeParameters.size()); |
| 94 | + for (TypeParameter typeParameter : typeTypes.typeParameters) { |
| 95 | + tas.add(new GenericType(typeParameter.getIdentifier())); |
| 96 | + } |
| 97 | + typeArguments = tas; |
| 98 | + } else { |
| 99 | + typeArguments = new GenericType(typeTypes.typeParameters.getFirst().getIdentifier()); |
| 100 | + } |
| 101 | + |
| 102 | + // Update generic type of outer field reference |
| 103 | + typeMaker.setFieldType(typeName, name, typeTypes.thisType.createType(typeArguments)); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void visit(ClassDeclaration declaration) { |
| 113 | + safeAccept(declaration.getBodyDeclaration()); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void visit(InterfaceDeclaration declaration) { |
| 118 | + safeAccept(declaration.getBodyDeclaration()); |
| 119 | + } |
| 120 | + |
| 121 | + @Override public void visit(AnnotationDeclaration declaration) {} |
| 122 | + @Override public void visit(EnumDeclaration declaration) {} |
| 123 | + |
| 124 | + protected static class SearchFieldVisitor extends AbstractJavaSyntaxVisitor { |
| 125 | + protected String name; |
| 126 | + protected boolean found; |
| 127 | + |
| 128 | + public void init(String name) { |
| 129 | + this.name = name; |
| 130 | + this.found = false; |
| 131 | + } |
| 132 | + |
| 133 | + public boolean found() { |
| 134 | + return found; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void visit(FieldDeclarator declaration) { |
| 139 | + found |= declaration.getName().equals(name); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments