@SuppressWarnings("UnusedParameters") private AstBuilder createAstBuilder( final DecompilationOptions options, final TypeDefinition currentType, final boolean isSingleMember) { final DecompilerSettings settings = options.getSettings(); final DecompilerContext context = new DecompilerContext(); context.setCurrentType(currentType); context.setSettings(settings); return new AstBuilder(context); }
private AstBuilder buildAst(final TypeDefinition type, final DecompilationOptions options) { final AstBuilder builder = createAstBuilder(options, type, false); builder.addType(type); runTransforms(builder, options, null); return builder; }
static void addTypeArguments(final TypeReference type, final AstType astType) { if (type.hasGenericParameters()) { final List<GenericParameter> genericParameters = type.getGenericParameters(); final int count = genericParameters.size(); final AstType[] typeArguments = new AstType[count]; for (int i = 0; i < count; i++) { final GenericParameter genericParameter = genericParameters.get(i); final SimpleType typeParameter = new SimpleType(genericParameter.getName()); typeParameter.putUserData(Keys.TYPE_REFERENCE, genericParameter); typeArguments[i] = typeParameter; } applyTypeArguments(astType, ArrayUtilities.asUnmodifiableList(typeArguments)); } }
private void addTypeMembers(final TypeDeclaration astType, final TypeDefinition type) { for (final FieldDefinition field : type.getDeclaredFields()) { astType.addChild(createField(field), Roles.TYPE_MEMBER); } for (final MethodDefinition method : type.getDeclaredMethods()) { if (method.isConstructor()) { astType.addChild(createConstructor(method), Roles.TYPE_MEMBER); } else { astType.addChild(createMethod(method), Roles.TYPE_MEMBER); } } final List<TypeDefinition> nestedTypes = new ArrayList<>(); for (final TypeDefinition nestedType : type.getDeclaredTypes()) { final TypeReference declaringType = nestedType.getDeclaringType(); if (!nestedType.isLocalClass() && type.isEquivalentTo(declaringType)) { if (nestedType.isAnonymous()) { _typeDeclarations.put(type.getInternalName(), new SoftReference<>(astType)); } else { nestedTypes.add(nestedType); } } } sortNestedTypes(nestedTypes); for (final TypeDefinition nestedType : nestedTypes) { astType.addChild(createTypeNoCache(nestedType), Roles.TYPE_MEMBER); } }
public final List<ParameterDeclaration> createParameters(final Iterable<ParameterDefinition> parameters) { final List<ParameterDeclaration> declarations = new ArrayList<>(); for (final ParameterDefinition p : parameters) { final TypeReference type = p.getParameterType(); final AstType astType = convertType(type); final ParameterDeclaration d = new ParameterDeclaration(p.getName(), astType); d.putUserData(Keys.PARAMETER_DEFINITION, p); for (final CustomAnnotation annotation : p.getAnnotations()) { d.getAnnotations().add(createAnnotation(annotation)); } declarations.add(d); if (p.isFinal()) { EntityDeclaration.addModifier(d, Modifier.FINAL); } } return Collections.unmodifiableList(declarations); }
private ConstructorDeclaration createConstructor(final MethodDefinition method) { final ConstructorDeclaration astMethod = new ConstructorDeclaration(); EntityDeclaration.setModifiers( astMethod, Flags.asModifierSet(scrubAccessModifiers(method.getFlags() & Flags.ConstructorFlags)) ); astMethod.setName(method.getDeclaringType().getName()); astMethod.getParameters().addAll(createParameters(method.getParameters())); astMethod.setBody(createMethodBody(method, astMethod.getParameters())); astMethod.putUserData(Keys.METHOD_DEFINITION, method); astMethod.putUserData(Keys.MEMBER_REFERENCE, method); for (final TypeReference thrownType : method.getThrownTypes()) { astMethod.addChild(convertType(thrownType), Roles.THROWN_TYPE); } return astMethod; }
modifiers = Flags.asModifierSet(scrubAccessModifiers(method.getFlags() & Flags.MethodFlags)); astMethod.getParameters().addAll(createParameters(method.getParameters())); astMethod.getTypeParameters().addAll(createTypeParameters(method.getGenericParameters())); astMethod.setReturnType(convertType(method.getReturnType())); astMethod.putUserData(Keys.METHOD_DEFINITION, method); astMethod.putUserData(Keys.MEMBER_REFERENCE, method); astMethod.getDeclaredTypes().add(createType(declaredType)); astMethod.setBody(createMethodBody(method, astMethod.getParameters())); astMethod.addChild(convertType(thrownType), Roles.THROWN_TYPE); astMethod.getAnnotations().add(createAnnotation(annotation)); final Expression defaultValue = createAnnotationElement(defaultAttribute.getDefaultValue());
Flags.asModifierSet(scrubAccessModifiers(flags)) ); final List<TypeParameterDeclaration> typeParameters = createTypeParameters(type.getGenericParameters()); astType.addChild(convertType(baseType), Roles.BASE_TYPE); continue; astType.addChild(convertType(interfaceType), Roles.IMPLEMENTED_INTERFACE); astType.getAnnotations().add(createAnnotation(annotation)); addTypeMembers(astType, type);
private ConstructorDeclaration createConstructor(final MethodDefinition method) { final ConstructorDeclaration astMethod = new ConstructorDeclaration(); EntityDeclaration.setModifiers( astMethod, Flags.asModifierSet(scrubAccessModifiers(method.getFlags() & Flags.ConstructorFlags)) ); astMethod.setName(method.getDeclaringType().getName()); astMethod.getParameters().addAll(createParameters(method.getParameters())); astMethod.getTypeParameters().addAll(createTypeParameters(method.getGenericParameters())); astMethod.setBody(createMethodBody(method, astMethod.getParameters())); astMethod.putUserData(Keys.METHOD_DEFINITION, method); astMethod.putUserData(Keys.MEMBER_REFERENCE, method); for (final CustomAnnotation annotation : method.getAnnotations()) { astMethod.getAnnotations().add(createAnnotation(annotation)); } for (final TypeReference thrownType : method.getThrownTypes()) { astMethod.addChild(convertType(thrownType), Roles.THROWN_TYPE); } return astMethod; }
@Override public AstNode apply(final AstNode input) { return new CastExpression(astBuilder.convertType(BuiltinTypes.Object), right); } }
private FieldDeclaration createField(final FieldDefinition field) { final FieldDeclaration astField = new FieldDeclaration(); final VariableInitializer initializer = new VariableInitializer(field.getName()); astField.setName(field.getName()); astField.addChild(initializer, Roles.VARIABLE); astField.setReturnType(convertType(field.getFieldType())); astField.putUserData(Keys.FIELD_DEFINITION, field); astField.putUserData(Keys.MEMBER_REFERENCE, field); EntityDeclaration.setModifiers( astField, Flags.asModifierSet(scrubAccessModifiers(field.getFlags() & Flags.VarFlags)) ); if (field.hasConstantValue()) { initializer.setInitializer(new PrimitiveExpression(Expression.MYSTERY_OFFSET, field.getConstantValue())); initializer.putUserData(Keys.FIELD_DEFINITION, field); initializer.putUserData(Keys.MEMBER_REFERENCE, field); } for (final CustomAnnotation annotation : field.getAnnotations()) { astField.getAnnotations().add(createAnnotation(annotation)); } return astField; }
return new TypeReferenceExpression(Expression.MYSTERY_OFFSET, convertType(enumElement.getEnumType())).member(enumElement.getEnumConstantName()); elements.add(createAnnotationElement(e)); return new ClassOfExpression( Expression.MYSTERY_OFFSET, convertType(((ClassAnnotationElement) element).getClassType()) ); return createAnnotation(((AnnotationAnnotationElement) element).getAnnotation());
final Object operand = byteCode.getOperand(); final Label label = operand instanceof Label ? (Label) operand : null; final AstType operandType = operand instanceof TypeReference ? _astBuilder.convertType((TypeReference) operand) : AstType.NULL; final Variable variableOperand = operand instanceof Variable ? (Variable) operand : null; final FieldReference fieldOperand = operand instanceof FieldReference ? (FieldReference) operand : null; return new CastExpression(_astBuilder.convertType(BuiltinTypes.Long), arg1); case I2F: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Float), arg1); case I2D: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Double), arg1); case L2I: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Integer), arg1); case L2F: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Float), arg1); case L2D: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Double), arg1); case F2I: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Integer), arg1); case F2L: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Long), arg1); case F2D: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Double), arg1); case D2I: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Integer), arg1); case D2L: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Long), arg1); case D2F: return new CastExpression(_astBuilder.convertType(BuiltinTypes.Float), arg1);
astBuilder = new AstBuilder(context); final TypeDeclaration declaration = astBuilder.createType(resolvedType);
return convertType(type.getElementType(), typeIndex.increment(), options).makeArrayType(); if (!options.getAllowWildcards()) { if (type.hasExtendsBound()) { return convertType(type.getExtendsBound(), options); return convertType(BuiltinTypes.Object, options); wildcardType.addChild(convertType(type.getExtendsBound()), Roles.EXTENDS_BOUND); wildcardType.addChild(convertType(type.getSuperBound()), Roles.SUPER_BOUND); baseType = convertType( (TypeReference) genericInstance.getGenericDefinition(), typeIndex.increment(), typeArguments.add(convertType(typeArgument, typeIndex.increment(), options)); applyTypeArguments(baseType, typeArguments); baseType.putUserData(Keys.TYPE_REFERENCE, type); current = current.getDeclaringType(); if (isContextWithinType(current)) { break;
return convertType(type.getElementType(), typeIndex.increment(), options).makeArrayType(); if (!options.getAllowWildcards()) { if (type.hasExtendsBound()) { return convertType(type.getExtendsBound(), options); return convertType(BuiltinTypes.Object, options); wildcardType.addChild(convertType(type.getExtendsBound()), Roles.EXTENDS_BOUND); wildcardType.addChild(convertType(type.getSuperBound()), Roles.SUPER_BOUND); baseType = convertType( (TypeReference) genericInstance.getGenericDefinition(), typeIndex.increment(), typeArguments.add(convertType(typeArgument, typeIndex.increment(), options)); applyTypeArguments(baseType, typeArguments); baseType.putUserData(Keys.TYPE_REFERENCE, type); current = current.getDeclaringType(); if (isContextWithinType(current)) { break; if (options.getAddImports() && !areImportsSuppressed() && !_typeDeclarations.containsKey(typeToImport.getInternalName())) { String importedName = _unqualifiedTypeNames.get(typeToImport.getSimpleName());
public Annotation createAnnotation(final CustomAnnotation annotation) { final Annotation a = new Annotation(); final AstNodeCollection<Expression> arguments = a.getArguments(); a.setType(convertType(annotation.getAnnotationType())); final List<AnnotationParameter> parameters = annotation.getParameters(); for (final AnnotationParameter p : parameters) { final String member = p.getMember(); final Expression value = createAnnotationElement(p.getValue()); if (StringUtilities.isNullOrEmpty(member) || parameters.size() == 1 && "value".equals(member)) { arguments.add(value); } else { arguments.add(new AssignmentExpression(new IdentifierExpression(value.getOffset(), member), value)); } } return a; }
final TypeDefinition currentType = astBuilder.getContext().getCurrentType(); final IMetadataResolver resolver = currentType != null ? currentType.getResolver() : MetadataSystem.instance(); final MetadataParser parser = new MetadataParser(resolver); new ObjectCreationExpression( Expression.MYSTERY_OFFSET, astBuilder.convertType(parser.parseTypeDescriptor("java/lang/IllegalStateException")), new PrimitiveExpression( Expression.MYSTERY_OFFSET, "An error occurred while decompiling this method.")
astBuilder = new AstBuilder(context); try (final SafeCloseable importSuppression = astBuilder.suppressImports()) { final TypeDeclaration declaration = astBuilder.createType(resolvedType);
@Nullable private MethodDefinition tryLocateClassMethodOutOfScope(final TypeDefinition currentType) { final TypeDefinition enclosingType = MetadataHelper.getOutermostEnclosingType(currentType); if (enclosingType == null) { return null; } final AstBuilder builder = _context.getUserData(Keys.AST_BUILDER); for (final MethodDefinition m : enclosingType.getDeclaredMethods()) { if (ClassMethodLocator.isClassMethodCandidate(m)) { final MethodBody body = m.getBody(); if (body != null && body.getCodeSize() <= CANDIDATE_MAX_CODE_SIZE) { final MethodDeclaration method = builder.createMethod(m); if (ClassMethodLocator.PATTERN.matches(method)) { return m; } } } } return null; }