aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/CtField.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist/CtField.java')
-rw-r--r--src/main/javassist/CtField.java130
1 files changed, 89 insertions, 41 deletions
diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java
index f9d8051b..892fff82 100644
--- a/src/main/javassist/CtField.java
+++ b/src/main/javassist/CtField.java
@@ -16,13 +16,23 @@
package javassist;
-import javassist.bytecode.*;
+import java.util.List;
+
+import javassist.bytecode.AccessFlag;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.AttributeInfo;
+import javassist.bytecode.Bytecode;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstPool;
+import javassist.bytecode.Descriptor;
+import javassist.bytecode.FieldInfo;
+import javassist.bytecode.SignatureAttribute;
+import javassist.compiler.CompileError;
import javassist.compiler.Javac;
import javassist.compiler.SymbolTable;
-import javassist.compiler.CompileError;
import javassist.compiler.ast.ASTree;
-import javassist.compiler.ast.IntConst;
import javassist.compiler.ast.DoubleConst;
+import javassist.compiler.ast.IntConst;
import javassist.compiler.ast.StringL;
/**
@@ -83,15 +93,12 @@ public class CtField extends CtMember {
{
this(src.fieldInfo.getDescriptor(), src.fieldInfo.getName(),
declaring);
- java.util.ListIterator iterator
- = src.fieldInfo.getAttributes().listIterator();
FieldInfo fi = fieldInfo;
fi.setAccessFlags(src.fieldInfo.getAccessFlags());
ConstPool cp = fi.getConstPool();
- while (iterator.hasNext()) {
- AttributeInfo ainfo = (AttributeInfo)iterator.next();
+ List<AttributeInfo> attributes = src.fieldInfo.getAttributes();
+ for (AttributeInfo ainfo : attributes)
fi.addAttribute(ainfo.copy(cp, null));
- }
}
private CtField(String typeDesc, String name, CtClass clazz)
@@ -114,11 +121,13 @@ public class CtField extends CtMember {
/**
* Returns a String representation of the object.
*/
+ @Override
public String toString() {
return getDeclaringClass().getName() + "." + getName()
+ ":" + fieldInfo.getDescriptor();
}
+ @Override
protected void extendToString(StringBuffer buffer) {
buffer.append(' ');
buffer.append(getName());
@@ -126,18 +135,15 @@ public class CtField extends CtMember {
buffer.append(fieldInfo.getDescriptor());
}
- /* Javac.CtFieldWithInit overrides.
- */
+ /* Javac.CtFieldWithInit overrides. */
protected ASTree getInitAST() { return null; }
- /* Called by CtClassType.addField().
- */
+ /* Called by CtClassType.addField(). */
Initializer getInit() {
ASTree tree = getInitAST();
if (tree == null)
return null;
- else
- return Initializer.byExpr(tree);
+ return Initializer.byExpr(tree);
}
/**
@@ -202,6 +208,7 @@ public class CtField extends CtMember {
/**
* Returns the class declaring the field.
*/
+ @Override
public CtClass getDeclaringClass() {
// this is redundant but for javadoc.
return super.getDeclaringClass();
@@ -210,6 +217,7 @@ public class CtField extends CtMember {
/**
* Returns the name of the field.
*/
+ @Override
public String getName() {
return fieldInfo.getName();
}
@@ -227,6 +235,7 @@ public class CtField extends CtMember {
*
* @see Modifier
*/
+ @Override
public int getModifiers() {
return AccessFlag.toModifier(fieldInfo.getAccessFlags());
}
@@ -236,6 +245,7 @@ public class CtField extends CtMember {
*
* @see Modifier
*/
+ @Override
public void setModifiers(int mod) {
declaringClass.checkModify();
fieldInfo.setAccessFlags(AccessFlag.of(mod));
@@ -248,6 +258,7 @@ public class CtField extends CtMember {
* @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
* @since 3.21
*/
+ @Override
public boolean hasAnnotation(String typeName) {
FieldInfo fi = getFieldInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
@@ -269,7 +280,8 @@ public class CtField extends CtMember {
* @return the annotation if found, otherwise <code>null</code>.
* @since 3.11
*/
- public Object getAnnotation(Class clz) throws ClassNotFoundException {
+ @Override
+ public Object getAnnotation(Class<?> clz) throws ClassNotFoundException {
FieldInfo fi = getFieldInfo2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
fi.getAttribute(AnnotationsAttribute.invisibleTag);
@@ -286,6 +298,7 @@ public class CtField extends CtMember {
* @see #getAvailableAnnotations()
* @since 3.1
*/
+ @Override
public Object[] getAnnotations() throws ClassNotFoundException {
return getAnnotations(false);
}
@@ -299,6 +312,7 @@ public class CtField extends CtMember {
* @see #getAnnotations()
* @since 3.3
*/
+ @Override
public Object[] getAvailableAnnotations(){
try {
return getAnnotations(true);
@@ -332,6 +346,7 @@ public class CtField extends CtMember {
* @see javassist.bytecode.Descriptor
* @see #getGenericSignature()
*/
+ @Override
public String getSignature() {
return fieldInfo.getDescriptor();
}
@@ -343,6 +358,7 @@ public class CtField extends CtMember {
* @see SignatureAttribute#toFieldSignature(String)
* @since 3.17
*/
+ @Override
public String getGenericSignature() {
SignatureAttribute sa
= (SignatureAttribute)fieldInfo.getAttribute(SignatureAttribute.tag);
@@ -359,6 +375,7 @@ public class CtField extends CtMember {
* @see javassist.bytecode.SignatureAttribute.ObjectType#encode()
* @since 3.17
*/
+ @Override
public void setGenericSignature(String sig) {
declaringClass.checkModify();
fieldInfo.addAttribute(new SignatureAttribute(fieldInfo.getConstPool(), sig));
@@ -426,8 +443,7 @@ public class CtField extends CtMember {
// "Z" means boolean type.
if ("Z".equals(fieldInfo.getDescriptor()))
return Boolean.valueOf(value != 0);
- else
- return Integer.valueOf(value);
+ return Integer.valueOf(value);
case ConstPool.CONST_String :
return cp.getStringInfo(index);
default :
@@ -447,12 +463,12 @@ public class CtField extends CtMember {
*
* @param name attribute name
*/
+ @Override
public byte[] getAttribute(String name) {
AttributeInfo ai = fieldInfo.getAttribute(name);
if (ai == null)
return null;
- else
- return ai.get();
+ return ai.get();
}
/**
@@ -465,6 +481,7 @@ public class CtField extends CtMember {
* @param name attribute name
* @param data attribute value
*/
+ @Override
public void setAttribute(String name, byte[] data) {
declaringClass.checkModify();
fieldInfo.addAttribute(new AttributeInfo(fieldInfo.getConstPool(),
@@ -854,6 +871,7 @@ public class CtField extends CtMember {
static abstract class CodeInitializer0 extends Initializer {
abstract void compileExpr(Javac drv) throws CompileError;
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -869,6 +887,7 @@ public class CtField extends CtMember {
}
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -887,9 +906,9 @@ public class CtField extends CtMember {
if (tree instanceof IntConst) {
long value = ((IntConst)tree).get();
if (type == CtClass.doubleType)
- return cp.addDoubleInfo((double)value);
+ return cp.addDoubleInfo(value);
else if (type == CtClass.floatType)
- return cp.addFloatInfo((float)value);
+ return cp.addFloatInfo(value);
else if (type == CtClass.longType)
return cp.addLongInfo(value);
else if (type != CtClass.voidType)
@@ -916,10 +935,12 @@ public class CtField extends CtMember {
CodeInitializer(String expr) { expression = expr; }
+ @Override
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
try {
ASTree t = Javac.parseExpr(expression, new SymbolTable());
@@ -936,10 +957,12 @@ public class CtField extends CtMember {
PtreeInitializer(ASTree expr) { expression = expr; }
+ @Override
void compileExpr(Javac drv) throws CompileError {
drv.compileExpr(expression);
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
return getConstantValue2(cp, type, expression);
}
@@ -954,6 +977,7 @@ public class CtField extends CtMember {
ParamInitializer() {}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -965,8 +989,7 @@ public class CtField extends CtMember {
code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
return s; // stack size
}
- else
- return 0; // do not initialize
+ return 0; // do not initialize
}
/**
@@ -998,6 +1021,7 @@ public class CtField extends CtMember {
return k;
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1019,6 +1043,7 @@ public class CtField extends CtMember {
* Produces codes in which a new object is created and assigned to
* the field as the initial value.
*/
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1053,16 +1078,17 @@ public class CtField extends CtMember {
return "(Ljava/lang/Object;[Ljava/lang/Object;)V";
else
return "(Ljava/lang/Object;)V";
- else
- if (withConstructorParams)
- return desc3;
- else
- return "(Ljava/lang/Object;[Ljava/lang/String;)V";
+
+ if (withConstructorParams)
+ return desc3;
+
+ return "(Ljava/lang/Object;[Ljava/lang/String;)V";
}
/**
* Produces codes for a static field.
*/
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1115,6 +1141,7 @@ public class CtField extends CtMember {
* Produces codes in which a new object is created and assigned to
* the field as the initial value.
*/
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1149,16 +1176,17 @@ public class CtField extends CtMember {
return "(Ljava/lang/Object;[Ljava/lang/Object;)";
else
return "(Ljava/lang/Object;)";
- else
- if (withConstructorParams)
- return desc3;
- else
- return "(Ljava/lang/Object;[Ljava/lang/String;)";
+
+ if (withConstructorParams)
+ return desc3;
+
+ return "(Ljava/lang/Object;[Ljava/lang/String;)";
}
/**
* Produces codes for a static field.
*/
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1184,12 +1212,14 @@ public class CtField extends CtMember {
IntInitializer(int v) { value = v; }
+ @Override
void check(String desc) throws CannotCompileException {
char c = desc.charAt(0);
if (c != 'I' && c != 'S' && c != 'B' && c != 'C' && c != 'Z')
throw new CannotCompileException("type mismatch");
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1200,6 +1230,7 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1208,6 +1239,7 @@ public class CtField extends CtMember {
return 1; // stack size
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
return cp.addIntegerInfo(value);
}
@@ -1218,11 +1250,13 @@ public class CtField extends CtMember {
LongInitializer(long v) { value = v; }
+ @Override
void check(String desc) throws CannotCompileException {
if (!desc.equals("J"))
throw new CannotCompileException("type mismatch");
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1233,6 +1267,7 @@ public class CtField extends CtMember {
return 3; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1241,11 +1276,11 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
if (type == CtClass.longType)
return cp.addLongInfo(value);
- else
- return 0;
+ return 0;
}
}
@@ -1254,11 +1289,13 @@ public class CtField extends CtMember {
FloatInitializer(float v) { value = v; }
+ @Override
void check(String desc) throws CannotCompileException {
if (!desc.equals("F"))
throw new CannotCompileException("type mismatch");
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1269,6 +1306,7 @@ public class CtField extends CtMember {
return 3; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1277,11 +1315,11 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
if (type == CtClass.floatType)
return cp.addFloatInfo(value);
- else
- return 0;
+ return 0;
}
}
@@ -1290,11 +1328,13 @@ public class CtField extends CtMember {
DoubleInitializer(double v) { value = v; }
+ @Override
void check(String desc) throws CannotCompileException {
if (!desc.equals("D"))
throw new CannotCompileException("type mismatch");
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1305,6 +1345,7 @@ public class CtField extends CtMember {
return 3; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1313,11 +1354,11 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
if (type == CtClass.doubleType)
return cp.addDoubleInfo(value);
- else
- return 0;
+ return 0;
}
}
@@ -1326,6 +1367,7 @@ public class CtField extends CtMember {
StringInitializer(String v) { value = v; }
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1336,6 +1378,7 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1344,11 +1387,11 @@ public class CtField extends CtMember {
return 1; // stack size
}
+ @Override
int getConstantValue(ConstPool cp, CtClass type) {
if (type.getName().equals(javaLangString))
return cp.addStringInfo(value);
- else
- return 0;
+ return 0;
}
}
@@ -1366,6 +1409,7 @@ public class CtField extends CtMember {
code.addAnewarray(type, size);
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1376,6 +1420,7 @@ public class CtField extends CtMember {
return 2; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{
@@ -1391,11 +1436,13 @@ public class CtField extends CtMember {
MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; }
+ @Override
void check(String desc) throws CannotCompileException {
if (desc.charAt(0) != '[')
throw new CannotCompileException("type mismatch");
}
+ @Override
int compile(CtClass type, String name, Bytecode code,
CtClass[] parameters, Javac drv)
throws CannotCompileException
@@ -1406,6 +1453,7 @@ public class CtField extends CtMember {
return s + 1; // stack size
}
+ @Override
int compileIfStatic(CtClass type, String name, Bytecode code,
Javac drv) throws CannotCompileException
{