是否有一个命令行工具,最好是在JDK中,要么在类文件中打印所有注释,要么将特定注释作为参数进行打印?
如果是这样,是否有一个等效命令可以在jar文件上运行,包含在其中的特定类?
我用谷歌搜索了一段时间,没有运气.:(
有人能告诉我为什么会出现以下错误?
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.transbinary.main.Client.main(Client.java:13)
Run Code Online (Sandbox Code Playgroud)
我得到了所需的结果,并使用数据填充人员表,但我收到此错误.
这是我正在使用的代码: Person.java
@Entity
public class Person implements Serializable {
private Integer id;
private Address homeAddress;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) { …Run Code Online (Sandbox Code Playgroud) 我有两个注解@LookAtThisMethod和@LookAtThisParameter,如果我身边有方法的切入点与@LookAtThisMethod我怎么能提取其标注了该方法的参数@LookAtThisParameter?
例如:
@Aspect
public class LookAdvisor {
@Pointcut("@annotation(lookAtThisMethod)")
public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){}
@Around("lookAtThisMethodPointcut(lookAtThisMethod)")
public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable {
for(Object argument : joinPoint.getArgs()) {
//I can get the parameter values here
}
//I can get the method signature with:
joinPoint.getSignature.toString();
//How do I get which parameters are annotated with @LookAtThisParameter?
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个新的注释,我将在其中进行一些运行时布线,但是,由于多种原因,我想在编译时验证我的布线是否会成功进行一些基本检查.
假设我创建了一个新注释:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation{
}
Run Code Online (Sandbox Code Playgroud)
现在我想在编译时进行某种验证,比如检查CustomAnnotation注释是否属于特定类型的字段:ParticularType.我在Java 6工作,所以我创建了一个AbstractProcessor:
@SupportedAnnotationTypes("com.example.CustomAnnotation")
public class CompileTimeAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for(Element e : elements){
if(!e.getClass().equals(ParticularType.class)){
processingEnv.getMessager().printMessage(Kind.ERROR,
"@CustomAnnotation annotated fields must be of type ParticularType");
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,根据我发现的一些说明,我创建了一个文件夹META-INF/services并创建了一个javax.annotation.processing.Processor包含内容的文件:
com.example.CompileTimeAnnotationProcessor
Run Code Online (Sandbox Code Playgroud)
然后,我将项目导出为jar.
在另一个项目中,我构建了一个简单的测试类:
public class TestClass {
@CustomAnnotation
private String bar; // not `ParticularType`
}
Run Code Online (Sandbox Code Playgroud)
我按如下方式配置了Eclipse项目属性: …
我有一个使用ActiveAndroid的应用程序,它是一个依赖于注释的数据库ORM库.
@Table(name="test")
public class DatabaseItem extends ActiveRecordBase<DatabaseItem> {
public DatabaseItem(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Column(name="counter")
public int counter;
}
Run Code Online (Sandbox Code Playgroud)
我如何让Proguard与之合作得很好?目前,我在使用Proguard时遇到ActiveAndroid没有找到列名的错误.我想它以某种方式破坏了注释.
我的相关Proguard配置:
#ActiveAndroid
-keep public class com.activeandroid.**
-keep public class * extends com.activeandroid.ActiveRecordBase
-keepattributes Column
-keepattributes Table
Run Code Online (Sandbox Code Playgroud) 来自Java doc:
CLASS:注释将由编译器记录在类文件中,但在运行时不需要由VM保留.
RUNTIME:注释将由编译器记录在类文件中,并在运行时由VM保留,因此可以反射性地读取它们.
SOURCE:编译器将丢弃注释.
我理解RUNTIME的用法(为了使用带有反射的注释)和CLASS(用于编译器),但我不明白何时使用它是有用的
@Retention(RetentionPolicy.SOURCE)
你可以解释吗?
Java大师,
我很新,annotations并没有搜索过这个,所以请耐心等待...
我想实现Custom Annotation这将intercept一个方法调用.从非常基本的东西开始,它可以只打印方法名称和参数,以便我可以避免使用该logger语句.
像这样的示例调用:
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
log.debug("in findMyAppObjectById(" + id + ")");
//....
}
Run Code Online (Sandbox Code Playgroud)
可以转换成:
@LogMethodCall(Logger.DEBUG)
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
//....
}
Run Code Online (Sandbox Code Playgroud)
我可以得到一些关于此的提示吗?
注释可以具有复杂的返回类型,例如HashMap.
我正在寻找类似的东西:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface column {
public HashMap<String, String> table();
}
Run Code Online (Sandbox Code Playgroud)
所以我可以有一个常量注释(伪代码):
@column({table=(dbName, tableName), table=(dbName, tableName2)})
public static final String USER_ID = "userid";
Run Code Online (Sandbox Code Playgroud)
如果Annotation不允许你有复杂的返回类型,那么对于这种情况有什么好的做法吗?
我有以下ORM Symfony实体,只有属性:
<?php
namespace Evr\HomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="ev_article")
* @ORM\Entity
*/
class Article
{
/**
*
* @ORM\Column(name="article_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
* @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
*/
private $subcategory;
/**
*
* @ORM\Column(type="string",length=512)
*/
private $title;
/**
*
* @ORM\Column(type="text")
*/
private $content;
/**
*
* @ORM\Column(type="text")
*/
private $exclusive_content;
/**
*
* @ORM\Column(type="date")
*/
private $creation_date;
/**
*
* @ORM\Column(type="integer")
*/
private $views;
/**
*
* @ORM\Column(type="integer")
*/
private …Run Code Online (Sandbox Code Playgroud) 我正在实现一个自定义数据结构,它给我一些集合的属性和列表的其他属性.但是对于大多数实现的方法,我在Java 7上的IntelliJ IDEA中得到了这个奇怪的警告:
未注释的方法覆盖使用@NotNull注释的方法
编辑:下面的代码与问题无关,而是原始问题的一部分.由于IntelliJ中存在错误,此警告会显示.查看答案(希望如此)解决您的问题.
我一直无法找到任何相关的东西,我不确定我是否真的错过了某种检查,但我已经查看了ArrayList和List接口的来源,看不清楚是什么这个警告实际上是关于.它位于引用列表字段的每个实现方法上.这是我所制作的课程的片段:
public class ListHashSet<T> implements List<T>, Set<T> {
private ArrayList<T> list;
private HashSet<T> set;
/**
* Constructs a new, empty list hash set with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity of the list hash set
* @param loadFactor the load factor of the list hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor …Run Code Online (Sandbox Code Playgroud) annotations ×10
java ×8
aop ×1
aspectj ×1
class ×1
collections ×1
command-line ×1
doctrine ×1
eclipse ×1
hibernate ×1
interceptor ×1
jar ×1
logging ×1
notnull ×1
proguard ×1
reflection ×1
symfony ×1