static ElementInfo getCheckedElementInfo( MJIEnv env, FieldInfo fi, int fobjRef, Class<?> fiType, String type) { ElementInfo ei; if (!isAvailable(env, fi, fobjRef)) { return null; } if (fi.isStatic()) { ei = fi.getClassInfo().getStaticElementInfo(); } else { // instance field ei = env.getElementInfo(fobjRef); } // our guards (still need IllegalAccessException) if (ei == null) { env.throwException("java.lang.NullPointerException"); return null; } if (fiType != null && !fiType.isInstance(fi)) { env.throwException( "java.lang.IllegalArgumentException", "field type incompatible with " + type); return null; } return ei; }
public String constructDN(final T o, final String parentDN) throws LDAPPersistException { final String existingDN = getEntryDN(o); if (existingDN != null) { return existingDN; } final LinkedHashMap<String, Attribute> attrMap = new LinkedHashMap<String, Attribute>(1); for (final FieldInfo i : rdnFields) { final Attribute a = i.encode(o, true); if (a == null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_RDN_FIELD_MISSING_VALUE.get(type.getName(), i.getField().getName())); } attrMap.put(toLowerCase(i.getAttributeName()), a); } for (final GetterInfo i : rdnGetters) { final Attribute a = i.encode(o); if (a == null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_RDN_GETTER_MISSING_VALUE.get( type.getName(), i.getMethod().getName())); } attrMap.put(toLowerCase(i.getAttributeName()), a); } return constructDN(o, parentDN, attrMap); }
/** * Adds the given FieldInfo to this ClassInfo * * @param fieldInfo the FieldInfo to add */ public void addFieldInfo(final FieldInfo fieldInfo) { if (fieldInfo == null) { return; } fieldInfo.setDeclaringClassInfo(this); switch (fieldInfo.getNodeType()) { case XMLInfo.ATTRIBUTE_TYPE: if (_atts == null) { _atts = new Vector(3); } if (!_atts.contains(fieldInfo)) { _atts.addElement(fieldInfo); } break; case XMLInfo.TEXT_TYPE: _textField = fieldInfo; break; default: if (_elements == null) { _elements = new Vector(5); } if (!_elements.contains(fieldInfo)) { _elements.addElement(fieldInfo); } break; } } // -- addFieldInfo
Entry encode(final T o, final String parentDN) throws LDAPPersistException { // Get the attributes that should be included in the entry. final LinkedHashMap<String, Attribute> attrMap = new LinkedHashMap<String, Attribute>(); attrMap.put("objectClass", objectClassAttribute); for (final Map.Entry<String, FieldInfo> e : fieldMap.entrySet()) { final FieldInfo i = e.getValue(); if (!i.includeInAdd()) { continue; } final Attribute a = i.encode(o, false); if (a != null) { attrMap.put(e.getKey(), a); } } for (final Map.Entry<String, GetterInfo> e : getterMap.entrySet()) { final GetterInfo i = e.getValue(); if (!i.includeInAdd()) { continue; } final Attribute a = i.encode(o); if (a != null) { attrMap.put(e.getKey(), a); } } final String dn = constructDN(o, parentDN, attrMap); final Entry entry = new Entry(dn, attrMap.values()); if (postEncodeMethod != null) { try { postEncodeMethod.invoke(o, entry); } catch (Throwable t) { debugException(t); if (t instanceof InvocationTargetException) { t = ((InvocationTargetException) t).getTargetException(); } throw new LDAPPersistException( ERR_OBJECT_HANDLER_ERROR_INVOKING_POST_ENCODE_METHOD.get( postEncodeMethod.getName(), type.getName(), getExceptionMessage(t)), t); } } setDNAndEntryFields(o, entry); if (superclassHandler != null) { final Entry e = superclassHandler.encode(o, parentDN); for (final Attribute a : e.getAttributes()) { entry.addAttribute(a); } } return entry; }
/** * Creates a public setter method. The setter method assigns the value of the first parameter to * the specified field in the class to which this method is added. The created method is not * static even if the field is static. You may not change it to be static by <code>setModifiers() * </code> in <code>CtBehavior</code>. * * @param methodName the name of the setter * @param field the field accessed. */ public static CtMethod setter(String methodName, CtField field) throws CannotCompileException { FieldInfo finfo = field.getFieldInfo2(); String fieldType = finfo.getDescriptor(); String desc = "(" + fieldType + ")V"; ConstPool cp = finfo.getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(AccessFlag.PUBLIC); Bytecode code = new Bytecode(cp, 3, 3); try { String fieldName = finfo.getName(); if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) { code.addAload(0); code.addLoad(1, field.getType()); code.addPutfield(Bytecode.THIS, fieldName, fieldType); } else { code.addLoad(1, field.getType()); code.addPutstatic(Bytecode.THIS, fieldName, fieldType); } code.addReturn(null); } catch (NotFoundException e) { throw new CannotCompileException(e); } minfo.setCodeAttribute(code.toCodeAttribute()); return new CtMethod(minfo, field.getDeclaringClass()); }
/** * Constructs a new field for the given field info. * * @param info the field info * @throws java.lang.IllegalArgumentException if the field info is null */ protected Field(FieldInfo info) { Guardian.assertNotNull("info", info); _info = info; _data = ProductData.createInstance(info.getDataType(), info.getNumDataElems()); if (_data == null) { throw new IllegalArgumentException("field info has an unknown data type"); /*I18N*/ } }
public void load(XDataInput in) { newContext(); try { magic = in.readInt(); minor = in.readUnsignedShort(); major = in.readUnsignedShort(); pool = ConstantPool.create(true); pool.load(in, pool); flags = Flags.create(in, pool); classname = pool.getConstant(in.readUnsignedShort(), ClassRef.class); Log.debug("Loading class %s", classname.value()); superclass = pool.getConstant(in.readUnsignedShort(), ClassRef.class); // interfaces { int count = in.readUnsignedShort(); interfaces = new ArrayList<ClassRef>(count); while (count-- > 0) { ClassRef iface = pool.getConstant(in.readUnsignedShort(), ClassRef.class); interfaces.add(iface); } } // fields { int count = in.readUnsignedShort(); fields = new ArrayList<FieldInfo>(count); while (count-- > 0) { FieldInfo f = new FieldInfo(this); f.load(in, pool); fields.add(f); } } // methods { int count = in.readUnsignedShort(); methods = new ArrayList<MethodInfo>(count); while (count-- > 0) { MethodInfo m = new MethodInfo(this); m.load(in, pool); methods.add(m); } } attributes = AttributeHelper.loadAttributes(this, in); AttributeHelper.qdhRemoveUnsupportedAttributes(attributes); pool.close(); } finally { closeContext(); } }
protected void checkFieldInfo(FieldInfo fi) { if (getClassInfo() != fi.getClassInfo()) { throw new JPFException( "wrong static FieldInfo : " + fi.getName() + " , no such field in class " + getClassInfo().getName()); } }
public FieldInfo getField(String name) { // fixme performance for (FieldInfo f : fields()) { if (f.name().equals(name)) { return f; } } return null; }
/** * Finds the original field name(s), appending the first one to the out line, and any additional * alternatives to the extra lines. */ private void originalFieldName( String className, String obfuscatedFieldName, String type, StringBuffer outLine, List extraOutLines) { int extraIndent = -1; // Class name -> obfuscated field names. Map fieldMap = (Map) classFieldMap.get(className); if (fieldMap != null) { // Obfuscated field names -> fields. Set fieldSet = (Set) fieldMap.get(obfuscatedFieldName); if (fieldSet != null) { // Find all matching fields. Iterator fieldInfoIterator = fieldSet.iterator(); while (fieldInfoIterator.hasNext()) { FieldInfo fieldInfo = (FieldInfo) fieldInfoIterator.next(); if (fieldInfo.matches(type)) { // Is this the first matching field? if (extraIndent < 0) { extraIndent = outLine.length(); // Append the first original name. if (verbose) { outLine.append(fieldInfo.type).append(' '); } outLine.append(fieldInfo.originalName); } else { // Create an additional line with the proper // indentation. StringBuffer extraBuffer = new StringBuffer(); for (int counter = 0; counter < extraIndent; counter++) { extraBuffer.append(' '); } // Append the alternative name. if (verbose) { extraBuffer.append(fieldInfo.type).append(' '); } extraBuffer.append(fieldInfo.originalName); // Store the additional line. extraOutLines.add(extraBuffer); } } } } } // Just append the obfuscated name if we haven't found any matching // fields. if (extraIndent < 0) { outLine.append(obfuscatedFieldName); } }
@Test public void testJPAEntityFieldInfoAnnotations() { JPAResponderGen rg = new JPAResponderGen(); EntityMetadata mdEntity = new EntityMetadata("Flight"); Vocabulary voc = new Vocabulary(); voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER)); voc.setTerm(new TermIdField(true)); mdEntity.setPropertyVocabulary("ID", voc); voc = new Vocabulary(); voc.setTerm(new TermValueType(TermValueType.TIMESTAMP)); mdEntity.setPropertyVocabulary("departureDT", voc); voc = new Vocabulary(); voc.setTerm(new TermValueType(TermValueType.DATE)); mdEntity.setPropertyVocabulary("departureDate", voc); voc = new Vocabulary(); voc.setTerm(new TermValueType(TermValueType.TIME)); mdEntity.setPropertyVocabulary("departureTime", voc); EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity); // Annotations FieldInfo dateOnlyFI = p.getFieldInfos().get(0); assertEquals(1, dateOnlyFI.getAnnotations().size()); assertEquals("@Temporal(TemporalType.DATE)", dateOnlyFI.getAnnotations().get(0)); FieldInfo timeFI = p.getFieldInfos().get(1); assertEquals(1, timeFI.getAnnotations().size()); assertEquals("@Temporal(TemporalType.TIME)", timeFI.getAnnotations().get(0)); FieldInfo dateFI = p.getFieldInfos().get(2); assertEquals(1, dateFI.getAnnotations().size()); assertEquals("@Temporal(TemporalType.TIMESTAMP)", dateFI.getAnnotations().get(0)); }
public static int genAssignInstanceFieldFromParam( FieldInfo info, int index, InstructionAdapter iv) { assert !info.isStatic(); Type fieldType = info.getFieldType(); iv.load(0, info.getOwnerType()); // this iv.load(index, fieldType); // param iv.visitFieldInsn( PUTFIELD, info.getOwnerInternalName(), info.getFieldName(), fieldType.getDescriptor()); index += fieldType.getSize(); return index; }
public static int getAnnotations_____3Ljava_lang_annotation_Annotation_2(MJIEnv env, int objRef) { FieldInfo fi = getFieldInfo(env, objRef); AnnotationInfo[] ai = fi.getAnnotations(); try { return env.newAnnotationProxies(ai); } catch (ClinitRequired x) { env.handleClinitRequest(x.getRequiredClassInfo()); return MJIEnv.NULL; } }
public static int getName____Ljava_lang_String_2(MJIEnv env, int objRef) { FieldInfo fi = getFieldInfo(env, objRef); int nameRef = env.getReferenceField(objRef, "name"); if (nameRef == -1) { nameRef = env.newString(fi.getName()); env.setReferenceField(objRef, "name", nameRef); } return nameRef; }
private void writeClasses(PackedDataOutputStream stream, Index index) throws IOException { Collection<ClassInfo> classes = index.getKnownClasses(); stream.writePackedU32(classes.size()); for (ClassInfo clazz : classes) { stream.writePackedU32(positionOf(clazz.name())); stream.writePackedU32(clazz.superName() == null ? 0 : positionOf(clazz.superName())); stream.writeShort(clazz.flags()); DotName[] interfaces = clazz.interfaces(); stream.writePackedU32(interfaces.length); for (DotName intf : interfaces) stream.writePackedU32(positionOf(intf)); Set<Entry<DotName, List<AnnotationTarget>>> entrySet = clazz.annotations().entrySet(); stream.writePackedU32(entrySet.size()); for (Entry<DotName, List<AnnotationTarget>> entry : entrySet) { stream.writePackedU32(positionOf(entry.getKey())); List<AnnotationTarget> targets = entry.getValue(); stream.writePackedU32(targets.size()); for (AnnotationTarget target : targets) { if (target instanceof FieldInfo) { FieldInfo field = (FieldInfo) target; stream.writeByte(FIELD_TAG); stream.writePackedU32(positionOf(field.name())); writeType(stream, field.type()); stream.writeShort(field.flags()); } else if (target instanceof MethodInfo) { MethodInfo method = (MethodInfo) target; stream.writeByte(METHOD_TAG); stream.writePackedU32(positionOf(method.name())); stream.writePackedU32(method.args().length); for (int i = 0; i < method.args().length; i++) { writeType(stream, method.args()[i]); } writeType(stream, method.returnType()); stream.writeShort(method.flags()); } else if (target instanceof MethodParameterInfo) { MethodParameterInfo param = (MethodParameterInfo) target; MethodInfo method = param.method(); stream.writeByte(METHOD_PARAMATER_TAG); stream.writePackedU32(positionOf(method.name())); stream.writePackedU32(method.args().length); for (int i = 0; i < method.args().length; i++) { writeType(stream, method.args()[i]); } writeType(stream, method.returnType()); stream.writeShort(method.flags()); stream.writePackedU32(param.position()); } else if (target instanceof ClassInfo) { stream.writeByte(CLASS_TAG); } else throw new IllegalStateException("Unknown target"); } } } }
String constructDN(final T o, final String parentDN, final Map<String, Attribute> attrMap) throws LDAPPersistException { final String existingDN = getEntryDN(o); if (existingDN != null) { return existingDN; } final ArrayList<String> rdnNameList = new ArrayList<String>(1); final ArrayList<byte[]> rdnValueList = new ArrayList<byte[]>(1); for (final FieldInfo i : rdnFields) { final Attribute a = attrMap.get(toLowerCase(i.getAttributeName())); if (a == null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_RDN_FIELD_MISSING_VALUE.get(type.getName(), i.getField().getName())); } rdnNameList.add(a.getName()); rdnValueList.add(a.getValueByteArray()); } for (final GetterInfo i : rdnGetters) { final Attribute a = attrMap.get(toLowerCase(i.getAttributeName())); if (a == null) { throw new LDAPPersistException( ERR_OBJECT_HANDLER_RDN_GETTER_MISSING_VALUE.get( type.getName(), i.getMethod().getName())); } rdnNameList.add(a.getName()); rdnValueList.add(a.getValueByteArray()); } final String[] rdnNames = new String[rdnNameList.size()]; rdnNameList.toArray(rdnNames); final byte[][] rdnValues = new byte[rdnNames.length][]; rdnValueList.toArray(rdnValues); final RDN rdn = new RDN(rdnNames, rdnValues); if (parentDN == null) { return new DN(rdn, defaultParentDN).toString(); } else { try { final DN parsedParentDN = new DN(parentDN); return new DN(rdn, parsedParentDN).toString(); } catch (LDAPException le) { debugException(le); throw new LDAPPersistException( ERR_OBJECT_HANDLER_INVALID_PARENT_DN.get(type.getName(), parentDN, le.getMessage()), le); } } }
public static int toString____Ljava_lang_String_2(MJIEnv env, int objRef) { StringBuilder sb = new StringBuilder(); FieldInfo fi = getFieldInfo(env, objRef); sb.append(Modifier.toString(fi.getModifiers())); sb.append(' '); sb.append(fi.getType()); sb.append(' '); sb.append(fi.getFullName()); int sref = env.newString(sb.toString()); return sref; }
public void handleMyShit(String value, FromHTMLConverter<?> callBack) throws Exception { String fieldValue; if (info.isHTMLTypeAdapterDefined()) { Object adaptedValue = info.getAdaptedValue(value); fieldValue = (String) adaptedValue; info.setField(adaptedValue); } else { fieldValue = value; info.setField(value); } info.handleLinkBinding(callBack, fieldValue); }
@MJI public int getName____Ljava_lang_String_2(MJIEnv env, int objRef, FeatureExpr ctx) { FieldInfo fi = getFieldInfo(ctx, env, objRef); int nameRef = env.getReferenceField(ctx, objRef, "name").getValue(); if (nameRef == MJIEnv.NULL) { nameRef = env.newString(ctx, fi.getName()); env.setReferenceField(ctx, objRef, "name", nameRef); } return nameRef; }
/** >2do> that doesn't take care of class init yet */ @MJI public int getType____Ljava_lang_Class_2(MJIEnv env, int objRef, FeatureExpr ctx) { ThreadInfo ti = env.getThreadInfo(); FieldInfo fi = getFieldInfo(ctx, env, objRef); ClassInfo ci = fi.getTypeClassInfo(); if (!ci.isRegistered()) { ci.registerClass(ctx, ti); } return ci.getClassObjectRef(); }
/** * Returns a fieldInfo that corresponds to an element with the given node name. A ClassInfo cannot * have 2 elements with the same xml name. * * @param nodeName the NodeName of the field to get. * @return a fieldInfo that corresponds to an element with the given node name. */ public FieldInfo getElementField(final String nodeName) { if (_elements != null) { for (int i = 0; i < _elements.size(); i++) { FieldInfo temp = (FieldInfo) _elements.get(i); String elementNodeName = temp.getNodeName(); if (elementNodeName != null && elementNodeName.equals(nodeName)) { return temp; } } } return null; }
/** * Returns a fieldInfo that corresponds to an attribute with the given node name. A ClassInfo * cannot have 2 attributes with the same xml name. * * @param nodeName the NodeName of the field to get. * @return a fieldInfo that corresponds to an attribute with the given node name. */ public FieldInfo getAttributeField(final String nodeName) { if (_atts == null) { return null; } for (int i = 0; i < _atts.size(); i++) { FieldInfo temp = (FieldInfo) _atts.get(i); if (temp.getNodeName().equals(nodeName)) { return temp; } } return null; }
private void registerConstants(ConstantPool pool) { classname = pool.register(classname); superclass = pool.register(superclass); interfaces = Helper.register(interfaces, pool); for (FieldInfo f : iterable(fields)) { f.registerConstants(pool); } for (MethodInfo m : iterable(methods)) { m.registerConstants(pool); } for (AttributeInfo a : iterable(attributes)) { a.registerConstants(pool); } pool.rebuild(); }
/** gc mark all objects stored in static reference fields */ void markStaticRoot(Heap heap) { ClassInfo ci = getClassInfo(); int n = ci.getNumberOfStaticFields(); for (int i = 0; i < n; i++) { FieldInfo fi = ci.getStaticField(i); if (fi.isReference()) { int objref = fields.getIntValue(fi.getStorageOffset()); heap.markStaticRoot(objref); } } // don't forget the class object itself (which is not a field) heap.markStaticRoot(classObjectRef); }
/** * Returns true if the given FieldInfo is contained within this ClassInfo * * @param fieldInfo the FieldInfo to check * @return true if the given FieldInfo is contained within this ClassInfo */ public boolean contains(final FieldInfo fieldInfo) { if (fieldInfo == null) { return false; } switch (fieldInfo.getNodeType()) { case XMLInfo.ATTRIBUTE_TYPE: if (_atts != null) { return _atts.contains(fieldInfo); } break; case XMLInfo.TEXT_TYPE: return (fieldInfo == _textField); default: if (_elements != null) { return _elements.contains(fieldInfo); } break; } // if (sourceInfo != null) // return sourceInfo.contains(fieldInfo); return false; } // -- contains
/** * mark all our fields as static (shared) reachable. No need to set our own attributes, since we * reside in the StaticArea * * @aspects: gc */ void markStaticRoot(Heap heap) { // WATCH IT! this overrides the heap object behavior in our super class. // See ElementInfo.markStaticRoot() for details ClassInfo ci = getClassInfo(); int n = ci.getNumberOfStaticFields(); for (int i = 0; i < n; i++) { FieldInfo fi = ci.getStaticField(i); if (fi.isReference()) { heap.markStaticRoot(fields.getIntValue(fi.getStorageOffset())); } } // don't forget the class object itself (which is not a field) heap.markStaticRoot(classObjectRef); }
/** >2do> that doesn't take care of class init yet */ public static int getType____Ljava_lang_Class_2(MJIEnv env, int objRef) { ThreadInfo ti = env.getThreadInfo(); FieldInfo fi = getFieldInfo(env, objRef); try { ClassInfo ci = fi.getTypeClassInfo(); if (!ci.isRegistered()) { ci.registerClass(ti); } return ci.getClassObjectRef(); } catch (NoClassInfoException cx) { env.throwException("java.lang.NoClassDefFoundError", cx.getMessage()); return MJIEnv.NULL; } }
public static int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2( MJIEnv env, int objRef, int annotationClsRef) { FieldInfo fi = getFieldInfo(env, objRef); ClassInfo aci = env.getReferredClassInfo(annotationClsRef); AnnotationInfo ai = fi.getAnnotation(aci.getName()); if (ai != null) { ClassInfo aciProxy = ClassInfo.getAnnotationProxy(aci); try { return env.newAnnotationProxy(aciProxy, ai); } catch (ClinitRequired x) { env.handleClinitRequest(x.getRequiredClassInfo()); return MJIEnv.NULL; } } return MJIEnv.NULL; }
static boolean isAvailable(MJIEnv env, FieldInfo fi, int fobjRef) { if (fi.isStatic()) { ClassInfo fci = fi.getClassInfo(); if (fci.requiresClinitExecution(env.getThreadInfo())) { env.repeatInvocation(); return false; } } else { if (fobjRef == MJIEnv.NULL) { env.throwException("java.lang.NullPointerException"); return false; } // class had obviously been initialized, otherwise we won't have an instance of it } return true; }
@Override public FieldInfo[] getDeclaredFields() { List<FieldInfo> result = new ArrayList<>(); Iterable<? extends Field> implFields = classDef.getFields(); for (Field field : implFields) { FieldInfo fi = new FieldInfo(); fi.typeName = DexlibAdapter.getTypeName(field.getType()); fi.modifiers = field.getAccessFlags(); fi.annotations = convertAnnotations(field.getAnnotations()); fi.name = field.getName(); result.add(fi); } FieldInfo[] array = new FieldInfo[result.size()]; return result.toArray(array); }