List of usage examples for org.apache.commons.lang3 ClassUtils getClass
public static Class<?> getClass(final String className) throws ClassNotFoundException
From source file:org.broadleafcommerce.core.web.api.jaxrs.IsJaxrsAvailableCondition.java
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { try {// w w w . jav a 2 s . c o m ClassUtils.getClass(JAXRSCLASS); } catch (ClassNotFoundException e) { return false; } return true; }
From source file:org.eclipse.hawkbit.event.BusProtoStuffMessageConverter.java
@Override public Object convertFromInternal(final Message<?> message, final Class<?> targetClass, final Object conversionHint) { final Object payload = message.getPayload(); try {/*from w ww . j a v a 2 s .c o m*/ final Class<?> deserializeClass = ClassUtils .getClass(message.getHeaders().get(DEFAULT_CLASS_FIELD_NAME).toString()); if (payload instanceof byte[]) { @SuppressWarnings("unchecked") final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(deserializeClass); final Object deserializeEvent = schema.newMessage(); ProtobufIOUtil.mergeFrom((byte[]) message.getPayload(), deserializeEvent, schema); return deserializeEvent; } } catch (final ClassNotFoundException e) { LOG.error("Protostuff cannot find derserialize class", e); throw new MessageConversionException(message, "Failed to read payload", e); } return null; }
From source file:org.eclipse.hawkbit.repository.event.remote.entity.RemoteEntityEvent.java
@SuppressWarnings("unchecked") private E reloadEntityFromRepository() { try {/*from w w w. j a v a 2 s .c o m*/ final Class<E> clazz = (Class<E>) ClassUtils.getClass(getEntityClass()); return EventEntityManagerHolder.getInstance().getEventEntityManager().findEntity(getTenant(), getEntityId(), clazz); } catch (final ClassNotFoundException e) { LOG.error("Cannot reload entity because class is not found", e); } return null; }
From source file:org.eclipse.hawkbit.ui.common.table.BaseUIEntityEvent.java
/** * Checks if the remote event is the same as this UI event. Then maybe you * can skip the remote event because it is already executed. * * @param tenantAwareEvent/* w ww . ja va 2 s. co m*/ * the remote event * @return {@code true} match ; {@code false} not match */ public boolean matchRemoteEvent(final TenantAwareEvent tenantAwareEvent) { if (!(tenantAwareEvent instanceof RemoteIdEvent) || entityClass == null || entityIds == null) { return false; } final RemoteIdEvent remoteIdEvent = (RemoteIdEvent) tenantAwareEvent; try { final Class<?> remoteEntityClass = ClassUtils.getClass(remoteIdEvent.getEntityClass()); return entityClass.isAssignableFrom(remoteEntityClass) && entityIds.contains(remoteIdEvent.getEntityId()); } catch (final ClassNotFoundException e) { LOG.error("Entity Class of remoteIdEvent cannot be found", e); return false; } }
From source file:org.fit.cssbox.scriptbox.deprecied.SanboxedJavaObject.java
public static Class<?>[] parseMethodSignature(String signature) { List<String> parsedArgList = new ArrayList<String>(); int leftParenPos = signature.indexOf("("); int rightParenPos = signature.lastIndexOf(")"); if (leftParenPos > 0 && rightParenPos > leftParenPos) { String typeAndMethodName = signature.substring(0, signature.indexOf("(")); String typeAndMethodNameArr[] = typeAndMethodName.split("\\s"); if (typeAndMethodNameArr.length != 2 || typeAndMethodNameArr[0].isEmpty()) { return null; }/* ww w . j ava 2 s. com*/ //parsedArgList.add(typeAndMethodNameArr[0]); } else { return null; } String callParams = signature.substring(leftParenPos + 1, rightParenPos); callParams = callParams.replaceAll("\\s+", ""); if (callParams != null && !callParams.isEmpty()) { String[] params = callParams.split("\\,"); for (String param : params) { if (param.isEmpty()) { return null; } parsedArgList.add(param); } } Class<?>[] types = new Class<?>[parsedArgList.size()]; for (int i = 0; i < parsedArgList.size(); i++) { String argTypeName = parsedArgList.get(i); if (i == 0 && argTypeName.equals("void")) { types[i] = null; continue; } try { types[i] = ClassUtils.getClass(argTypeName); } catch (ClassNotFoundException e) { return null; } } return types; }
From source file:org.force66.json.tools.JSonSchemaGenerator.java
public static void main(String[] args) throws Exception { Validate.isTrue(args != null && args.length >= 2, "Usage arguments: className fileTarget"); String className = args[0];//from ww w . j a v a 2 s. c o m String outputFileName = args[1]; HyperSchemaFactoryWrapper schemaVisitor = new HyperSchemaFactoryWrapper(); ObjectMapper mapper = new ObjectMapper(); mapper.acceptJsonFormatVisitor(ClassUtils.getClass(className), schemaVisitor); JsonSchema jsonSchema = schemaVisitor.finalSchema(); File outputFile = new File(outputFileName); FileUtils.write(outputFile, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema)); }
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
/** * Reads the bytecode from the given {@link InsnCursor}'s <strong>current position</strong>, until * there is no further instruction to proceed. It is the responsability of the caller to set the * cursor position.//from w w w .j a v a2s. c o m * * @param insnCursor the instruction cursor used to read the bytecode. * @param expressionStack the expression stack to put on or pop from. * @param localVariables the local variables * @return a {@link List} of {@link Statement} containing the {@link Statement} */ private List<Statement> readStatements(final InsnCursor insnCursor, final Stack<Expression> expressionStack, final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) { final List<Statement> statements = new ArrayList<>(); while (insnCursor.hasCurrent()) { final AbstractInsnNode currentInstruction = insnCursor.getCurrent(); switch (currentInstruction.getType()) { case AbstractInsnNode.VAR_INSN: final VarInsnNode varInstruction = (VarInsnNode) currentInstruction; switch (currentInstruction.getOpcode()) { // load a reference onto the stack from a local variable case Opcodes.ALOAD: case Opcodes.ILOAD: // load an int value from a local variable // Note: The 'var' operand is the index of a local variable // all captured arguments come before the local variable in the method signature, // which means that the local variables table is empty on the first slots which are // "allocated" // for the captured arguments. if (varInstruction.var < capturedArguments.size()) { // if the variable index matches a captured argument // note: not using actual captured argument but rather, use a _reference_ to it. final Object capturedArgumentValue = capturedArguments.get(varInstruction.var).getValue(); final Class<?> capturedArgumentValueType = capturedArgumentValue != null ? capturedArgumentValue.getClass() : Object.class; final CapturedArgumentRef capturedArgumentRef = new CapturedArgumentRef(varInstruction.var, capturedArgumentValueType); expressionStack.add(capturedArgumentRef); } else { // the variable index matches a local variable final LocalVariableNode var = localVariables.load(varInstruction.var); expressionStack.add(new LocalVariable(var.index, var.name, readSignature(var.desc))); } break; case Opcodes.ASTORE: // store a reference into a local variable localVariables.store(varInstruction.var); break; default: throw new AnalyzeException( "Unexpected Variable instruction code: " + varInstruction.getOpcode()); } break; case AbstractInsnNode.LDC_INSN: // let's move this instruction on top of the stack until it // is used as an argument during a method call final LdcInsnNode ldcInsnNode = (LdcInsnNode) currentInstruction; final Expression constant = ExpressionFactory.getExpression(ldcInsnNode.cst); LOGGER.trace("Stacking constant {}", constant); expressionStack.add(constant); break; case AbstractInsnNode.FIELD_INSN: final FieldInsnNode fieldInsnNode = (FieldInsnNode) currentInstruction; switch (fieldInsnNode.getOpcode()) { case Opcodes.GETSTATIC: final Type ownerType = Type.getType(fieldInsnNode.desc); final FieldAccess staticFieldAccess = new FieldAccess(new ClassLiteral(getType(ownerType)), fieldInsnNode.name); expressionStack.add(staticFieldAccess); break; case Opcodes.GETFIELD: final Expression fieldAccessParent = expressionStack.pop(); final FieldAccess fieldAccess = new FieldAccess(fieldAccessParent, fieldInsnNode.name); expressionStack.add(fieldAccess); break; case Opcodes.PUTFIELD: final Expression fieldAssignationValue = expressionStack.pop(); final Expression parentSource = expressionStack.pop(); final FieldAccess source = new FieldAccess(parentSource, fieldInsnNode.name); final Assignment assignmentExpression = new Assignment(source, fieldAssignationValue); statements.add(new ExpressionStatement(assignmentExpression)); break; default: throw new AnalyzeException("Unexpected field instruction type: " + fieldInsnNode.getOpcode()); } break; case AbstractInsnNode.METHOD_INSN: final MethodInsnNode methodInsnNode = (MethodInsnNode) currentInstruction; final Type[] argumentTypes = Type.getArgumentTypes(methodInsnNode.desc); final List<Expression> args = new ArrayList<>(); final List<Class<?>> parameterTypes = new ArrayList<>(); Stream.of(argumentTypes).forEach(argumentType -> { final Expression arg = expressionStack.pop(); final String argumentClassName = argumentType.getClassName(); args.add(castOperand(arg, argumentClassName)); try { parameterTypes.add(ClassUtils.getClass(argumentClassName)); } catch (Exception e) { throw new AnalyzeException("Failed to find class '" + argumentClassName + "'", e); } }); // arguments appear in reverse order in the bytecode Collections.reverse(args); switch (methodInsnNode.getOpcode()) { case Opcodes.INVOKEINTERFACE: case Opcodes.INVOKEVIRTUAL: case Opcodes.INVOKESPECIAL: // object instantiation if (methodInsnNode.name.equals("<init>")) { final ObjectInstanciation objectVariable = (ObjectInstanciation) expressionStack.pop(); objectVariable.setInitArguments(args); } else { final Expression sourceExpression = expressionStack.pop(); final Method javaMethod = ReflectionUtils.findJavaMethod(sourceExpression.getJavaType(), methodInsnNode.name, parameterTypes); final Class<?> returnType = findReturnType(insnCursor, javaMethod); final MethodInvocation invokedMethod = new MethodInvocation(sourceExpression, javaMethod, returnType, args); expressionStack.add(invokedMethod); } break; case Opcodes.INVOKESTATIC: final Type type = Type.getObjectType(methodInsnNode.owner); try { final Class<?> sourceClass = Class.forName(type.getClassName()); final Method javaMethod = ReflectionUtils.findJavaMethod(sourceClass, methodInsnNode.name, parameterTypes); final Class<?> returnType = findReturnType(insnCursor, javaMethod); final MethodInvocation invokedStaticMethod = new MethodInvocation( new ClassLiteral(sourceClass), javaMethod, returnType, args); expressionStack.add(invokedStaticMethod); } catch (ClassNotFoundException e) { throw new AnalyzeException("Failed to retrieve class for " + methodInsnNode.owner, e); } break; default: throw new AnalyzeException("Unexpected method invocation type: " + methodInsnNode.getOpcode()); } break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: final InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) currentInstruction; final Handle handle = (Handle) invokeDynamicInsnNode.bsmArgs[1]; final int argNumber = Type.getArgumentTypes(invokeDynamicInsnNode.desc).length; final List<CapturedArgumentRef> lambdaArgs = new ArrayList<>(); for (int i = 0; i < argNumber; i++) { final Expression expr = expressionStack.pop(); if (expr.getExpressionType() != ExpressionType.CAPTURED_ARGUMENT_REF) { throw new AnalyzeException("Unexpected argument type when following InvokeDynamic call: " + expr.getExpressionType()); } lambdaArgs.add((CapturedArgumentRef) expr); // , expr.getValue() } Collections.reverse(lambdaArgs); final EmbeddedSerializedLambdaInfo lambdaInfo = new EmbeddedSerializedLambdaInfo(handle.getOwner(), handle.getName(), handle.getDesc(), lambdaArgs, capturedArguments); final LambdaExpression lambdaExpression = LambdaExpressionAnalyzer.getInstance() .analyzeExpression(lambdaInfo); expressionStack.add(lambdaExpression); break; case AbstractInsnNode.JUMP_INSN: statements.addAll( readJumpInstruction(insnCursor, expressionStack, capturedArguments, localVariables)); return statements; case AbstractInsnNode.INT_INSN: readIntInstruction((IntInsnNode) currentInstruction, expressionStack, localVariables); break; case AbstractInsnNode.INSN: final List<Statement> instructionStatement = readInstruction(insnCursor, expressionStack, capturedArguments, localVariables); statements.addAll(instructionStatement); break; case AbstractInsnNode.TYPE_INSN: readTypeInstruction((TypeInsnNode) currentInstruction, expressionStack, localVariables); break; default: throw new AnalyzeException( "This is embarrassing... We've reached an unexpected instruction operator: " + currentInstruction.getType()); } insnCursor.next(); } return statements; }
From source file:org.lambdamatic.mongodb.apt.LambdamaticAnnotationProcessorTest.java
@Test @WithDomainClass(Foo.class) @WithDomainClass(Bar.class) public void shouldProcessFooClassAndGenerateQueryMetadataClass() throws ClassNotFoundException { // verification final Class<?> fooQueryClass = Class.forName("com.sample.QFoo"); ClassAssertion.assertThat(fooQueryClass).isAbstract().isImplementing(QueryMetadata.class, Foo.class); // id/*from www .j a v a 2 s.co m*/ FieldAssertion.assertThat(fooQueryClass, "id").isParameterizedType(QueryField.class, ObjectId.class) .isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", EncoderUtils.MONGOBD_DOCUMENT_ID); // stringField: *custom name in the @DocumentField annotation* FieldAssertion.assertThat(fooQueryClass, "stringField").isParameterizedType(QueryField.class, String.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringField_"); // transientStringField: annotated with @Transient, should not be here ClassAssertion.assertThat(fooQueryClass).hasNoField("transientStringField"); // primitiveByteField: on @DocumentField annotation: should use class field name as document // field name FieldAssertion.assertThat(fooQueryClass, "primitiveByteField") .isParameterizedType(QueryField.class, Byte.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveByteField"); // byteField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "byteField").isParameterizedType(QueryField.class, Byte.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "byteField"); // primitiveShortField FieldAssertion.assertThat(fooQueryClass, "primitiveShortField") .isParameterizedType(QueryField.class, Short.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveShortField"); // shortField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "shortField").isParameterizedType(QueryField.class, Short.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "shortField"); // primitiveIntField FieldAssertion.assertThat(fooQueryClass, "primitiveIntField") .isParameterizedType(QueryField.class, Integer.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveIntField"); // integerField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "integerField") .isParameterizedType(QueryField.class, Integer.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "integerField"); // primitiveLongField FieldAssertion.assertThat(fooQueryClass, "primitiveLongField") .isParameterizedType(QueryField.class, Long.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveLongField"); // longField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "longField").isParameterizedType(QueryField.class, Long.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "longField"); // primitiveFloatField FieldAssertion.assertThat(fooQueryClass, "primitiveFloatField") .isParameterizedType(QueryField.class, Float.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveFloatField"); // floatField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "floatField").isParameterizedType(QueryField.class, Float.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "floatField"); // primitiveDoubleField FieldAssertion.assertThat(fooQueryClass, "primitiveDoubleField") .isParameterizedType(QueryField.class, Double.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveDoubleField"); // doubleField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "doubleField").isParameterizedType(QueryField.class, Double.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "doubleField"); // primitiveBooleanField FieldAssertion.assertThat(fooQueryClass, "primitiveBooleanField") .isParameterizedType(QueryField.class, Boolean.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveBooleanField"); // booleanField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "booleanField") .isParameterizedType(QueryField.class, Boolean.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "booleanField"); // primitiveCharField FieldAssertion.assertThat(fooQueryClass, "primitiveCharField") .isParameterizedType(QueryField.class, Character.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "primitiveCharField"); // characterField: metadata field is primitive type instead of class wrapper FieldAssertion.assertThat(fooQueryClass, "characterField") .isParameterizedType(QueryField.class, Character.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "characterField"); // location FieldAssertion.assertThat(fooQueryClass, "location").isType(LocationField.class).isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "location"); // enumFoo FieldAssertion.assertThat(fooQueryClass, "enumFoo").isParameterizedType(QueryField.class, EnumFoo.class) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "enumFoo"); // bar FieldAssertion.assertThat(fooQueryClass, "bar").isType("com.sample.QBar").isNotFinal().isNotStatic() .hasAnnotation(DocumentField.class, "name", "bar"); // barList FieldAssertion.assertThat(fooQueryClass, "barList") .isParameterizedType(QueryArray.class, ClassUtils.getClass("com.sample.QBar")).isNotFinal() .isNotStatic().hasAnnotation(DocumentField.class, "name", "barList"); // barMap FieldAssertion.assertThat(fooQueryClass, "barMap") .isParameterizedType(QueryMap.class, String.class, Class.forName("com.sample.QBar")).isNotFinal() .isNotStatic().hasAnnotation(DocumentField.class, "name", "barMap"); // barArray FieldAssertion.assertThat(fooQueryClass, "barArray") .isParameterizedType(QueryArray.class, ClassUtils.getClass("com.sample.QBar")).isNotFinal() .isNotStatic().hasAnnotation(DocumentField.class, "name", "barArray"); // enumBarArray FieldAssertion.assertThat(fooQueryClass, "enumBarArray") .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, ClassUtils.getClass("com.sample.EnumBar"))) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "enumBarArray"); // stringArray FieldAssertion.assertThat(fooQueryClass, "stringArray") .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, String.class)) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringArray"); // stringSet FieldAssertion.assertThat(fooQueryClass, "stringSet") .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, String.class)) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringSet"); // stringMap FieldAssertion.assertThat(fooQueryClass, "stringMap") .isParameterizedType(QueryMap.class, String.class, TypeUtils.parameterize(QueryField.class, String.class)) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringMap"); // bytes FieldAssertion.assertThat(fooQueryClass, "bytes") .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, Byte.class)) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytes"); // bytesList FieldAssertion.assertThat(fooQueryClass, "bytesList") .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryArray.class, TypeUtils.parameterize(QueryField.class, Byte.class))) .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytesList"); }
From source file:org.lambdamatic.mongodb.apt.template.ElementUtils.java
/** * Attempts to load the Java {@link Class} associated with the given {@link TypeMirror}. This may * not be possible if the {@link TypeMirror} corresponds to a user class that has not been * compiled yet, in which case the method returns <code>null</code>. * // w w w . j a v a2 s . com * @param variableType the {@link TypeMirror} to analyze * @return the Java {@link Class} or <code>null</code> if it could not be loaded. */ public static Class<?> getVariableType(final TypeMirror variableType) { try { return ClassUtils.getClass(variableType.toString()); } catch (ClassNotFoundException e) { return null; } }
From source file:org.lambdamatic.mongodb.apt.template.ElementUtils.java
/** * Attempts to load the Java {@link Class} associated with the given {@code className}. This may * not be possible if the {@link TypeMirror} corresponds to a user class that has not been * compiled yet, in which case the method returns <code>null</code>. * /*from ww w .jav a2 s . c om*/ * @param className the fully qualified name of the class to load. * @return the Java {@link Class} or <code>null</code> if it could not be loaded. */ public static Class<?> toClass(final String className) { try { return ClassUtils.getClass(className); } catch (ClassNotFoundException e) { return null; } }