下面列出了java.lang.reflect.Modifier#FINAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Declares {@code type}.
*
* @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link
* Modifier#FINAL} and {@link Modifier#ABSTRACT}.
*/
public void declare(TypeId<?> type, String sourceFile, int flags,
TypeId<?> supertype, TypeId<?>... interfaces) {
TypeDeclaration declaration = getTypeDeclaration(type);
int supportedFlags = Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT
| AccessFlags.ACC_SYNTHETIC;
if ((flags & ~supportedFlags) != 0) {
throw new IllegalArgumentException("Unexpected flag: "
+ Integer.toHexString(flags));
}
if (declaration.declared) {
throw new IllegalStateException("already declared: " + type);
}
declaration.declared = true;
declaration.flags = flags;
declaration.supertype = supertype;
declaration.sourceFile = sourceFile;
declaration.interfaces = new TypeList(interfaces);
}
private void eachField(Class<?> clazz, P1<Field> p1) {
for (final Field field : clazz.getDeclaredFields()) {
int modifiers = field.getModifiers();
if ((modifiers & (Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT)) > 0
|| (modifiers & Modifier.PUBLIC) == 0
) {
continue;
}
if (idFields.contains(field.getName())) {
continue;
}
p1.e(field);
}
if (!clazz.equals(Object.class)) {
eachField(clazz.getSuperclass(), p1);
}
}
/**
* Declares {@code type}.
*
* @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link
* Modifier#FINAL} and {@link Modifier#ABSTRACT}.
*/
public void declare(TypeId<?> type, String sourceFile, int flags,
TypeId<?> supertype, TypeId<?>... interfaces) {
TypeDeclaration declaration = getTypeDeclaration(type);
int supportedFlags = Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT;
if ((flags & ~supportedFlags) != 0) {
throw new IllegalArgumentException("Unexpected flag: "
+ Integer.toHexString(flags));
}
if (declaration.declared) {
throw new IllegalStateException("already declared: " + type);
}
declaration.declared = true;
declaration.flags = flags;
declaration.supertype = supertype;
declaration.sourceFile = sourceFile;
declaration.interfaces = new TypeList(interfaces);
}
/**
* By the time this is done, it has constructed the following class:
* <pre>
* public class #className extends #superClass {
* public #className() { super(); }
* }
* </pre>
*
* @exception StandardException thrown on failure
*/
protected ExpressionClassBuilder (String superClass, String className, CompilerContext cc )
throws StandardException
{
int modifiers = Modifier.PUBLIC | Modifier.FINAL;
myCompCtx = cc;
JavaFactory javaFac = myCompCtx.getJavaFactory();
if ( className == null ) { className = myCompCtx.getUniqueClassName(); }
// start the class
cb = javaFac.newClassBuilder(myCompCtx.getClassFactory(),
getPackageName(), modifiers,
className, superClass);
beginConstructor();
}
static String getVKText(int keyCode) {
VKCollection vkCollect = getVKCollection();
Integer key = Integer.valueOf(keyCode);
String name = vkCollect.findName(key);
if (name != null) {
return name.substring(3);
}
int expected_modifiers =
(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
Field[] fields = KeyEvent.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
if (fields[i].getModifiers() == expected_modifiers
&& fields[i].getType() == Integer.TYPE
&& fields[i].getName().startsWith("VK_")
&& fields[i].getInt(KeyEvent.class) == keyCode)
{
name = fields[i].getName();
vkCollect.put(name, key);
return name.substring(3);
}
} catch (IllegalAccessException e) {
assert(false);
}
}
return "UNKNOWN";
}
/**
* Returns explicit serial version UID value declared by given class, or
* null if none.
*/
private static Long getDeclaredSUID(Class<?> cl) {
try {
Field f = cl.getDeclaredField("serialVersionUID");
int mask = Modifier.STATIC | Modifier.FINAL;
if ((f.getModifiers() & mask) == mask) {
f.setAccessible(true);
return Long.valueOf(f.getLong(null));
}
} catch (Exception ex) {
}
return null;
}
public LocalDeclaration createLocalDeclaration(ASTNode source, char[] dollarFieldName, TypeReference type, Expression initializer) {
int pS = source.sourceStart, pE = source.sourceEnd;
LocalDeclaration tempVar = new LocalDeclaration(dollarFieldName, pS, pE);
setGeneratedBy(tempVar, source);
tempVar.initialization = initializer;
tempVar.type = type;
tempVar.type.sourceStart = pS; tempVar.type.sourceEnd = pE;
setGeneratedBy(tempVar.type, source);
tempVar.modifiers = Modifier.FINAL;
return tempVar;
}
/**
* Check to see each error code has there description in the
* "MetaError.properties".
*
* @throws IOException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void testCheckMetaErrorConsistency( ) throws IOException,
IllegalArgumentException, IllegalAccessException
{
Properties props = new Properties( );
props.load( MetaDataException.class
.getResourceAsStream( MetaDataException.ERROR_FILE ) );
int PUBLIC_FINAL_STATIC = Modifier.PUBLIC | Modifier.FINAL
| Modifier.STATIC;
boolean success = true;
Field[] fields = MetaDataException.class.getDeclaredFields( );
String errorCode = null;
for ( int i = 0; i < fields.length; i++ )
{
Field field = fields[i];
if ( PUBLIC_FINAL_STATIC == field.getModifiers( ) )
{
errorCode = (String) fields[i].get( null );
if ( errorCode.equalsIgnoreCase( ModelException.PLUGIN_ID ) )
continue;
if ( !props.containsKey( errorCode ) )
{
System.out
.println( "MetaDataException ErrorCode: " + errorCode + " not described in 'MetaError.properties'." ); //$NON-NLS-1$ //$NON-NLS-2$
success = false;
}
}
}
assertTrue( success );
}
/**
* Returns explicit serial version UID value declared by given class, or
* null if none.
*/
private static Long getDeclaredSUID(Class<?> cl) {
try {
Field f = cl.getDeclaredField("serialVersionUID");
int mask = Modifier.STATIC | Modifier.FINAL;
if ((f.getModifiers() & mask) == mask) {
f.setAccessible(true);
return Long.valueOf(f.getLong(null));
}
} catch (Exception ex) {
}
return null;
}
static String getVKText(int keyCode) {
VKCollection vkCollect = getVKCollection();
Integer key = Integer.valueOf(keyCode);
String name = vkCollect.findName(key);
if (name != null) {
return name.substring(3);
}
int expected_modifiers =
(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
Field[] fields = KeyEvent.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
if (fields[i].getModifiers() == expected_modifiers
&& fields[i].getType() == Integer.TYPE
&& fields[i].getName().startsWith("VK_")
&& fields[i].getInt(KeyEvent.class) == keyCode)
{
name = fields[i].getName();
vkCollect.put(name, key);
return name.substring(3);
}
} catch (IllegalAccessException e) {
assert(false);
}
}
return "UNKNOWN";
}
/**
* Returns explicit serial version UID value declared by given class, or
* null if none.
*/
private static Long getDeclaredSUID(Class<?> cl) {
try {
Field f = cl.getDeclaredField("serialVersionUID");
int mask = Modifier.STATIC | Modifier.FINAL;
if ((f.getModifiers() & mask) == mask) {
f.setAccessible(true);
return Long.valueOf(f.getLong(null));
}
} catch (Exception ex) {
}
return null;
}
static String getVKText(int keyCode) {
VKCollection vkCollect = getVKCollection();
Integer key = Integer.valueOf(keyCode);
String name = vkCollect.findName(key);
if (name != null) {
return name.substring(3);
}
int expected_modifiers =
(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
Field[] fields = KeyEvent.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
if (fields[i].getModifiers() == expected_modifiers
&& fields[i].getType() == Integer.TYPE
&& fields[i].getName().startsWith("VK_")
&& fields[i].getInt(KeyEvent.class) == keyCode)
{
name = fields[i].getName();
vkCollect.put(name, key);
return name.substring(3);
}
} catch (IllegalAccessException e) {
assert(false);
}
}
return "UNKNOWN";
}
public Tester makeAnonExtendsLocal(final String message) {
abstract class LocalTester extends Tester {
public LocalTester(final int localparam) {
super(localparam);
}
protected String[] names() {
return new String[] {
"this$1",
"localparam",
"val$message"
};
}
protected int[] modifiers() {
return new int[] {
Modifier.FINAL | MANDATED,
Modifier.FINAL,
Modifier.FINAL | SYNTHETIC
};
}
protected Class[] types() {
return new Class[] {
Encloser.class,
int.class,
String.class
};
}
}
return new LocalTester(2) {
public String message() {
return message;
}
};
}
static String getVKText(int keyCode) {
VKCollection vkCollect = getVKCollection();
Integer key = Integer.valueOf(keyCode);
String name = vkCollect.findName(key);
if (name != null) {
return name.substring(3);
}
int expected_modifiers =
(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
Field[] fields = KeyEvent.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
if (fields[i].getModifiers() == expected_modifiers
&& fields[i].getType() == Integer.TYPE
&& fields[i].getName().startsWith("VK_")
&& fields[i].getInt(KeyEvent.class) == keyCode)
{
name = fields[i].getName();
vkCollect.put(name, key);
return name.substring(3);
}
} catch (IllegalAccessException e) {
assert(false);
}
}
return "UNKNOWN";
}
/** Add all static-final token-id fields declared
* in this token-context using <tt>Class.getDeclaredFields()</tt> call.
*/
protected void addDeclaredTokenIDs() throws IllegalAccessException, SecurityException {
Field[] fields = this.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
int flags = Modifier.STATIC | Modifier.FINAL;
if ((fields[i].getModifiers() & flags) == flags
&& TokenID.class.isAssignableFrom(fields[i].getType())
) {
addTokenID((TokenID)fields[i].get(null));
}
}
}
protected int[] modifiers() {
return new int[] {
Modifier.FINAL | SYNTHETIC,
Modifier.FINAL
};
}
public MethodDeclaration createCanEqual(EclipseNode type, ASTNode source, List<Annotation> onParam) {
/* public boolean canEqual(final java.lang.Object other) {
* return other instanceof Outer.Inner.MyType;
* }
*/
int pS = source.sourceStart; int pE = source.sourceEnd;
long p = (long)pS << 32 | pE;
char[] otherName = "other".toCharArray();
MethodDeclaration method = new MethodDeclaration(
((CompilationUnitDeclaration) type.top().get()).compilationResult);
setGeneratedBy(method, source);
method.modifiers = toEclipseModifier(AccessLevel.PROTECTED);
method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
setGeneratedBy(method.returnType, source);
method.selector = "canEqual".toCharArray();
method.thrownExceptions = null;
method.typeParameters = null;
method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p });
setGeneratedBy(objectRef, source);
method.arguments = new Argument[] {new Argument(otherName, 0, objectRef, Modifier.FINAL)};
method.arguments[0].sourceStart = pS; method.arguments[0].sourceEnd = pE;
if (!onParam.isEmpty()) method.arguments[0].annotations = onParam.toArray(new Annotation[0]);
setGeneratedBy(method.arguments[0], source);
SingleNameReference otherRef = new SingleNameReference(otherName, p);
setGeneratedBy(otherRef, source);
TypeReference typeReference = createTypeReference(type, p);
setGeneratedBy(typeReference, source);
InstanceOfExpression instanceOf = new InstanceOfExpression(otherRef, typeReference);
instanceOf.sourceStart = pS; instanceOf.sourceEnd = pE;
setGeneratedBy(instanceOf, source);
ReturnStatement returnStatement = new ReturnStatement(instanceOf, pS, pE);
setGeneratedBy(returnStatement, source);
method.statements = new Statement[] {returnStatement};
return method;
}
private static Object createObjectProxy(Object realObject, Class realClass, ObjectProxyMethodHook hook) throws Throwable {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(HackJavaFX.class.getPackage().getName() + "." +
realClass.getSimpleName() + "_Proxy");
cc.setSuperclass(pool.get(realClass.getName()));
CtField realObjectField = new CtField(cc.getSuperclass(), "realObject", cc);
realObjectField.setModifiers(Modifier.FINAL | Modifier.PRIVATE);
cc.addField(realObjectField);
CtField realClassField = new CtField(pool.get("java.lang.Class"), "realClass", cc);
realClassField.setModifiers(Modifier.FINAL | Modifier.PRIVATE);
cc.addField(realClassField);
CtConstructor constructor = new CtConstructor(
new CtClass[]{realObjectField.getType(), realClassField.getType()}, cc
);
constructor.setModifiers(Modifier.PUBLIC);
constructor.setBody("{ realObject = $1; realClass = $2; }");
cc.addConstructor(constructor);
for (CtMethod method : cc.getSuperclass().getDeclaredMethods()) {
if ((method.getModifiers() & Modifier.FINAL) != 0) {
continue;
}
if ((method.getModifiers() & Modifier.STATIC) != 0) {
continue;
}
CtMethod newMethod = new CtMethod(method.getReturnType(), method.getName(),
method.getParameterTypes(), cc);
newMethod.setModifiers(method.getModifiers() & ~(Modifier.NATIVE | Modifier.SYNCHRONIZED));
newMethod.setExceptionTypes(method.getExceptionTypes());
if (newMethod.getReturnType().equals(CtClass.voidType)) {
if ((newMethod.getModifiers() & Modifier.PUBLIC) != 0) {
newMethod.setBody("realObject." + method.getName() + "($$);");
} else {
newMethod.setBody("{ java.lang.reflect.Method method = realClass.getDeclaredMethod(\""
+ method.getName() + "\", $sig);" + "method.setAccessible(true);"
+ "method.invoke(this.realObject, $args); }");
}
} else {
if ((newMethod.getModifiers() & Modifier.PUBLIC) != 0) {
newMethod.setBody("return realObject." + method.getName() + "($$);");
} else {
newMethod.setBody("{ java.lang.reflect.Method method = realClass.getDeclaredMethod(\""
+ method.getName() + "\", $sig);" + "method.setAccessible(true);"
+ "java.lang.Object retVal = method.invoke(realObject, $args);"
+ "return ($r) retVal; }");
}
}
if (hook != null) {
hook.processMethod(newMethod);
}
cc.addMethod(newMethod);
}
Class cls = cc.toClass();
Constructor c = cls.getDeclaredConstructor(cls.getSuperclass(), Class.class);
Object proxy = c.newInstance(realObject, realClass);
for (Field field : realClass.getDeclaredFields()) {
if ((field.getModifiers() & Modifier.STATIC) != 0) {
continue;
}
if ((field.getModifiers() & Modifier.FINAL) != 0) {
continue;
}
field.setAccessible(true);
field.set(proxy, field.get(realObject));
}
return proxy;
}
protected boolean isSafeForReuse(DeclarationStatement decl) {
return (decl.modifiers & Modifier.FINAL) != 0 && !decl.parameter.name.startsWith("_");
}
/**
* 计算默认的 serialVersionUID
*
* @see java.io.ObjectStreamClass#lookup(Class)
* @see java.io.ObjectStreamClass#computeDefaultSUID(Class)
*/
public static long computeDefaultSUID(String className, List<Field> fields) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
// simple class name
dout.writeUTF(className);
int classMods = Modifier.PUBLIC & (Modifier.PUBLIC | Modifier.FINAL | Modifier.INTERFACE | Modifier.ABSTRACT);
dout.writeInt(classMods);
// interface name
dout.writeUTF("java.io.Serializable");
// fields
// fields.sort(Comparator.comparing(Field::getField));
for (Field field : fields) {
int mods = Modifier.PRIVATE &
(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED |
Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE |
Modifier.TRANSIENT);
if (((mods & Modifier.PRIVATE) == 0) ||
((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) {
dout.writeUTF(field.getField());
dout.writeInt(mods);
dout.writeUTF(field.getFieldType());
}
}
// method ignore
dout.flush();
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] hashBytes = md.digest(bout.toByteArray());
long hash = 0;
for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
hash = (hash << 8) | (hashBytes[i] & 0xFF);
}
return hash;
} catch (Exception e) {
// ignore
}
return 1;
}