props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma"); this.pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(documentText); List<CoreMap> sentences = document.get(SentencesAnnotation.class); for(CoreMap sentence: sentences) {
List<?> classFileAttributes = classFile.getAttributes(); Iterator<?> classItr = classFileAttributes.iterator(); AnnotationsAttribute attr = null; if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(Table.class.getName())) { Set<String> keys = annotation.getMemberNames(); for (String key : keys) { if (key.equalsIgnoreCase("name")) { StringMemberValue value = (StringMemberValue) annotation.getMemberValue(key); String oldTableName = value.getValue(); value.setValue(tableName); if (LOG.isDebugEnabled()) { LOG.debug("Altering " + classFile.getName() + " table name from: " + oldTableName + "" + " to: " + value.getValue()); attr.setAnnotations(items); break; classFile.addAttribute(attr);
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName))); Class<?> type = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { MemberValue memberValue = createMemberValue( classFile.getConstPool(), pool.get(member.getReturnType().getName()), value); ja.addMemberValue(member.getName(), memberValue); attribute.addAnnotation(ja); CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
@Override public DynamicField requestParameter(String param) { Annotation annot = new Annotation(RequestParameter.class.getName(), cpool); annot.addMemberValue("value", new StringMemberValue(param, cpool)); attr.addAnnotation(annot); return this; }
@Override public DynamicField text(boolean own) { Annotation annot = new Annotation(Text.class.getName(), cpool); annot.addMemberValue("own", new BooleanMemberValue(own, cpool)); attr.addAnnotation(annot); return this; }
/** * Returns a string representation of the annotation. */ public String toString() { StringBuffer buf = new StringBuffer("@"); buf.append(getTypeName()); if (members != null) { buf.append("("); Iterator mit = members.keySet().iterator(); while (mit.hasNext()) { String name = (String)mit.next(); buf.append(name).append("=").append(getMemberValue(name)); if (mit.hasNext()) buf.append(", "); } buf.append(")"); } return buf.toString(); }
Annotation ann = new Annotation("this is a sentence"); StanfordCoreNLP stage1 = new StanfordCoreNLP(new Properties(){{ setProperty("annotators", "tokenize,ssplit,pos"); }}); stage1.annotate(ann); // <DO OTHER THINGS> StanfordCoreNLP stage1 = new StanfordCoreNLP(new Properties(){{ setProperty("annotators", "ner"); setProperty("enforceRequirements", "true"); }}); stage2.annotate(ann);
public Class<?> toClass() { if (mCtc != null) mCtc.detach(); long id = CLASS_NAME_COUNTER.getAndIncrement(); try { CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? TccClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) ConstPool constpool = mCtc.getClassFile().getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("org.mengyun.tcctransaction.api.Compensable", constpool); EnumMemberValue enumMemberValue = new EnumMemberValue(constpool); enumMemberValue.setType("org.mengyun.tcctransaction.api.Propagation"); enumMemberValue.setValue("SUPPORTS"); annot.addMemberValue("propagation", enumMemberValue); annot.addMemberValue("confirmMethod", new StringMemberValue(ctMethod.getName(), constpool)); annot.addMemberValue("cancelMethod", new StringMemberValue(ctMethod.getName(), constpool)); annot.addMemberValue("transactionContextEditor", classMemberValue); attr.addAnnotation(annot); ctMethod.getMethodInfo().addAttribute(attr);
cl.setSuperclass(cp.get(cls.getName())); ClassFile ccFile = cl.getClassFile(); ConstPool constpool = ccFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("org.junit.BeforeClass", constpool); attr.addAnnotation(annot); mtd.getMethodInfo().addAttribute(attr); cl.addMethod(mtd); return cl.toClass();
ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer))); boolean containsTypeLevelAnnotation = false; List<FieldInfo> fieldInfos = classFile.getFields(); ConstPool constantPool = classFile.getConstPool(); for (FieldInfo myField : fieldInfos) { List<?> attributes = myField.getAttributes(); Iterator<?> itr = attributes.iterator(); AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(Type.class.getName())) { StringMemberValue annot = (StringMemberValue) annotation.getMemberValue("type"); if (annot != null && annot.getValue().equals(StringClobType.class.getName())) { Annotation clobType = new Annotation(Type.class.getName(), constantPool); StringMemberValue type = new StringMemberValue(constantPool); type.setValue(MaterializedClobType.class.getName()); clobType.addMemberValue("type", type); annotationsAttribute.addAnnotation(clobType); transformed = true;
@SneakyThrows public Proxy<I> addField(String code, Class<? extends java.lang.annotation.Annotation> annotation, Map<String, Object> annotationProperties) { return handleException(() -> { CtField ctField = CtField.make(code, ctClass); if (null != annotation) { ConstPool constPool = ctClass.getClassFile().getConstPool(); AnnotationsAttribute attributeInfo = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation ann = new javassist.bytecode.annotation.Annotation(annotation.getName(), constPool); if (null != annotationProperties) { annotationProperties.forEach((key, value) -> { MemberValue memberValue = createMemberValue(value, constPool); if (memberValue != null) { ann.addMemberValue(key, memberValue); } }); } attributeInfo.addAnnotation(ann); ctField.getFieldInfo().addAttribute(attributeInfo); } ctClass.addField(ctField); }); }
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (!typeName.equals(Inheritance.class.getName())) { annotationsAttribute.addAnnotation(annotation); Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool); ClassPool pool = ClassPool.getDefault(); pool.importPackage("javax.persistence"); pool.importPackage("java.lang"); EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType")); strategy.setType(InheritanceType.class.getName()); strategy.setValue(InheritanceType.SINGLE_TABLE.name()); inheritance.addMemberValue("strategy", strategy); Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool); StringMemberValue name = new StringMemberValue(constantPool); name.setValue(myInfo.getDiscriminatorName()); discriminator.addMemberValue("name", name); EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType")); discriminatorType.setType(DiscriminatorType.class.getName()); discriminatorType.setValue(myInfo.getDiscriminatorType().name()); discriminator.addMemberValue("discriminatorType", discriminatorType); IntegerMemberValue length = new IntegerMemberValue(constantPool); length.setValue(myInfo.getDiscriminatorLength()); discriminator.addMemberValue("length", length);
private static void inheritAnnotations(final CtClass theClass, final CtMethod theMethod) throws NotFoundException { if (hasMethod(theClass, theMethod)) { CtMethod aOtherMethod = theClass.getMethod(theMethod.getName(), theMethod.getSignature()); // method we're probably overriding or implementing in the case of an abstract method. AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) aOtherMethod.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag); if (annotationsAttribute != null) { ConstPool cp = theClass.getClassFile().getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); for (Object obj : annotationsAttribute.getAnnotations()) { Annotation a = (Annotation) obj; Annotation theAnnotation = new Annotation(a.getTypeName(), cp); if (a.getMemberNames() != null) { for (Object aName : a.getMemberNames()) { theAnnotation.addMemberValue(aName.toString(), a.getMemberValue(aName.toString())); } } attr.addAnnotation(theAnnotation); } theMethod.getMethodInfo().addAttribute(attr); } } }
else if (type == CtClass.doubleType) return new DoubleMemberValue(cp); else if (type.getName().equals("java.lang.Class")) return new ClassMemberValue(cp); else if (type.getName().equals("java.lang.String")) return new StringMemberValue(cp); else if (type.isArray()) { CtClass arrayType = type.getComponentType(); MemberValue member = createMemberValue(cp, arrayType); return new ArrayMemberValue(member, cp); else if (type.isInterface()) { Annotation info = new Annotation(cp, type); return new AnnotationMemberValue(info, cp);
private static void addAnnotation(final CtClass clazz, final String fieldName, final String annotationName, String member, int memberValue) throws Exception { final ClassFile cfile = clazz.getClassFile(); final ConstPool cpool = cfile.getConstPool(); final CtField cfield = clazz.getField(fieldName); final AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag); final Annotation annot = new Annotation(annotationName, cpool); annot.addMemberValue(member, new IntegerMemberValue(cpool, memberValue)); attr.addAnnotation(annot); cfield.getFieldInfo().addAttribute(attr); }
private static void createAnchorConstructor(final CtClass impl, final CtClass anchor) throws Exception { final ClassPool classPool = impl.getClassPool(); final CtConstructor ctConstructor = CtNewConstructor.make( new CtClass[]{anchor}, null, CtNewConstructor.PASS_NONE, null, null, impl); final ConstPool constPool = impl.getClassFile().getConstPool(); final MethodInfo methodInfo = ctConstructor.getMethodInfo(); // add injection annotation final AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); final Annotation annotation = new Annotation(constPool, classPool.get(javax.inject.Inject.class.getName())); attr.addAnnotation(annotation); methodInfo.addAttribute(attr); impl.addConstructor(ctConstructor); }
@Override public JavassistDynamicBean gecco(String[] matchUrl, String downloader, int timeout, String... pipelines) { AnnotationsAttribute attr = new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation(Gecco.class.getName(), cpool); MemberValue[] elementMatchUrls = new StringMemberValue[matchUrl.length]; for (int i = 0; i < matchUrl.length; i++) { elementMatchUrls[i] = new StringMemberValue(matchUrl[i], cpool); annot.addMemberValue("matchUrl", arrayMemberValueMatchUrl); annot.addMemberValue("downloader", new StringMemberValue(downloader, cpool)); annot.addMemberValue("timeout", new IntegerMemberValue(cpool, timeout)); elements[i] = new StringMemberValue(pipelines[i], cpool); annot.addMemberValue("pipelines", arrayMemberValue); attr.addAnnotation(annot); cfile.addAttribute(attr); return this;
if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); List<Annotation> newItems = new ArrayList<Annotation>(); ArrayMemberValue namedQueryArray = new ArrayMemberValue(constantPool); ArrayMemberValue nativeQueryArray = new ArrayMemberValue(constantPool); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(NamedQueries.class.getName())) { namedQueryArray = (ArrayMemberValue) annotation.getMemberValue("value"); } else if (typeName.equals(NamedNativeQueries.class.getName())) { nativeQueryArray = (ArrayMemberValue) annotation.getMemberValue("value"); } else { newItems.add(annotation); Annotation namedQueriesAnnotation = new Annotation(NamedQueries.class.getName(), constantPool); namedQueriesAnnotation.addMemberValue("value", namedQueryArray); newItems.add(namedQueriesAnnotation); Annotation nativeQueriesAnnotation = new Annotation(NamedQueries.class.getName(), constantPool); nativeQueriesAnnotation.addMemberValue("value", nativeQueryArray); newItems.add(nativeQueriesAnnotation); attr.setAnnotations(newItems.toArray(new Annotation[newItems.size()]));
protected void buildClassLevelAnnotations(ClassFile classFile, ClassFile templateClassFile, ConstPool constantPool) throws NotFoundException { List<?> templateAttributes = templateClassFile.getAttributes(); Iterator<?> templateItr = templateAttributes.iterator(); Annotation templateEntityListeners = null; if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(EntityListeners.class.getName())) { templateEntityListeners = annotation; AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); List<?> attributes = classFile.getAttributes(); Iterator<?> itr = attributes.iterator(); Annotation existingEntityListeners = null; if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(EntityListeners.class.getName())) { logger.debug("Stripping out previous EntityListeners annotation at the class level - will merge into new EntityListeners"); annotationsAttribute.addAnnotation(entityListeners); classFile.addAttribute(annotationsAttribute);