This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.cpp
More file actions
91 lines (67 loc) · 3.22 KB
/
field.cpp
File metadata and controls
91 lines (67 loc) · 3.22 KB
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
#ifndef JDECOMPILER_FIELD_CPP
#define JDECOMPILER_FIELD_CPP
#include "field.h"
#include "code.cpp"
namespace jdecompiler {
Field::Field(modifiers_t modifiers, const FieldDescriptor& descriptor, const Attributes& attributes, const ClassInfo& classinfo)
try:
ClassElement(modifiers), descriptor(descriptor), attributes(attributes),
constantValueAttribute(attributes.get<ConstantValueAttribute>()), genericType(getGenericType(attributes)),
initializer(constantValueAttribute == nullptr ? nullptr :
constantValueAttribute->getInitializer()) {
if(initializer != nullptr)
initializer->castReturnTypeTo(&descriptor.type);
if(modifiers & ACC_ENUM && !(classinfo.modifiers & ACC_ENUM))
throw IllegalModifiersException("Field " + descriptor.toString() + " cannot have enum flag in non-enum class");
} catch(DecompilationException& ex) {
cerr << "Exception while decompiling field " << descriptor.toString() << ": " << ex.toString() << endl;
}
Field::Field(const ClassInfo& classinfo, ClassInputStream& instream): Field(instream.readUShort(),
*new FieldDescriptor(classinfo.constPool.getUtf8Constant(instream.readUShort()), classinfo.constPool.getUtf8Constant(instream.readUShort())),
*new Attributes(instream, classinfo.constPool, instream.readUShort(), AttributesType::FIELD), classinfo) {}
string Field::toString(const ClassInfo&) const {
throw IllegalStateException("For Fields, the toString(const StringifyContext&) method must be called");
}
string Field::toString(const StringifyContext& context) const {
string str;
format_string comment;
if(this->isSynthetic())
comment += "synthetic field";
if(const AnnotationsAttribute* annotationsAttribute = attributes.get<AnnotationsAttribute>())
str += annotationsAttribute->toString(context.classinfo) + '\n';
str += context.classinfo.getIndent() + (string)(modifiersToString(modifiers) +
variableDeclarationToString(genericType == nullptr ? &descriptor.type : genericType, context.classinfo, descriptor.name));
if(initializer != nullptr) {
str += " = " + (
initializer->isAbstractConstOperation() ?
initializer->toString(context,
ConstantDecompilationContext(context.classinfo, context.classinfo.thisType, descriptor)) :
JDecompiler::getInstance().useShortArrayInitializing() ?
initializer->toArrayInitString(context) :
initializer->toString(context));
}
if(!comment.empty())
str += " // " + (string)comment;
return str;
}
bool Field::canStringify(const ClassInfo&) const {
return !(this->isSynthetic() && !JDecompiler::getInstance().showSynthetic());
}
format_string Field::modifiersToString(modifiers_t modifiers) {
format_string str;
switch(modifiers & ACC_ACCESS_FLAGS) {
case ACC_VISIBLE: break;
case ACC_PUBLIC: str += "public"; break;
case ACC_PRIVATE: str += "private"; break;
case ACC_PROTECTED: str += "protected"; break;
default: throw IllegalModifiersException(modifiers);
}
if(modifiers & ACC_STATIC) str += "static";
if(modifiers & ACC_FINAL && modifiers & ACC_VOLATILE) throw IllegalModifiersException(modifiers);
if(modifiers & ACC_FINAL) str += "final";
if(modifiers & ACC_TRANSIENT) str += "transient";
if(modifiers & ACC_VOLATILE) str += "volatile";
return str;
}
}
#endif