Java Code Examples for org.apache.uima.cas.impl.TypeImpl#getJavaDescriptor()

The following examples show how to use org.apache.uima.cas.impl.TypeImpl#getJavaDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FeatureStructureClassGen.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Not called for built-in types
 * @param type the type to generate
 * @return the bytecode for that type
 */
byte[] createJCas_TypeCoverClass(TypeImpl type) {
  this.type = type;
  typeJavaDescriptor = type.getJavaDescriptor();
  typeJavaClassName = type.getName().replace('.', '/') + "_Type";
  cn = new ClassNode(ASM5); // java 8
  cn.version = JAVA_CLASS_VERSION;
  cn.access = ACC_PUBLIC + ACC_SUPER;
  cn.name = typeJavaClassName;   
  cn.superName = type.getSuperType().getName().replace('.', '/') + "_Type";

  // TODO
  
  ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
  cn.accept(cw);
  return cw.toByteArray();
}
 
Example 2
Source File: FeatureStructureClassGen.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
   * Create - no customization case
   *          not used for TOP or other built-in predefined types
   * @return the class as a byte array
   */
  byte[] createJCasCoverClass(TypeImpl type) {
    this.type = type;
    typeJavaDescriptor = type.getJavaDescriptor();
    typeJavaClassName = type.getName().replace('.', '/');
    cn = new ClassNode(ASM5); // java 8
    cn.version = JAVA_CLASS_VERSION;
    cn.access = ACC_PUBLIC + ACC_SUPER;
    cn.name = typeJavaClassName;   
    cn.superName = type.getSuperType().getName().replace('.', '/');
//    cn.interfaces = typeImpl.getInterfaceNamesArray();   // TODO
    
    // add the "_typeImpl" field - this has a ref to the TypeImpl for this class
    cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "_typeImpl", "Lorg/apache/uima/type_system/impl/TypeImpl;", null, null));
    
    // add field declares, and getters and setters, and special getters/setters for array things    
    type.getMergedStaticFeaturesIntroducedByThisType().stream()
          .forEach(this::addFeatureFieldGetSet);
 
    addStaticInitAndConstructors();
    
    createSwitchGettersAndSetters();
    
    
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cn.accept(cw);
    return cw.toByteArray();
  }
 
Example 3
Source File: FeatureStructureClassGen.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void makeTypedSwitchGetterSetter(TypeImpl featureRangeType) {
  List<FeatureImpl> features = type.getFeaturesSharingRange(featureRangeType);
  if (null == features || features.size() == 0) {
    return;
  }
  final int nbrFeats = features.size();
  
  String rangeName = featureRangeType.isArray() ? ((TypeImplArray) featureRangeType).getComponentType().getName() + "Array" : featureRangeType.getShortName();
  String javaDesc = featureRangeType.getJavaDescriptor();

  // these two are for the method calls to the actual getters/setters
  String getterJavaDesc = "()" + javaDesc;
  String setterJavaDesc = "(" + javaDesc + ")V"; 

  LabelNode[] labelNodesGet = new LabelNode[nbrFeats];
  LabelNode[] labelNodesSet = new LabelNode[nbrFeats];
  int[] keys = new int[nbrFeats];
  for (int i = 0; i < nbrFeats; i++) {
    labelNodesGet[i] = new LabelNode(new Label());
    labelNodesSet[i] = new LabelNode(new Label());
    keys[i] = features.get(i).getOffsetForGenerics();
  }
  Arrays.sort(keys);
  
  LabelNode defaultLabelNodeGet = new LabelNode(new Label());
  LabelNode defaultLabelNodeSet = new LabelNode(new Label());

  for (boolean isGet : GET_SET) {
    MethodNode mn = new MethodNode(ASM5, ACC_PUBLIC,    
        "_" + (isGet ? "get" : "set") + rangeName,  // _ avoids name collision with other getters/setters 
        isGet ? ("(I)" + javaDesc) : 
                ("(I" + javaDesc + ")V"), null, null);
    InsnList il = mn.instructions;
    il.add(new VarInsnNode(ILOAD,  1)); // load the switch int   
    il.add(new LookupSwitchInsnNode(isGet ? defaultLabelNodeGet : defaultLabelNodeSet, 
                                    keys, 
                                    isGet ? labelNodesGet : labelNodesSet));   
    
    for (int i = 0; i < nbrFeats; i++) {
      final FeatureImpl fi = features.get(i);
      il.add((isGet ? labelNodesGet : labelNodesSet)[i]);
      il.add(new FrameNode(F_SAME, 0, null, 0, null));
      il.add(new VarInsnNode(ALOAD, 0));  // load this
      if (isGet) {
        il.add(new MethodInsnNode(INVOKEVIRTUAL, typeJavaClassName, fi.getGetterSetterName(GET), getterJavaDesc, false));
        il.add(new InsnNode(getReturnInst(fi)));
      } else {
        // setter  - here we might insert code to do index corruption fixup
        il.add(new VarInsnNode(getLoadInst(fi), 2));  // load the value or value ref
        il.add(new MethodInsnNode(INVOKEVIRTUAL, typeJavaClassName, fi.getGetterSetterName(SET), setterJavaDesc, false));
        il.add(new InsnNode(RETURN));
      }
    }
    
    // default - throw
    il.add(isGet? defaultLabelNodeGet : defaultLabelNodeSet);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new TypeInsnNode(NEW, CAS_RUN_EX));
    il.add(new InsnNode(DUP));
    il.add(new LdcInsnNode("INAPPROP_FEAT_X"));
    il.add(new MethodInsnNode(INVOKESPECIAL, CAS_RUN_EX, "<init>", "(Ljava/lang/String;)V", false));
    il.add(new InsnNode(ATHROW));

    boolean is2slotValue = featureRangeType.isLongOrDouble();
    mn.maxStack = 3;   // for throw
    mn.maxLocals = isGet ? 2 : (is2slotValue ? 4 : 3);
    
    cn.methods.add(mn);
  }
}